Logging
Logging
Section titled “Logging”go-zero’s logx package provides structured, level-based, high-performance logging with zero allocations on the hot path.
Basic Usage
Section titled “Basic Usage”import "github.com/zeromicro/go-zero/core/logx"
logx.Info("server started")logx.Infof("listening on port %d", port)logx.Errorw("database error", logx.Field("err", err), logx.Field("query", sql))Log Levels
Section titled “Log Levels”logx.Debug("verbose debug info") // hidden by defaultlogx.Info("normal operation")logx.Slow("query took 200ms") // slow threshold logginglogx.Error("something failed")logx.Severe("critical condition") // also writes to stderrConfiguration
Section titled “Configuration”Log: ServiceName: order-api Mode: file # "console" | "file" | "volume" Path: /var/log/order-api Level: info # "debug" | "info" | "error" Compress: false KeepDays: 7 Encoding: json # "json" | "plain"Context Logging
Section titled “Context Logging”Attach request-scoped fields automatically:
func (l *OrderLogic) CreateOrder(req *types.OrderReq) (*types.OrderResp, error) { l.Logger.Infow("creating order", logx.Field("userId", req.UserId), logx.Field("amount", req.Amount), ) ...}Disable in Tests
Section titled “Disable in Tests”func TestMain(m *testing.M) { logx.Disable() os.Exit(m.Run())}