feat: adding log rotation to Azure-IPAM Zap logger via lumberjack (#1523)

* adding log rotation to Azure-IPAM Zap logger via lumberjack

* feat: adding log rotation to Azure-IPAM Zap logger via lumberjack

* feat: adding log rotation to Azure-IPAM Zap logger via lumberjack. comments addressed.

* feat: adding log rotation to Azure-IPAM Zap logger via lumberjack. Addressed the empty filepath comment.

* feat: adding log rotation to Azure-IPAM Zap logger via lumberjack
This commit is contained in:
Behzad Mirkhanzadeh 2022-08-25 08:19:03 -07:00 коммит произвёл GitHub
Родитель 93c1233a70
Коммит 1fec709a25
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 36 добавлений и 40 удалений

Просмотреть файл

@ -71,6 +71,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.24.2 // indirect

Просмотреть файл

@ -842,6 +842,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
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/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=

Просмотреть файл

@ -1,61 +1,53 @@
package logger
import (
"strings"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
Filepath = "/var/log/azure-ipam.log"
)
type Config struct {
Level string
OutputPaths string // comma separated list of paths
ErrorOutputPaths string // comma separated list of paths
Level string // Debug by default
Filepath string // default /var/log/azure-ipam.log
MaxSizeInMB int // MegaBytes
MaxBackups int // # of backups, no limitation by default
}
// NewLogger creates and returns a zap logger and a clean up function
func New(cfg *Config) (*zap.Logger, func(), error) {
loggerCfg := &zap.Config{}
level, err := zapcore.ParseLevel(cfg.Level)
logLevel, err := zapcore.ParseLevel(cfg.Level)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to parse log level")
}
loggerCfg.Level = zap.NewAtomicLevelAt(level)
loggerCfg.Encoding = "json"
loggerCfg.OutputPaths = getLogOutputPath(cfg.OutputPaths)
loggerCfg.ErrorOutputPaths = getErrOutputPath(cfg.ErrorOutputPaths)
loggerCfg.EncoderConfig = zapcore.EncoderConfig{
TimeKey: "time",
MessageKey: "msg",
LevelKey: "level",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
if cfg.Filepath == "" {
cfg.Filepath = Filepath
}
logger, err := loggerCfg.Build()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to build zap logger")
}
logger := newFileLogger(cfg, logLevel)
cleanup := func() {
_ = logger.Sync()
}
return logger, cleanup, nil
}
func getLogOutputPath(paths string) []string {
if paths == "" {
return nil
}
return strings.Split(paths, ",")
}
func getErrOutputPath(paths string) []string {
if paths == "" {
return nil
}
return strings.Split(paths, ",")
// create and return a zap logger via lumbejack with rotation
func newFileLogger(cfg *Config, logLevel zapcore.Level) (*zap.Logger) {
// define a lumberjack fileWriter
logFileWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: cfg.Filepath,
MaxSize: cfg.MaxSizeInMB, // MegaBytes
MaxBackups: cfg.MaxBackups,
})
// define the log encoding
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
// create a new zap logger
core := zapcore.NewCore(jsonEncoder, logFileWriter, logLevel)
logger := zap.New(core)
return logger
}

Просмотреть файл

@ -22,9 +22,10 @@ func main() {
func executePlugin() error {
// logger config
loggerCfg := &logger.Config{
Level: "debug",
OutputPaths: "var/log/azure-ipam.log",
ErrorOutputPaths: "var/log/azure-ipam.log",
Level: "debug",
Filepath: "/var/log/azure-ipam.log",
MaxSizeInMB: 5, // MegaBytes
MaxBackups: 8,
}
// Create logger
pluginLogger, cleanup, err := logger.New(loggerCfg)