Skip to content

Commit

Permalink
feat: use lumberjack for log rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 30, 2022
1 parent 59ec17a commit 046bbb3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
13 changes: 7 additions & 6 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "github.com/alist-org/alist/v3/drivers"
"github.com/alist-org/alist/v3/internal/bootstrap"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
Expand All @@ -36,7 +37,7 @@ the address is defined in config file`,
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
server.Init(r)
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
log.Infof("start server @ %s", base)
utils.Log.Infof("start server @ %s", base)
srv := &http.Server{Addr: base, Handler: r}
go func() {
var err error
Expand All @@ -47,7 +48,7 @@ the address is defined in config file`,
err = srv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Errorf("failed to start: %s", err.Error())
utils.Log.Errorf("failed to start: %s", err.Error())
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
Expand All @@ -58,19 +59,19 @@ the address is defined in config file`,
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
utils.Log.Println("Shutdown Server ...")

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
utils.Log.Fatal("Server Shutdown:", err)
}
// catching ctx.Done(). timeout of 3 seconds.
select {
case <-ctx.Done():
log.Println("timeout of 3 seconds.")
utils.Log.Println("timeout of 3 seconds.")
}
log.Println("Server exiting")
utils.Log.Println("Server exiting")
},
}

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ go 1.18
require (
github.com/Xhofe/go-cache v0.0.0-20220723083548-714439c8af9a
github.com/caarlos0/env/v6 v6.9.3
github.com/disintegration/imaging v1.6.2
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.8.0
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/json-iterator/go v1.1.12
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.3.0
github.com/sirupsen/logrus v1.8.1
Expand All @@ -25,7 +26,6 @@ require (

require (
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
Expand All @@ -43,9 +43,7 @@ require (
github.com/jackc/pgx/v4 v4.16.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.3.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.13 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -59,5 +57,6 @@ require (
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
13 changes: 5 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
Expand Down Expand Up @@ -111,8 +112,6 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand All @@ -131,12 +130,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand All @@ -159,6 +152,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down Expand Up @@ -299,6 +294,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
4 changes: 2 additions & 2 deletions internal/bootstrap/aria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package bootstrap

import (
"github.com/alist-org/alist/v3/internal/aria2"
log "github.com/sirupsen/logrus"
"github.com/alist-org/alist/v3/pkg/utils"
)

func InitAria2() {
go func() {
_, err := aria2.InitClient(2)
log.Errorf("failed to init aria2 client: %+v", err)
utils.Log.Errorf("failed to init aria2 client: %+v", err)
}()
}
58 changes: 25 additions & 33 deletions internal/bootstrap/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,49 @@ package bootstrap

import (
"log"
"time"

"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
)

func init() {
logrus.SetFormatter(&logrus.TextFormatter{
formatter := logrus.TextFormatter{
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
logrus.SetFormatter(&formatter)
utils.Log.SetFormatter(&formatter)
// logrus.SetLevel(logrus.DebugLevel)
}

func Log() {
log.SetOutput(logrus.StandardLogger().Out)
func setLog(l *logrus.Logger) {
if flags.Debug || flags.Dev {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetReportCaller(true)
l.SetLevel(logrus.DebugLevel)
l.SetReportCaller(true)
} else {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetReportCaller(false)
l.SetLevel(logrus.InfoLevel)
l.SetReportCaller(false)
}
}

func Log() {
log.SetOutput(logrus.StandardLogger().Out)
setLog(logrus.StandardLogger())
setLog(utils.Log)
logConfig := conf.Conf.Log
if logConfig.Enable {
var (
writer *rotatelogs.RotateLogs
err error
)
if logConfig.Name != "" {
writer, err = rotatelogs.New(
logConfig.Path,
rotatelogs.WithLinkName(logConfig.Name),
rotatelogs.WithRotationCount(logConfig.RotationCount),
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
)
} else {
writer, err = rotatelogs.New(
logConfig.Path,
rotatelogs.WithRotationCount(logConfig.RotationCount),
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
)
}
if err != nil {
logrus.Fatalf("failed to create rotate logrus: %s", err)
}
logrus.SetOutput(writer)
logrus.SetOutput(&lumberjack.Logger{
Filename: logConfig.Name,
MaxSize: logConfig.MaxSize, // megabytes
MaxBackups: logConfig.MaxBackups,
MaxAge: logConfig.MaxAge, //days
Compress: logConfig.Compress, // disabled by default
})
}
logrus.Infof("init logrus...")
utils.Log.Infof("init logrus...")
}
8 changes: 4 additions & 4 deletions internal/bootstrap/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
log "github.com/sirupsen/logrus"
"github.com/alist-org/alist/v3/pkg/utils"
)

func LoadStorages() {
storages, err := db.GetEnabledStorages()
if err != nil {
log.Fatalf("failed get enabled storages: %+v", err)
utils.Log.Fatalf("failed get enabled storages: %+v", err)
}
go func(storages []model.Storage) {
for i := range storages {
err := operations.LoadStorage(context.Background(), storages[i])
if err != nil {
log.Errorf("failed get enabled storages: %+v", err)
utils.Log.Errorf("failed get enabled storages: %+v", err)
} else {
log.Infof("success load storage: [%s], driver: [%s]",
utils.Log.Infof("success load storage: [%s], driver: [%s]",
storages[i].MountPath, storages[i].Driver)
}
}
Expand Down
21 changes: 11 additions & 10 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type Scheme struct {
}

type LogConfig struct {
Enable bool `json:"enable" env:"log_enable"`
Path string `json:"path" env:"LOG_PATH"`
Name string `json:"name" env:"LOG_NAME"`
RotationTime uint `json:"rotation_time" env:"LOG_TIME"`
RotationCount uint `json:"rotation_count" env:"LOG_COUNT"`
Enable bool `json:"enable" env:"log_enable"`
Name string `json:"name" env:"LOG_NAME"`
MaxSize int `json:"max_size" env:"MAX_SIZE"`
MaxBackups int `json:"max_backups" env:"MAX_BACKUPS"`
MaxAge int `json:"max_age" env:"MAX_AGE"`
Compress bool `json:"compress" env:"COMPRESS"`
}

type Config struct {
Expand Down Expand Up @@ -58,11 +59,11 @@ func DefaultConfig() *Config {
},
// CaCheExpiration: 30,
Log: LogConfig{
Enable: true,
Path: "log/%Y-%m-%d-%H:%M.log",
Name: "log/log.log",
RotationTime: 24,
RotationCount: 5,
Enable: true,
Name: "log/log.log",
MaxSize: 10,
MaxBackups: 5,
MaxAge: 28,
},
}
}
7 changes: 7 additions & 0 deletions pkg/utils/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

import (
log "github.com/sirupsen/logrus"
)

var Log = log.New()

0 comments on commit 046bbb3

Please sign in to comment.