Last active
December 16, 2020 22:32
-
-
Save NonLogicalDev/b2ae33095ec17b1780780bf8eceee0a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "go.uber.org/zap" | |
| ) | |
| func init() { | |
| RegisterColorfulLoggerEncoder() | |
| log, err := NewColorfulZapLogger() | |
| if err != nil { | |
| panic(err) | |
| } | |
| zap.ReplaceGlobals(log) | |
| } | |
| type h struct { } | |
| func (h) veryLongFunctionNameJustForTestingLogger() { | |
| zap.L().Named("test1").Named("test2").Info("Hello") | |
| } | |
| func main() { | |
| log := zap.L() | |
| log = log. | |
| With(zap.String("string", "value")). | |
| With(zap.Int("int", 1)). | |
| With(zap.Any("any", []interface{}{1, "2"})). | |
| Named("ROOT") | |
| log.Debug("This is debug level") | |
| log.Info("This is info level") | |
| log.Warn("This is warn level") | |
| log.Error("This is error level") | |
| h{}.veryLongFunctionNameJustForTestingLogger() | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "sort" | |
| "strings" | |
| "sync" | |
| "time" | |
| "github.com/fatih/color" | |
| "go.uber.org/zap" | |
| "go.uber.org/zap/buffer" | |
| "go.uber.org/zap/zapcore" | |
| ) | |
| const ColorfulLoggerEncoder = "colorful" | |
| var ( | |
| registerOnce = sync.Once{} | |
| baseTimestamp = time.Now() | |
| bufferPool = buffer.NewPool() | |
| levelToColor = map[zapcore.Level]*color.Color{ | |
| zapcore.DebugLevel: color.New(color.FgBlue), | |
| zapcore.InfoLevel: color.New(color.FgGreen), | |
| zapcore.WarnLevel: color.New(color.FgYellow), | |
| zapcore.ErrorLevel: color.New(color.FgRed), | |
| zapcore.DPanicLevel: color.New(color.FgRed), | |
| zapcore.PanicLevel: color.New(color.FgRed), | |
| zapcore.FatalLevel: color.New(color.FgRed), | |
| } | |
| unfocused = color.New(color.FgHiBlack) | |
| ) | |
| func RegisterColorfulLoggerEncoder() { | |
| registerOnce.Do(func() { | |
| zap.RegisterEncoder(ColorfulLoggerEncoder, func(config zapcore.EncoderConfig) (zapcore.Encoder, error) { | |
| return newColorfulLoggerEncoder(), nil | |
| }) | |
| }) | |
| } | |
| func NewColorfulLoggerConfiguration() zap.Config { | |
| cfg := zap.NewDevelopmentConfig() | |
| cfg.Encoding = ColorfulLoggerEncoder | |
| return cfg | |
| } | |
| func NewColorfulZapLogger(options ...zap.Option) (*zap.Logger, error) { | |
| return NewColorfulLoggerConfiguration().Build(options...) | |
| } | |
| type colorfulConsoleEncoder struct { | |
| *zapcore.MapObjectEncoder | |
| } | |
| func newColorfulLoggerEncoder() *colorfulConsoleEncoder { | |
| return &colorfulConsoleEncoder{ | |
| zapcore.NewMapObjectEncoder(), | |
| } | |
| } | |
| func (c *colorfulConsoleEncoder) Clone() zapcore.Encoder { | |
| enc := zapcore.NewMapObjectEncoder() | |
| for k, v := range c.MapObjectEncoder.Fields { | |
| enc.Fields[k] = v | |
| } | |
| return &colorfulConsoleEncoder{enc} | |
| } | |
| func (c *colorfulConsoleEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { | |
| b := bufferPool.Get() | |
| levelColor := levelToColor[entry.Level] | |
| levelName := fmt.Sprintf("%5s", strings.ToUpper(entry.Level.String())) | |
| timeStamp := fmt.Sprintf("[%04d]", int(entry.Time.Sub(baseTimestamp)/time.Second)) | |
| callerName := fmt.Sprintf("%35s", entry.Caller.TrimmedPath()) | |
| prefix := "" | |
| if entry.LoggerName != "" { | |
| prefix = entry.LoggerName + ":" | |
| } | |
| functionName := "" | |
| if entry.Caller.Function != "" { | |
| functionName = fmt.Sprintf("(fn: %v)", entry.Caller.Function) | |
| } | |
| fmt.Fprintf(b, | |
| "%s %s | %s | %s %s %s", | |
| unfocused.Sprint(timeStamp), | |
| levelColor.Sprint(levelName), | |
| unfocused.Sprint(callerName), | |
| unfocused.Sprint(prefix), | |
| entry.Message, | |
| unfocused.Sprintf(functionName), | |
| ) | |
| enc := c | |
| if len(fields) > 0 { | |
| enc = c.Clone().(*colorfulConsoleEncoder) | |
| for i, _ := range fields { | |
| fields[i].AddTo(enc) | |
| } | |
| } | |
| if len(enc.Fields) > 0 { | |
| var keys []string | |
| for k := range enc.Fields { | |
| keys = append(keys, k) | |
| } | |
| sort.Strings(keys) | |
| for _, k := range keys { | |
| value := enc.Fields[k] | |
| marshalledValue, _ := json.Marshal(value) | |
| fmt.Fprintf(b, " %v=%v", levelColor.Sprint(k), string(marshalledValue)) | |
| } | |
| } | |
| if entry.Stack != "" && entry.Level > zap.WarnLevel { | |
| b.AppendString("\n") | |
| b.AppendString(color.RedString("Stacktrace:\n")) | |
| b.AppendString(color.RedString(entry.Stack)) | |
| b.AppendString("\n") | |
| } | |
| if b.Len() != 0 { | |
| b.AppendString("\n") | |
| } | |
| return b, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment