1 // Copyright (c) 2016 Uber Technologies, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
31 // A Level is a logging priority. Higher levels are more important.
35 // DebugLevel logs are typically voluminous, and are usually disabled in
37 DebugLevel Level = iota - 1
38 // InfoLevel is the default logging priority.
40 // WarnLevel logs are more important than Info, but don't need individual
43 // ErrorLevel logs are high-priority. If an application is running smoothly,
44 // it shouldn't generate any error-level logs.
46 // DPanicLevel logs are particularly important errors. In development the
47 // logger panics after writing the message.
49 // PanicLevel logs a message, then panics.
51 // FatalLevel logs a message, then calls os.Exit(1).
54 _minLevel = DebugLevel
55 _maxLevel = FatalLevel
58 // String returns a lower-case ASCII representation of the log level.
59 func (l Level) String() string {
76 return fmt.Sprintf("Level(%d)", l)
80 // CapitalString returns an all-caps ASCII representation of the log level.
81 func (l Level) CapitalString() string {
82 // Printing levels in all-caps is common enough that we should export this
100 return fmt.Sprintf("LEVEL(%d)", l)
104 // MarshalText marshals the Level to text. Note that the text representation
105 // drops the -Level suffix (see example).
106 func (l Level) MarshalText() ([]byte, error) {
107 return []byte(l.String()), nil
110 // UnmarshalText unmarshals text to a level. Like MarshalText, UnmarshalText
111 // expects the text representation of a Level to drop the -Level suffix (see
114 // In particular, this makes it easy to configure logging levels using YAML,
115 // TOML, or JSON files.
116 func (l *Level) UnmarshalText(text []byte) error {
118 return errUnmarshalNilLevel
120 if !l.unmarshalText(text) && !l.unmarshalText(bytes.ToLower(text)) {
121 return fmt.Errorf("unrecognized level: %q", text)
126 func (l *Level) unmarshalText(text []byte) bool {
127 switch string(text) {
128 case "debug", "DEBUG":
130 case "info", "INFO", "": // make the zero value useful
134 case "error", "ERROR":
136 case "dpanic", "DPANIC":
138 case "panic", "PANIC":
140 case "fatal", "FATAL":
148 // Set sets the level for the flag.Value interface.
149 func (l *Level) Set(s string) error {
150 return l.UnmarshalText([]byte(s))
153 // Get gets the level for the flag.Getter interface.
154 func (l *Level) Get() interface{} {
158 // Enabled returns true if the given level is at or above this level.
159 func (l Level) Enabled(lvl Level) bool {
163 // LevelEnabler decides whether a given logging level is enabled when logging a
166 // Enablers are intended to be used to implement deterministic filters;
167 // concerns like sampling are better implemented as a Core.
169 // Each concrete Level value implements a static LevelEnabler which returns
170 // true for itself and all higher logging levels. For example WarnLevel.Enabled()
171 // will return true for WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, and
172 // FatalLevel, but return false for InfoLevel and DebugLevel.
173 type LevelEnabler interface {