github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/design/framework/logger.md (about) 1 # Logging 2 3 Micro need usable logger to be able to write messages about internal state and errors and also provide useful 4 logger interface for end-user. 5 6 ## Overview 7 8 Logger must provide minimal interface to write logs at specific levels with specific fields. 9 10 ## Implemenations 11 12 We have 4 implemntations: 13 * micro internal that writes to console and also to internal in-memory ring buffer 14 * zap 15 * logrus 16 * zerolog 17 18 ## Design 19 20 ```go 21 type Logger interface { 22 // Init initialises options 23 Init(options ...Option) error 24 // The Logger options 25 Options() Options 26 // Fields set fields to always be logged 27 Fields(fields map[string]interface{}) Logger 28 // Log writes a log entry 29 Log(level Level, v ...interface{}) 30 // Logf writes a formatted log entry 31 Logf(level Level, format string, v ...interface{}) 32 // String returns the name of logger 33 String() string 34 } 35 ``` 36 37 Also we have helper functions that automatic uses specified log-level: 38 * Warn/Warnf 39 * Error/Errorf 40 * Debug/Debugf 41 * Info/Infof 42 * Fatal/Fatalf 43 * Trace/Tracef 44 45 This is enought for internal micro usage. Additional helper functions implemented via Helper struct and interface inheritance. 46 47 ```go 48 49 type Helper struct { 50 Logger 51 } 52 53 func NewHelper(log Logger) *Helper { 54 return &Helper{log: log} 55 } 56 57 func (h *Helper) Info(args...interface{}) {} 58 ..... 59 ``` 60 61 ## Benefits 62 63 We don't need to implemet helper functions in all all loggers, but internally helper uses only one Log/Logf 64 65 ## Expected usage 66 67 /main.go: 68 69 ```go 70 ctx, cancel := context.WitchCancel(context.Bacground()) 71 defer cancel() 72 73 ... 74 log := zerolog.NewLogger(logger.WithOutput(os.Stdout), logger.WithLevel(logger.DebugLevel)) 75 logger.NewContext(ctx, log) 76 77 ... 78 handler.RegisterHelloHandler(service.Server(), new(handler.Hello)) 79 .... 80 ``` 81 82 /handler/hello.go: 83 84 ```go 85 ... 86 func (h *Hello) Call(ctx context.Context, req *xx, rsp *yy) error { 87 log := logger.FromContext(ctx) 88 l := logger.NewHelper(log.Fileds(map[string]interace{}{"reqid":req.Id})) 89 ... 90 l.Debug("process request") 91 ... 92 return nil 93 } 94 95 ```