feat: added the log rotation (#910)
This commit is contained in:
parent
43f6d16236
commit
85485fca16
|
@ -48,7 +48,14 @@ func main() {
|
|||
|
||||
v := config.LoadConfig()
|
||||
|
||||
log.Init(config.Opt.Advanced.LogLevel, config.Opt.Advanced.LogFile, config.Opt.Advanced.Dir)
|
||||
log.Init(config.Opt.Advanced.LogLevel,
|
||||
config.Opt.Advanced.LogFile,
|
||||
config.Opt.Advanced.Dir,
|
||||
config.Opt.Advanced.LogRotation,
|
||||
config.Opt.Advanced.LogMaxSize,
|
||||
config.Opt.Advanced.LogMaxAge,
|
||||
config.Opt.Advanced.LogMaxBackups,
|
||||
config.Opt.Advanced.LogCompress)
|
||||
utils.ChdirAndAcquireFileLock()
|
||||
utils.SetNcpu()
|
||||
utils.SetPprofPort()
|
||||
|
|
2
go.mod
2
go.mod
|
@ -10,10 +10,10 @@ require (
|
|||
github.com/rs/zerolog v1.28.0
|
||||
github.com/spf13/viper v1.18.1
|
||||
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/a8m/envsubst v1.4.2 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,3 @@
|
|||
github.com/a8m/envsubst v1.4.2 h1:4yWIHXOLEJHQEFd4UjrWDrYeYlV7ncFWJOCBRLOZHQg=
|
||||
github.com/a8m/envsubst v1.4.2/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGtlsbY=
|
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
@ -96,6 +94,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8X
|
|||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -41,9 +41,14 @@ type AdvancedOptions struct {
|
|||
StatusPort int `mapstructure:"status_port" default:"0"`
|
||||
|
||||
// log
|
||||
LogFile string `mapstructure:"log_file" default:"shake.log"`
|
||||
LogLevel string `mapstructure:"log_level" default:"info"`
|
||||
LogInterval int `mapstructure:"log_interval" default:"5"`
|
||||
LogFile string `mapstructure:"log_file" default:"shake.log"`
|
||||
LogLevel string `mapstructure:"log_level" default:"info"`
|
||||
LogInterval int `mapstructure:"log_interval" default:"5"`
|
||||
LogRotation bool `mapstructure:"log_rotation" default:"true"`
|
||||
LogMaxSize int `mapstructure:"log_max_size" default:"512"`
|
||||
LogMaxAge int `mapstructure:"log_max_age" default:"7"`
|
||||
LogMaxBackups int `mapstructure:"log_max_backups" default:"3"`
|
||||
LogCompress bool `mapstructure:"log_compress" default:"true"`
|
||||
|
||||
// redis-shake gets key and value from rdb file, and uses RESTORE command to
|
||||
// create the key in target redis. Redis RESTORE will return a "Target key name
|
||||
|
|
|
@ -2,15 +2,17 @@ package log
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var logger zerolog.Logger
|
||||
|
||||
func Init(level string, file string, dir string) {
|
||||
func Init(level string, file string, dir string, rotation bool, size int, age int, backups int, compress bool) {
|
||||
// log level
|
||||
switch level {
|
||||
case "debug":
|
||||
|
@ -38,7 +40,19 @@ func Init(level string, file string, dir string) {
|
|||
|
||||
// log file
|
||||
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006-01-02 15:04:05"}
|
||||
fileWriter, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
var fileWriter io.Writer
|
||||
if rotation {
|
||||
fileWriter = &lumberjack.Logger{
|
||||
Filename: path,
|
||||
MaxSize: size, // megabytes
|
||||
MaxBackups: backups,
|
||||
MaxAge: age, //days
|
||||
Compress: compress, // disabled by default
|
||||
LocalTime: true,
|
||||
}
|
||||
} else {
|
||||
fileWriter, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
}
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("open log file failed. file=[%s], err=[%s]", path, err))
|
||||
}
|
||||
|
|
|
@ -102,6 +102,11 @@ status_port = 0 # status port, 0 means disable
|
|||
log_file = "shake.log"
|
||||
log_level = "info" # debug, info or warn
|
||||
log_interval = 5 # in seconds
|
||||
log_rotation = true # enable log rotation
|
||||
log_max_size = 512 # MiB, logs max size to rotate, default 512 MiB
|
||||
log_max_age = 7 # days, logs are kept, default 7 days
|
||||
log_max_backups = 3 # number of log backups, default 3
|
||||
log_compress = true # enable log compression after rotate, default true
|
||||
|
||||
# redis-shake gets key and value from rdb file, and uses RESTORE command to
|
||||
# create the key in target redis. Redis RESTORE will return a "Target key name
|
||||
|
|
Loading…
Reference in New Issue