@@ -4,37 +4,67 @@ import (
44 "dev.gaijin.team/go/golib/fields"
55)
66
7- // Adapter is an interface that allows to encapsulate any logger inside [Logger].
7+ // Adapter is an interface that allows to encapsulate any logger backend inside
8+ // [Logger].
9+ //
10+ // Adapters provide a bridge between the logger abstraction and concrete logging
11+ // implementations (zap, logrus, slog, etc.). They handle the translation of
12+ // logger's generic API calls to backend-specific operations.
13+ //
14+ // For examples of adapter implementations, see the adapters subpackages.
815type Adapter interface {
9- // Log logs a message with the provided level, message, optional error, and an
10- // arbitrary number of fields.
16+ // Log logs a message with the provided level, message, optional error, and
17+ // additional fields. The level parameter is one of the logger level constants
18+ // (LevelError, LevelWarning, LevelInfo, LevelDebug, LevelTrace) or a custom
19+ // level value.
20+ //
21+ // The err parameter may be nil if no error is associated with the log message.
22+ // Adapters should handle nil errors gracefully.
23+ //
24+ // The fs parameter contains zero or more fields that should be attached to the
25+ // log entry. This may include both fields from WithFields calls and fields
26+ // passed directly to the Log call.
1127 Log (level int , msg string , err error , fs ... fields.Field )
1228
13- // WithFields returns a logger adapter with the given fields attached to it.
29+ // WithFields returns a new adapter instance with the given fields attached.
30+ // The returned adapter should include these fields in all subsequent Log calls.
31+ //
32+ // This method must return a new adapter instance and must not modify the
33+ // original adapter.
34+ //
35+ // Rationale:
36+ //
37+ // Although it might look unnecessary to alter the adapter instead of carrying
38+ // fields in the logger itself, this design allows adapters to optimize field
39+ // handling based on their backend capabilities.
40+ //
41+ // Some logging backends may support efficient field storage and reuse, while
42+ // others may require fields to be passed with each log call. For example zap
43+ // pre-encodes fields and then reuses encoded values, instead of encoding them
44+ // each time a log entry is created.
45+ //
46+ // By attaching fields to the adapter, we enable adapters to implement the
47+ // most efficient strategy for their backend.
1448 WithFields (fs ... fields.Field ) Adapter
1549
16- // WithName returns a logger adapter with the given name attached to it.
17- WithName (name string ) Adapter
18-
19- // WithStackTrace returns a logger adapter with the stack trace attached to it.
20- WithStackTrace (trace string ) Adapter
21-
22- // Flush all logs to the output. It is a no-op for non-buffered loggers.
50+ // Flush flushes any buffered log entries to the output. This is a no-op for
51+ // non-buffered loggers.
2352 //
24- // It is application's responsibility to call this method before it exits.
53+ // It is the application's responsibility to call [Logger.Flush] before exiting
54+ // to ensure all log entries are written. Adapters should return any errors
55+ // that occur during flushing.
2556 Flush () error
2657}
2758
28- // NopAdapter is a no-op logger adapter, it does nothing even if any of its
29- // methods called.
59+ // NopAdapter is a no-op logger adapter that discards all log messages.
60+ //
61+ // This adapter is useful for testing or when logging needs to be disabled
62+ // entirely. All methods are no-ops and return immediately without performing
63+ // any work.
3064type NopAdapter struct {}
3165
3266func (NopAdapter ) Log (int , string , error , ... fields.Field ) {}
3367
3468func (a NopAdapter ) WithFields (... fields.Field ) Adapter { return a }
3569
36- func (a NopAdapter ) WithName (string ) Adapter { return a }
37-
38- func (a NopAdapter ) WithStackTrace (string ) Adapter { return a }
39-
4070func (NopAdapter ) Flush () error { return nil }
0 commit comments