| 1 | // Copyright (c) 2016 Uber Technologies, Inc. |
| 2 | // |
| 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: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 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 |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | package zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ |
| 22 | |
| 23 | import ( |
| 24 | "bytesscip-go gomod github.com/golang/go/src go1.19 bytes/" |
| 25 | "errorsscip-go gomod github.com/golang/go/src go1.19 errors/" |
| 26 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 27 | ) |
| 28 | |
| 29 | var errUnmarshalNilLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/errUnmarshalNilLevel. = errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("can't unmarshal a nil *Level") |
| 30 | |
| 31 | // A Level is a logging priority. Higher levels are more important. |
| 32 | type Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# int8 |
| 33 | |
| 34 | const ( |
| 35 | // DebugLevel logs are typically voluminous, and are usually disabled in |
| 36 | // production. |
| 37 | DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel. Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# = iota - 1 |
| 38 | // InfoLevel is the default logging priority. |
| 39 | InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel. |
| 40 | // WarnLevel logs are more important than Info, but don't need individual |
| 41 | // human review. |
| 42 | WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel. |
| 43 | // ErrorLevel logs are high-priority. If an application is running smoothly, |
| 44 | // it shouldn't generate any error-level logs. |
| 45 | ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel. |
| 46 | // DPanicLevel logs are particularly important errors. In development the |
| 47 | // logger panics after writing the message. |
| 48 | DPanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DPanicLevel. |
| 49 | // PanicLevel logs a message, then panics. |
| 50 | PanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/PanicLevel. |
| 51 | // FatalLevel logs a message, then calls os.Exit(1). |
| 52 | FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel. |
| 53 | |
| 54 | _minLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/_minLevel. = DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel. |
| 55 | _maxLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/_maxLevel. = FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel. |
| 56 | |
| 57 | // InvalidLevel is an invalid value for Level. |
| 58 | // |
| 59 | // Core implementations may panic if they see messages of this level. |
| 60 | InvalidLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InvalidLevel. = _maxLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/_maxLevel. + 1 |
| 61 | ) |
| 62 | |
| 63 | // ParseLevel parses a level based on the lower-case or all-caps ASCII |
| 64 | // representation of the log level. If the provided ASCII representation is |
| 65 | // invalid an error is returned. |
| 66 | // |
| 67 | // This is particularly useful when dealing with text input to configure log |
| 68 | // levels. |
| 69 | func ParseLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ParseLevel().(text string) (Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#, error) { |
| 70 | var level Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# |
| 71 | err := level.UnmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#UnmarshalText().([]byte(text)) |
| 72 | return level, err |
| 73 | } |
| 74 | |
| 75 | type leveledEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/leveledEnabler# interface { |
| 76 | LevelEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler# |
| 77 | |
| 78 | Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/leveledEnabler#Level.() Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# |
| 79 | } |
| 80 | |
| 81 | // LevelOf reports the minimum enabled log level for the given LevelEnabler |
| 82 | // from Zap's supported log levels, or [InvalidLevel] if none of them are |
| 83 | // enabled. |
| 84 | // |
| 85 | // A LevelEnabler may implement a 'Level() Level' method to override the |
| 86 | // behavior of this function. |
| 87 | // |
| 88 | // func (c *core) Level() Level { |
| 89 | // return c.currentLevel |
| 90 | // } |
| 91 | // |
| 92 | // It is recommended that [Core] implementations that wrap other cores use |
| 93 | // LevelOf to retrieve the level of the wrapped core. For example, |
| 94 | // |
| 95 | // func (c *coreWrapper) Level() Level { |
| 96 | // return zapcore.LevelOf(c.wrappedCore) |
| 97 | // } |
| 98 | func LevelOfscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelOf().(enab LevelEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#) Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# { |
| 99 | if lvler, ok := enab.(leveledEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/leveledEnabler#); ok { |
| 100 | return lvler.Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/leveledEnabler#Level.() |
| 101 | } |
| 102 | |
| 103 | for lvl := _minLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/_minLevel.; lvl <= _maxLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/_maxLevel.; lvl++ { |
| 104 | if enab.Enabledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#Enabled.(lvl) { |
| 105 | return lvl |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return InvalidLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InvalidLevel. |
| 110 | } |
| 111 | |
| 112 | // String returns a lower-case ASCII representation of the log level. |
| 113 | func (l Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) Stringscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#String().() string { |
| 114 | switch l { |
| 115 | case DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel.: |
| 116 | return "debug" |
| 117 | case InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.: |
| 118 | return "info" |
| 119 | case WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel.: |
| 120 | return "warn" |
| 121 | case ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.: |
| 122 | return "error" |
| 123 | case DPanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DPanicLevel.: |
| 124 | return "dpanic" |
| 125 | case PanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/PanicLevel.: |
| 126 | return "panic" |
| 127 | case FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel.: |
| 128 | return "fatal" |
| 129 | default: |
| 130 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("Level(%d)", l) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // CapitalString returns an all-caps ASCII representation of the log level. |
| 135 | func (l Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) CapitalStringscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#CapitalString().() string { |
| 136 | // Printing levels in all-caps is common enough that we should export this |
| 137 | // functionality. |
| 138 | switch l { |
| 139 | case DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel.: |
| 140 | return "DEBUG" |
| 141 | case InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.: |
| 142 | return "INFO" |
| 143 | case WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel.: |
| 144 | return "WARN" |
| 145 | case ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.: |
| 146 | return "ERROR" |
| 147 | case DPanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DPanicLevel.: |
| 148 | return "DPANIC" |
| 149 | case PanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/PanicLevel.: |
| 150 | return "PANIC" |
| 151 | case FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel.: |
| 152 | return "FATAL" |
| 153 | default: |
| 154 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("LEVEL(%d)", l) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // MarshalText marshals the Level to text. Note that the text representation |
| 159 | // drops the -Level suffix (see example). |
| 160 | func (l Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) MarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#MarshalText().() ([]byte, error) { |
| 161 | return []byte(l.Stringscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#String().()), nil |
| 162 | } |
| 163 | |
| 164 | // UnmarshalText unmarshals text to a level. Like MarshalText, UnmarshalText |
| 165 | // expects the text representation of a Level to drop the -Level suffix (see |
| 166 | // example). |
| 167 | // |
| 168 | // In particular, this makes it easy to configure logging levels using YAML, |
| 169 | // TOML, or JSON files. |
| 170 | func (l *Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) UnmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#UnmarshalText().(text []byte) error { |
| 171 | if l == nil { |
| 172 | return errUnmarshalNilLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/errUnmarshalNilLevel. |
| 173 | } |
| 174 | if !l.unmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#unmarshalText().(text) && !l.unmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#unmarshalText().(bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.ToLowerscip-go gomod github.com/golang/go/src go1.19 bytes/ToLower().(text)) { |
| 175 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("unrecognized level: %q", text) |
| 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | func (l *Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) unmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#unmarshalText().(text []byte) bool { |
| 181 | switch string(text) { |
| 182 | case "debug", "DEBUG": |
| 183 | *l = DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel. |
| 184 | case "info", "INFO", "": // make the zero value useful |
| 185 | *l = InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel. |
| 186 | case "warn", "WARN": |
| 187 | *l = WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel. |
| 188 | case "error", "ERROR": |
| 189 | *l = ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel. |
| 190 | case "dpanic", "DPANIC": |
| 191 | *l = DPanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DPanicLevel. |
| 192 | case "panic", "PANIC": |
| 193 | *l = PanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/PanicLevel. |
| 194 | case "fatal", "FATAL": |
| 195 | *l = FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel. |
| 196 | default: |
| 197 | return false |
| 198 | } |
| 199 | return true |
| 200 | } |
| 201 | |
| 202 | // Set sets the level for the flag.Value interface. |
| 203 | func (l *Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) Setscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#Set().(s string) error { |
| 204 | return l.UnmarshalTextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#UnmarshalText().([]byte(s)) |
| 205 | } |
| 206 | |
| 207 | // Get gets the level for the flag.Getter interface. |
| 208 | func (l *Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) Getscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#Get().() interface{} { |
| 209 | return *l |
| 210 | } |
| 211 | |
| 212 | // Enabled returns true if the given level is at or above this level. |
| 213 | func (l Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) Enabledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#Enabled().(lvl Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) bool { |
| 214 | return lvl >= l |
| 215 | } |
| 216 | |
| 217 | // LevelEnabler decides whether a given logging level is enabled when logging a |
| 218 | // message. |
| 219 | // |
| 220 | // Enablers are intended to be used to implement deterministic filters; |
| 221 | // concerns like sampling are better implemented as a Core. |
| 222 | // |
| 223 | // Each concrete Level value implements a static LevelEnabler which returns |
| 224 | // true for itself and all higher logging levels. For example WarnLevel.Enabled() |
| 225 | // will return true for WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, and |
| 226 | // FatalLevel, but return false for InfoLevel and DebugLevel. |
| 227 | type LevelEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler# interface { |
| 228 | Enabledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#Enabled.(Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#) bool |
| 229 | } |