|
| 1 | +// Package logxzap provides an implementation of the logx.Logger interface |
| 2 | +// backed by the Uber Zap logging library. |
| 3 | +// |
| 4 | +// It allows applications to use the generic logx logging API while leveraging |
| 5 | +// Zap's high-performance structured logging capabilities. This helps maintain |
| 6 | +// a consistent logging abstraction across different logger backends. |
| 7 | +// |
| 8 | +// The package includes: |
| 9 | +// - Logger: an adapter that wraps zap.Logger and implements logx.Logger. |
| 10 | +// - New: creates a Logger from an existing zap.Logger. |
| 11 | +// - DefaultLogger: returns a preconfigured console logger at Debug level. |
| 12 | +// |
| 13 | +// This package is intended for use where you want to: |
| 14 | +// - Keep application logging code decoupled from the underlying logging library. |
| 15 | +// - Maintain structured, leveled logging with minimal overhead. |
| 16 | +// - Reuse the same logging interface (logx.Logger) across different backends. |
| 17 | +// |
| 18 | +// Example: |
| 19 | +// |
| 20 | +// import ( |
| 21 | +// "github.com/scylladb/alternator-client-golang/shared/logx" |
| 22 | +// "github.com/scylladb/alternator-client-golang/shared/logxzap" |
| 23 | +// ) |
| 24 | +// |
| 25 | +// func main() { |
| 26 | +// logger := logxzap.DefaultLogger() |
| 27 | +// logger.Info("Application started", logx.A("version", "1.0.0")) |
| 28 | +// } |
| 29 | +package logxzap |
| 30 | + |
| 31 | +import ( |
| 32 | + "os" |
| 33 | + |
| 34 | + "go.uber.org/zap" |
| 35 | + "go.uber.org/zap/zapcore" |
| 36 | + |
| 37 | + "github.com/scylladb/alternator-client-golang/shared/logx" |
| 38 | +) |
| 39 | + |
| 40 | +// Logger is an adapter that implements the logx.Logger interface |
| 41 | +// using an underlying zap.Logger instance. |
| 42 | +type Logger struct { |
| 43 | + z *zap.Logger |
| 44 | +} |
| 45 | + |
| 46 | +// New creates a new Logger that wraps the given zap.Logger. |
| 47 | +func New(z *zap.Logger) *Logger { return &Logger{z: z} } |
| 48 | + |
| 49 | +// Log writes a log entry at the specified level with the given message |
| 50 | +// and optional structured attributes. If the level is disabled, the log |
| 51 | +// is skipped without formatting cost. |
| 52 | +func (l *Logger) Log(lvl logx.Level, msg string, attrs ...logx.Attr) { |
| 53 | + if ce := l.z.Check(toZapLevel(lvl), msg); ce != nil { |
| 54 | + ce.Write(toZapFields(attrs)...) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Debug logs a message at the Debug level with optional structured attributes. |
| 59 | +func (l *Logger) Debug(msg string, attrs ...logx.Attr) { |
| 60 | + l.Log(logx.Debug, msg, attrs...) |
| 61 | +} |
| 62 | + |
| 63 | +// Info logs a message at the Info level with optional structured attributes. |
| 64 | +func (l *Logger) Info(msg string, attrs ...logx.Attr) { |
| 65 | + l.Log(logx.Info, msg, attrs...) |
| 66 | +} |
| 67 | + |
| 68 | +// Warn logs a message at the Warn level with optional structured attributes. |
| 69 | +func (l *Logger) Warn(msg string, attrs ...logx.Attr) { |
| 70 | + l.Log(logx.Warn, msg, attrs...) |
| 71 | +} |
| 72 | + |
| 73 | +// Error logs a message at the Error level with optional structured attributes. |
| 74 | +func (l *Logger) Error(msg string, attrs ...logx.Attr) { |
| 75 | + l.Log(logx.Error, msg, attrs...) |
| 76 | +} |
| 77 | + |
| 78 | +// With returns a new Logger instance that includes the given attributes |
| 79 | +// in all subsequent log entries. |
| 80 | +func (l *Logger) With(attrs ...logx.Attr) logx.Logger { |
| 81 | + return &Logger{z: l.z.With(toZapFields(attrs)...)} |
| 82 | +} |
| 83 | + |
| 84 | +// Named returns a new Logger instance with an additional name scope. |
| 85 | +// The name appears in the log output and helps identify the log source. |
| 86 | +func (l *Logger) Named(name string) logx.Logger { |
| 87 | + return &Logger{z: l.z.Named(name)} |
| 88 | +} |
| 89 | + |
| 90 | +// Enabled reports whether the specified log level is enabled. |
| 91 | +func (l *Logger) Enabled(lvl logx.Level) bool { |
| 92 | + return l.z.Core().Enabled(toZapLevel(lvl)) |
| 93 | +} |
| 94 | + |
| 95 | +// toZapLevel converts a logx.Level to the corresponding zapcore.Level. |
| 96 | +func toZapLevel(l logx.Level) zapcore.Level { |
| 97 | + switch l { |
| 98 | + case logx.Debug: |
| 99 | + return zapcore.DebugLevel |
| 100 | + case logx.Info: |
| 101 | + return zapcore.InfoLevel |
| 102 | + case logx.Warn: |
| 103 | + return zapcore.WarnLevel |
| 104 | + default: |
| 105 | + return zapcore.ErrorLevel |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// toZapFields converts logx.Attr values to zap.Field values. |
| 110 | +func toZapFields(attrs []logx.Attr) []zap.Field { |
| 111 | + if len(attrs) == 0 { |
| 112 | + return nil |
| 113 | + } |
| 114 | + fs := make([]zap.Field, 0, len(attrs)) |
| 115 | + for _, a := range attrs { |
| 116 | + fs = append(fs, zap.Any(a.Key, a.Value)) |
| 117 | + } |
| 118 | + return fs |
| 119 | +} |
| 120 | + |
| 121 | +// DefaultLogger creates a new Logger that writes to standard output |
| 122 | +// using zap's console encoder with ISO8601 timestamps and caller information. |
| 123 | +// Logging is enabled at the Debug level and above. |
| 124 | +func DefaultLogger() logx.Logger { |
| 125 | + cfg := zapcore.EncoderConfig{ |
| 126 | + TimeKey: "T", |
| 127 | + LevelKey: "L", |
| 128 | + NameKey: "N", |
| 129 | + CallerKey: "C", |
| 130 | + MessageKey: "M", |
| 131 | + StacktraceKey: "S", |
| 132 | + LineEnding: zapcore.DefaultLineEnding, |
| 133 | + EncodeLevel: zapcore.CapitalLevelEncoder, |
| 134 | + EncodeTime: zapcore.ISO8601TimeEncoder, |
| 135 | + EncodeDuration: zapcore.StringDurationEncoder, |
| 136 | + EncodeCaller: zapcore.ShortCallerEncoder, |
| 137 | + } |
| 138 | + |
| 139 | + consoleEncoder := zapcore.NewConsoleEncoder(cfg) |
| 140 | + core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zap.DebugLevel) |
| 141 | + |
| 142 | + return New(zap.New(core, zap.AddCaller())) |
| 143 | +} |
0 commit comments