-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(logger): Moved initialization function
- Loading branch information
1 parent
006fe3b
commit 6ffbd14
Showing
2 changed files
with
44 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
var Logger *zap.Logger | ||
|
||
func InitLogger() { | ||
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||
enc.AppendString(t.Format("02/01/2006 03:04 PM")) | ||
} | ||
consoleConfig := zap.NewDevelopmentEncoderConfig() | ||
consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
consoleConfig.EncodeTime = customTimeEncoder | ||
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig) | ||
defaultLogLevel := zapcore.DebugLevel | ||
|
||
fileEncoderConfig := zap.NewProductionEncoderConfig() | ||
fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
fileEncoder := zapcore.NewJSONEncoder(fileEncoderConfig) | ||
|
||
fileWriter := zapcore.AddSync(&lumberjack.Logger{ | ||
Filename: "logs/app.log", | ||
MaxSize: 10, | ||
MaxBackups: 3, | ||
MaxAge: 7, | ||
Compress: true, | ||
}) | ||
|
||
core := zapcore.NewTee( | ||
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel), | ||
zapcore.NewCore(fileEncoder, fileWriter, defaultLogLevel), | ||
) | ||
|
||
Logger = zap.New(core, zap.AddStacktrace(zapcore.ErrorLevel)) | ||
} |