1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // A Level is the importance or severity of a log event.
16 // The higher the level, the more important or severe the event.
19 // Names for common levels.
21 // Level numbers are inherently arbitrary,
22 // but we picked them to satisfy three constraints.
23 // Any system can map them to another numbering scheme if it wishes.
25 // First, we wanted the default level to be Info, Since Levels are ints, Info is
26 // the default value for int, zero.
28 // Second, we wanted to make it easy to use levels to specify logger verbosity.
29 // Since a larger level means a more severe event, a logger that accepts events
30 // with smaller (or more negative) level means a more verbose logger. Logger
31 // verbosity is thus the negation of event severity, and the default verbosity
32 // of 0 accepts all events at least as severe as INFO.
34 // Third, we wanted some room between levels to accommodate schemes with named
35 // levels between ours. For example, Google Cloud Logging defines a Notice level
36 // between Info and Warn. Since there are only a few of these intermediate
37 // levels, the gap between the numbers need not be large. Our gap of 4 matches
38 // OpenTelemetry's mapping. Subtracting 9 from an OpenTelemetry level in the
39 // DEBUG, INFO, WARN and ERROR ranges converts it to the corresponding slog
40 // Level range. OpenTelemetry also has the names TRACE and FATAL, which slog
41 // does not. But those OpenTelemetry levels can still be represented as slog
42 // Levels by using the appropriate integers.
50 // String returns a name for the level.
51 // If the level has a name, then that name
52 // in uppercase is returned.
53 // If the level is between named values, then
54 // an integer is appended to the uppercased name.
57 // LevelWarn.String() => "WARN"
58 // (LevelInfo+2).String() => "INFO+2"
59 func (l Level) String() string {
60 str := func(base string, val Level) string {
64 return fmt.Sprintf("%s%+d", base, val)
69 return str("DEBUG", l-LevelDebug)
71 return str("INFO", l-LevelInfo)
73 return str("WARN", l-LevelWarn)
75 return str("ERROR", l-LevelError)
79 // MarshalJSON implements [encoding/json.Marshaler]
80 // by quoting the output of [Level.String].
81 func (l Level) MarshalJSON() ([]byte, error) {
82 // AppendQuote is sufficient for JSON-encoding all Level strings.
83 // They don't contain any runes that would produce invalid JSON
85 return strconv.AppendQuote(nil, l.String()), nil
88 // UnmarshalJSON implements [encoding/json.Unmarshaler]
89 // It accepts any string produced by [Level.MarshalJSON],
91 // It also accepts numeric offsets that would result in a different string on
92 // output. For example, "Error-8" would marshal as "INFO".
93 func (l *Level) UnmarshalJSON(data []byte) error {
94 s, err := strconv.Unquote(string(data))
101 // MarshalText implements [encoding.TextMarshaler]
102 // by calling [Level.String].
103 func (l Level) MarshalText() ([]byte, error) {
104 return []byte(l.String()), nil
107 // UnmarshalText implements [encoding.TextUnmarshaler].
108 // It accepts any string produced by [Level.MarshalText],
110 // It also accepts numeric offsets that would result in a different string on
111 // output. For example, "Error-8" would marshal as "INFO".
112 func (l *Level) UnmarshalText(data []byte) error {
113 return l.parse(string(data))
116 func (l *Level) parse(s string) (err error) {
119 err = fmt.Errorf("slog: level string %q: %w", s, err)
125 if i := strings.IndexAny(s, "+-"); i >= 0 {
127 offset, err = strconv.Atoi(s[i:])
132 switch strings.ToUpper(name) {
142 return errors.New("unknown name")
148 // Level returns the receiver.
149 // It implements [Leveler].
150 func (l Level) Level() Level { return l }
152 // A LevelVar is a [Level] variable, to allow a [Handler] level to change
154 // It implements [Leveler] as well as a Set method,
155 // and it is safe for use by multiple goroutines.
156 // The zero LevelVar corresponds to [LevelInfo].
157 type LevelVar struct {
161 // Level returns v's level.
162 func (v *LevelVar) Level() Level {
163 return Level(int(v.val.Load()))
166 // Set sets v's level to l.
167 func (v *LevelVar) Set(l Level) {
168 v.val.Store(int64(l))
171 func (v *LevelVar) String() string {
172 return fmt.Sprintf("LevelVar(%s)", v.Level())
175 // MarshalText implements [encoding.TextMarshaler]
176 // by calling [Level.MarshalText].
177 func (v *LevelVar) MarshalText() ([]byte, error) {
178 return v.Level().MarshalText()
181 // UnmarshalText implements [encoding.TextUnmarshaler]
182 // by calling [Level.UnmarshalText].
183 func (v *LevelVar) UnmarshalText(data []byte) error {
185 if err := l.UnmarshalText(data); err != nil {
192 // A Leveler provides a [Level] value.
194 // As Level itself implements Leveler, clients typically supply
195 // a Level value wherever a Leveler is needed, such as in [HandlerOptions].
196 // Clients who need to vary the level dynamically can provide a more complex
197 // Leveler implementation such as *[LevelVar].
198 type Leveler interface {