-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor InitGlobalLogger to Accept String Level Argument
* Add level to logger * Minor fix on InigGlobalLogger * Add error to InitGlobalLogger * Fix linter
- Loading branch information
1 parent
6420004
commit 7ba291b
Showing
4 changed files
with
31 additions
and
6 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
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,21 +1,46 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func InitGlobalLogger() *zap.Logger { | ||
const ( | ||
DebugLevel = "debug" | ||
InfoLevel = "info" | ||
WarnLevel = "warn" | ||
ErrorLevel = "error" | ||
FatalLevel = "fatal" | ||
PanicLevel = "panic" | ||
) | ||
|
||
var stringToLevel = map[string]zapcore.Level{ | ||
DebugLevel: zapcore.DebugLevel, | ||
InfoLevel: zapcore.InfoLevel, | ||
WarnLevel: zapcore.WarnLevel, | ||
ErrorLevel: zapcore.ErrorLevel, | ||
FatalLevel: zapcore.FatalLevel, | ||
PanicLevel: zapcore.PanicLevel, | ||
} | ||
|
||
func InitGlobalLogger(level string) (*zap.Logger, error) { | ||
zapLevel, ok := stringToLevel[strings.ToLower(level)] | ||
if !ok { | ||
return nil, errors.New("log level '%s' is incorrect") | ||
} | ||
|
||
encoderConfig := zap.NewProductionEncoderConfig() | ||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder | ||
encoder := zapcore.NewConsoleEncoder(encoderConfig) | ||
|
||
core := zapcore.NewCore(encoder, os.Stdout, zapcore.DebugLevel) | ||
core := zapcore.NewCore(encoder, os.Stdout, zapLevel) | ||
logger := zap.New(core) | ||
|
||
zap.ReplaceGlobals(logger) | ||
|
||
return logger | ||
return logger, nil | ||
} |
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