Add options for customizing threadiness, logger encoding, and global logger level

This commit is contained in:
Alex Wong
2019-03-05 14:30:23 +08:00
parent 296015faff
commit 104e8ef050
2 changed files with 23 additions and 2 deletions
+19 -1
View File
@@ -1,6 +1,7 @@
package logging
import (
"flag"
"fmt"
"os"
@@ -8,6 +9,17 @@ import (
"go.uber.org/zap/zapcore"
)
var (
replaceGlobals bool
encoding string
)
func init() {
flag.BoolVar(&replaceGlobals, "zap-replace-globals", false, "Whether to change level of global zap logger.")
flag.StringVar(&encoding, "zap-encoding", "json", "Zap logger encoding.")
}
// NewLogger returns a zap sugared logger configured with json format and caller id
func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
level := zap.NewAtomicLevelAt(zapcore.InfoLevel)
@@ -47,7 +59,7 @@ func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
Encoding: encoding,
EncoderConfig: zapEncoderConfig,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
@@ -60,6 +72,12 @@ func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
return logger.Sugar(), nil
}
func ReplaceGlobalIf(logger *zap.Logger) {
if replaceGlobals {
zap.ReplaceGlobals(logger)
}
}
// Console writes to stdout if the console env var exists
func Console(a ...interface{}) (n int, err error) {
if os.Getenv("console") != "" {