Skip to content

Commit

Permalink
feat: add lumberjack file logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wintbiit committed Dec 12, 2023
1 parent 228f073 commit ab97cc3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ go.work
.idea/
bin/

./ninedns.json
./ninedns.json
logs/
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/redis/go-redis/v9 v9.3.0
github.com/wintbiit/larki v0.1.0
go.uber.org/zap v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
Expand Down
14 changes: 13 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package log
import (
"os"

"gopkg.in/natefinch/lumberjack.v2"

"github.com/wintbiit/ninedns/utils"

"go.uber.org/zap"
Expand All @@ -28,6 +30,14 @@ func NewLogger(module string) *Logger {
}

func NewLoggerWithLevel(module string, level zapcore.Level) *Logger {
lumberjackLogger := zapcore.AddSync(&lumberjack.Logger{
Filename: "logs/" + module + ".log",
MaxSize: 100,
MaxBackups: 10,
MaxAge: 30,
Compress: true,
})

encoder := zap.NewProductionEncoderConfig()
encoder.EncodeTime = zapcore.ISO8601TimeEncoder
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
Expand All @@ -36,7 +46,9 @@ func NewLoggerWithLevel(module string, level zapcore.Level) *Logger {
enc.AppendString(caller.String())
}

core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), zapcore.Lock(os.Stdout), level)
core := zapcore.NewTee(
zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), zapcore.Lock(os.Stdout), level),
zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), lumberjackLogger, level))

logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel))
logger.Named(module)
Expand Down

0 comments on commit ab97cc3

Please sign in to comment.