-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a41869
commit 1c2401e
Showing
7 changed files
with
131 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ func start() { | |
|
||
// init logger | ||
logger.Init() | ||
|
||
logger := logger.Logger | ||
|
||
// load env config | ||
|
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
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 |
---|---|---|
@@ -1,34 +1 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
var Logger *logrus.Logger | ||
|
||
func Init() *logrus.Logger { | ||
logger := logrus.New() | ||
|
||
logger.SetLevel(logrus.TraceLevel) | ||
logger.SetFormatter(&logrus.JSONFormatter{}) | ||
// logger.SetReportCaller(true) | ||
|
||
logFile := &lumberjack.Logger{ | ||
Filename: "logs/app.log", | ||
MaxSize: 10, | ||
Compress: true, | ||
} | ||
|
||
// multi writer, both file and stdout | ||
writers := []io.Writer{logFile, os.Stdout} | ||
logger.SetOutput(io.MultiWriter(writers...)) | ||
|
||
// export Logger | ||
Logger = logger | ||
|
||
return logger | ||
} |
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,36 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
var Logger *logrus.Logger | ||
|
||
var LogrusLogger *logrus.Logger | ||
|
||
func Init() *logrus.Logger { | ||
logger := logrus.New() | ||
|
||
logger.SetLevel(logrus.TraceLevel) | ||
logger.SetFormatter(&logrus.JSONFormatter{}) | ||
// logger.SetReportCaller(true) | ||
|
||
logFile := &lumberjack.Logger{ | ||
Filename: "logs/app.log", | ||
MaxSize: 10, | ||
Compress: true, | ||
} | ||
|
||
// multi writer, both file and stdout | ||
writers := []io.Writer{logFile, os.Stdout} | ||
logger.SetOutput(io.MultiWriter(writers...)) | ||
|
||
// export Logger | ||
LogrusLogger = logger | ||
|
||
return logger | ||
} |
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,65 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
var ( | ||
ZapLogger *zap.Logger | ||
ZapSugar *zap.SugaredLogger | ||
) | ||
|
||
func InitZapLogger() { | ||
|
||
encoder := getEncoder() | ||
|
||
// First, define our level-handling logic. | ||
|
||
// level: debug,info,warning | ||
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { | ||
return lvl < zapcore.ErrorLevel | ||
}) | ||
// level: error, dpanic, panic, fatal | ||
errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { | ||
return lvl >= zapcore.ErrorLevel | ||
}) | ||
|
||
infoLevel_writerSyncer := getWriterSyncer("info") | ||
errorLevel_writerSyncer := getWriterSyncer("error") | ||
|
||
info_multiWriteSyncer := zapcore.NewMultiWriteSyncer(infoLevel_writerSyncer, os.Stdout) | ||
error_multiWriteSyncer := zapcore.NewMultiWriteSyncer(errorLevel_writerSyncer, os.Stdout) | ||
|
||
core := zapcore.NewCore(encoder, info_multiWriteSyncer, infoLevel) | ||
errorCore := zapcore.NewCore(encoder, error_multiWriteSyncer, errorLevel) | ||
|
||
coreArr := []zapcore.Core{core, errorCore} | ||
|
||
// export | ||
ZapLogger = zap.New(zapcore.NewTee(coreArr...), zap.AddCaller()) // zap.AddCaller() will add line number and file name | ||
ZapSugar = ZapLogger.Sugar() | ||
} | ||
|
||
func getEncoder() zapcore.Encoder { | ||
encoderConfig := zap.NewProductionEncoderConfig() | ||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder | ||
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder | ||
return zapcore.NewJSONEncoder(encoderConfig) | ||
} | ||
|
||
func getWriterSyncer(level string) zapcore.WriteSyncer { | ||
lumberWriteSyncer := &lumberjack.Logger{ | ||
Filename: fmt.Sprintf("logs/%s.log", level), | ||
MaxSize: 10, // megabytes | ||
Compress: true, | ||
} | ||
// file, _ := os.Create("logs/app.log") | ||
return zapcore.Lock(zapcore.AddSync(lumberWriteSyncer)) | ||
|
||
} |