2019-11-06 12:33:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2020-03-03 10:36:20 +00:00
|
|
|
type (
|
|
|
|
zapLogger struct {
|
|
|
|
zapcore.Core
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
logger interface {
|
|
|
|
grpclog.LoggerV2
|
|
|
|
Println(v ...interface{})
|
|
|
|
}
|
|
|
|
)
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
const (
|
|
|
|
formatJSON = "json"
|
|
|
|
formatConsole = "console"
|
|
|
|
|
|
|
|
defaultSamplingInitial = 100
|
|
|
|
defaultSamplingThereafter = 100
|
|
|
|
)
|
|
|
|
|
2020-03-03 10:36:20 +00:00
|
|
|
func gRPCLogger(l *zap.Logger) logger {
|
2020-02-29 15:00:20 +00:00
|
|
|
log := l.WithOptions(
|
|
|
|
// skip gRPCLog + zapLogger in caller
|
|
|
|
zap.AddCallerSkip(2))
|
2019-12-13 16:02:48 +00:00
|
|
|
return &zapLogger{
|
2020-02-29 15:00:20 +00:00
|
|
|
Core: log.Core(),
|
|
|
|
log: log.Sugar(),
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
func safeLevel(lvl string) zap.AtomicLevel {
|
|
|
|
switch strings.ToLower(lvl) {
|
|
|
|
case "debug":
|
|
|
|
return zap.NewAtomicLevelAt(zap.DebugLevel)
|
|
|
|
case "warn":
|
|
|
|
return zap.NewAtomicLevelAt(zap.WarnLevel)
|
|
|
|
case "error":
|
|
|
|
return zap.NewAtomicLevelAt(zap.ErrorLevel)
|
|
|
|
case "fatal":
|
|
|
|
return zap.NewAtomicLevelAt(zap.FatalLevel)
|
|
|
|
case "panic":
|
|
|
|
return zap.NewAtomicLevelAt(zap.PanicLevel)
|
|
|
|
default:
|
|
|
|
return zap.NewAtomicLevelAt(zap.InfoLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
func newLogger(v *viper.Viper) *zap.Logger {
|
2019-11-06 12:33:46 +00:00
|
|
|
c := zap.NewProductionConfig()
|
|
|
|
|
|
|
|
c.OutputPaths = []string{"stdout"}
|
|
|
|
c.ErrorOutputPaths = []string{"stdout"}
|
|
|
|
|
|
|
|
if v.IsSet("logger.sampling") {
|
|
|
|
c.Sampling = &zap.SamplingConfig{
|
|
|
|
Initial: defaultSamplingInitial,
|
|
|
|
Thereafter: defaultSamplingThereafter,
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := v.GetInt("logger.sampling.initial"); val > 0 {
|
|
|
|
c.Sampling.Initial = val
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := v.GetInt("logger.sampling.thereafter"); val > 0 {
|
|
|
|
c.Sampling.Thereafter = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// logger level
|
|
|
|
c.Level = safeLevel(v.GetString("logger.level"))
|
|
|
|
traceLvl := safeLevel(v.GetString("logger.trace_level"))
|
|
|
|
|
|
|
|
// logger format
|
|
|
|
switch f := v.GetString("logger.format"); strings.ToLower(f) {
|
|
|
|
case formatConsole:
|
|
|
|
c.Encoding = formatConsole
|
|
|
|
default:
|
|
|
|
c.Encoding = formatJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
// logger time
|
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
|
|
|
|
l, err := c.Build(
|
|
|
|
// enable trace only for current log-level
|
|
|
|
zap.AddStacktrace(traceLvl))
|
|
|
|
if err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
panic(err)
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.GetBool("logger.no_disclaimer") {
|
2019-12-21 10:26:14 +00:00
|
|
|
return l
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := v.GetString("app.name")
|
|
|
|
version := v.GetString("app.version")
|
|
|
|
|
|
|
|
return l.With(
|
|
|
|
zap.String("app_name", name),
|
2019-12-21 10:26:14 +00:00
|
|
|
zap.String("app_version", version))
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) }
|
2020-02-28 16:44:32 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
2020-02-28 16:44:32 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.Fatalf(format, args...) }
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-29 15:00:20 +00:00
|
|
|
func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) }
|