1414 limitations under the License.
1515*/
1616
17+ // Package log provides types and functions related to logging, passing
18+ // loggers through a context, and attaching context to the logger.
19+ //
20+ // # Transitional types
21+ //
22+ // This package contains various types that are aliases for types in [logrus].
23+ // These aliases are intended for transitioning away from hard-coding logrus
24+ // as logging implementation. Consumers of this package are encouraged to use
25+ // the type-aliases from this package instead of directly using their logrus
26+ // equivalent.
27+ //
28+ // The intent is to replace these aliases with locally defined types and
29+ // interfaces once all consumers are no longer directly importing logrus
30+ // types.
31+ //
32+ // IMPORTANT: due to the transitional purpose of this package, it is not
33+ // guaranteed for the full logrus API to be provided in the future. As
34+ // outlined, these aliases are provided as a step to transition away from
35+ // a specific implementation which, as a result, exposes the full logrus API.
36+ // While no decisions have been made on the ultimate design and interface
37+ // provided by this package, we do not expect carrying "less common" features.
1738package log
1839
1940import (
@@ -23,98 +44,138 @@ import (
2344 "github.com/sirupsen/logrus"
2445)
2546
26- var (
27- // G is an alias for GetLogger.
28- //
29- // We may want to define this locally to a package to get package tagged log
30- // messages.
31- G = GetLogger
47+ // L is an alias for the standard logger.
48+ var L = & Entry {
49+ Logger : logrus . StandardLogger (),
50+ // Default is three fields plus a little extra room.
51+ Data : make ( Fields , 6 ),
52+ }
3253
33- // L is an alias for the standard logger.
34- L = logrus .NewEntry (logrus .StandardLogger ())
35- )
54+ type loggerKey struct {}
3655
37- type (
38- loggerKey struct {}
56+ // Fields type to pass to "WithFields".
57+ type Fields = map [ string ] any
3958
40- // Fields type to pass to `WithFields`, alias from `logrus`.
41- Fields = logrus.Fields
59+ // Entry is a logging entry. It contains all the fields passed with
60+ // [Entry.WithFields]. It's finally logged when Trace, Debug, Info, Warn,
61+ // Error, Fatal or Panic is called on it. These objects can be reused and
62+ // passed around as much as you wish to avoid field duplication.
63+ //
64+ // Entry is a transitional type, and currently an alias for [logrus.Entry].
65+ type Entry = logrus.Entry
4266
43- // Level is a logging level
44- Level = logrus.Level
45- )
67+ // RFC3339NanoFixed is [time.RFC3339Nano] with nanoseconds padded using
68+ // zeros to ensure the formatted time is always the same number of
69+ // characters.
70+ const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
71+
72+ // Level is a logging level.
73+ type Level = logrus.Level
4674
75+ // Supported log levels.
4776const (
48- // RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
49- // ensure the formatted time is always the same number of characters .
50- RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
77+ // TraceLevel level. Designates finer-grained informational events
78+ // than [DebugLevel] .
79+ TraceLevel Level = logrus . TraceLevel
5180
52- // TextFormat represents the text logging format
53- TextFormat = "text"
81+ // DebugLevel level. Usually only enabled when debugging. Very verbose
82+ // logging.
83+ DebugLevel Level = logrus .DebugLevel
5484
55- // JSONFormat represents the JSON logging format
56- JSONFormat = "json"
85+ // InfoLevel level. General operational entries about what's going on
86+ // inside the application.
87+ InfoLevel Level = logrus .InfoLevel
5788
58- // TraceLevel level.
59- TraceLevel = logrus .TraceLevel
89+ // WarnLevel level. Non-critical entries that deserve eyes .
90+ WarnLevel Level = logrus .WarnLevel
6091
61- // DebugLevel level.
62- DebugLevel = logrus .DebugLevel
92+ // ErrorLevel level. Logs errors that should definitely be noted.
93+ // Commonly used for hooks to send errors to an error tracking service.
94+ ErrorLevel Level = logrus .ErrorLevel
6395
64- // InfoLevel level.
65- InfoLevel = logrus .InfoLevel
96+ // FatalLevel level. Logs and then calls "logger.Exit(1)". It exits
97+ // even if the logging level is set to Panic.
98+ FatalLevel Level = logrus .FatalLevel
99+
100+ // PanicLevel level. This is the highest level of severity. Logs and
101+ // then calls panic with the message passed to Debug, Info, ...
102+ PanicLevel Level = logrus .PanicLevel
66103)
67104
68- // SetLevel sets log level globally.
105+ // SetLevel sets log level globally. It returns an error if the given
106+ // level is not supported.
107+ //
108+ // level can be one of:
109+ //
110+ // - "trace" ([TraceLevel])
111+ // - "debug" ([DebugLevel])
112+ // - "info" ([InfoLevel])
113+ // - "warn" ([WarnLevel])
114+ // - "error" ([ErrorLevel])
115+ // - "fatal" ([FatalLevel])
116+ // - "panic" ([PanicLevel])
69117func SetLevel (level string ) error {
70118 lvl , err := logrus .ParseLevel (level )
71119 if err != nil {
72120 return err
73121 }
74122
75- logrus .SetLevel (lvl )
123+ L . Logger .SetLevel (lvl )
76124 return nil
77125}
78126
79127// GetLevel returns the current log level.
80128func GetLevel () Level {
81- return logrus .GetLevel ()
129+ return L . Logger .GetLevel ()
82130}
83131
84- // SetFormat sets log output format
85- func SetFormat (format string ) error {
132+ // OutputFormat specifies a log output format.
133+ type OutputFormat string
134+
135+ // Supported log output formats.
136+ const (
137+ // TextFormat represents the text logging format.
138+ TextFormat OutputFormat = "text"
139+
140+ // JSONFormat represents the JSON logging format.
141+ JSONFormat OutputFormat = "json"
142+ )
143+
144+ // SetFormat sets the log output format ([TextFormat] or [JSONFormat]).
145+ func SetFormat (format OutputFormat ) error {
86146 switch format {
87147 case TextFormat :
88- logrus .SetFormatter (& logrus.TextFormatter {
148+ L . Logger .SetFormatter (& logrus.TextFormatter {
89149 TimestampFormat : RFC3339NanoFixed ,
90150 FullTimestamp : true ,
91151 })
152+ return nil
92153 case JSONFormat :
93- logrus .SetFormatter (& logrus.JSONFormatter {
154+ L . Logger .SetFormatter (& logrus.JSONFormatter {
94155 TimestampFormat : RFC3339NanoFixed ,
95156 })
157+ return nil
96158 default :
97159 return fmt .Errorf ("unknown log format: %s" , format )
98160 }
99-
100- return nil
101161}
102162
103163// WithLogger returns a new context with the provided logger. Use in
104164// combination with logger.WithField(s) for great effect.
105- func WithLogger (ctx context.Context , logger * logrus.Entry ) context.Context {
106- e := logger .WithContext (ctx )
107- return context .WithValue (ctx , loggerKey {}, e )
165+ func WithLogger (ctx context.Context , logger * Entry ) context.Context {
166+ return context .WithValue (ctx , loggerKey {}, logger .WithContext (ctx ))
108167}
109168
110169// GetLogger retrieves the current logger from the context. If no logger is
111170// available, the default logger is returned.
112- func GetLogger (ctx context.Context ) * logrus.Entry {
113- logger := ctx .Value (loggerKey {})
171+ func GetLogger (ctx context.Context ) * Entry {
172+ return G (ctx )
173+ }
114174
115- if logger == nil {
116- return L .WithContext (ctx )
175+ // G is a shorthand for [GetLogger].
176+ func G (ctx context.Context ) * Entry {
177+ if logger := ctx .Value (loggerKey {}); logger != nil {
178+ return logger .(* Entry )
117179 }
118-
119- return logger .(* logrus.Entry )
180+ return L .WithContext (ctx )
120181}
0 commit comments