level.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. package zapcore
  21. import (
  22. "bytes"
  23. "errors"
  24. "fmt"
  25. )
  26. var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
  27. // A Level is a logging priority. Higher levels are more important.
  28. type Level int8
  29. const (
  30. // DebugLevel logs are typically voluminous, and are usually disabled in
  31. // production.
  32. DebugLevel Level = iota - 1
  33. // InfoLevel is the default logging priority.
  34. InfoLevel
  35. // WarnLevel logs are more important than Info, but don't need individual
  36. // human review.
  37. WarnLevel
  38. // ErrorLevel logs are high-priority. If an application is running smoothly,
  39. // it shouldn't generate any error-level logs.
  40. ErrorLevel
  41. // DPanicLevel logs are particularly important errors. In development the
  42. // logger panics after writing the message.
  43. DPanicLevel
  44. // PanicLevel logs a message, then panics.
  45. PanicLevel
  46. // FatalLevel logs a message, then calls os.Exit(1).
  47. FatalLevel
  48. _minLevel = DebugLevel
  49. _maxLevel = FatalLevel
  50. )
  51. // String returns a lower-case ASCII representation of the log level.
  52. func (l Level) String() string {
  53. switch l {
  54. case DebugLevel:
  55. return "debug"
  56. case InfoLevel:
  57. return "info"
  58. case WarnLevel:
  59. return "warn"
  60. case ErrorLevel:
  61. return "error"
  62. case DPanicLevel:
  63. return "dpanic"
  64. case PanicLevel:
  65. return "panic"
  66. case FatalLevel:
  67. return "fatal"
  68. default:
  69. return fmt.Sprintf("Level(%d)", l)
  70. }
  71. }
  72. // CapitalString returns an all-caps ASCII representation of the log level.
  73. func (l Level) CapitalString() string {
  74. // Printing levels in all-caps is common enough that we should export this
  75. // functionality.
  76. switch l {
  77. case DebugLevel:
  78. return "DEBUG"
  79. case InfoLevel:
  80. return "INFO"
  81. case WarnLevel:
  82. return "WARN"
  83. case ErrorLevel:
  84. return "ERROR"
  85. case DPanicLevel:
  86. return "DPANIC"
  87. case PanicLevel:
  88. return "PANIC"
  89. case FatalLevel:
  90. return "FATAL"
  91. default:
  92. return fmt.Sprintf("LEVEL(%d)", l)
  93. }
  94. }
  95. // MarshalText marshals the Level to text. Note that the text representation
  96. // drops the -Level suffix (see example).
  97. func (l Level) MarshalText() ([]byte, error) {
  98. return []byte(l.String()), nil
  99. }
  100. // UnmarshalText unmarshals text to a level. Like MarshalText, UnmarshalText
  101. // expects the text representation of a Level to drop the -Level suffix (see
  102. // example).
  103. //
  104. // In particular, this makes it easy to configure logging levels using YAML,
  105. // TOML, or JSON files.
  106. func (l *Level) UnmarshalText(text []byte) error {
  107. if l == nil {
  108. return errUnmarshalNilLevel
  109. }
  110. if !l.unmarshalText(text) && !l.unmarshalText(bytes.ToLower(text)) {
  111. return fmt.Errorf("unrecognized level: %q", text)
  112. }
  113. return nil
  114. }
  115. func (l *Level) unmarshalText(text []byte) bool {
  116. switch string(text) {
  117. case "debug", "DEBUG":
  118. *l = DebugLevel
  119. case "info", "INFO", "": // make the zero value useful
  120. *l = InfoLevel
  121. case "warn", "WARN":
  122. *l = WarnLevel
  123. case "error", "ERROR":
  124. *l = ErrorLevel
  125. case "dpanic", "DPANIC":
  126. *l = DPanicLevel
  127. case "panic", "PANIC":
  128. *l = PanicLevel
  129. case "fatal", "FATAL":
  130. *l = FatalLevel
  131. default:
  132. return false
  133. }
  134. return true
  135. }
  136. // Set sets the level for the flag.Value interface.
  137. func (l *Level) Set(s string) error {
  138. return l.UnmarshalText([]byte(s))
  139. }
  140. // Get gets the level for the flag.Getter interface.
  141. func (l *Level) Get() interface{} {
  142. return *l
  143. }
  144. // Enabled returns true if the given level is at or above this level.
  145. func (l Level) Enabled(lvl Level) bool {
  146. return lvl >= l
  147. }
  148. // LevelEnabler decides whether a given logging level is enabled when logging a
  149. // message.
  150. //
  151. // Enablers are intended to be used to implement deterministic filters;
  152. // concerns like sampling are better implemented as a Core.
  153. //
  154. // Each concrete Level value implements a static LevelEnabler which returns
  155. // true for itself and all higher logging levels. For example WarnLevel.Enabled()
  156. // will return true for WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, and
  157. // FatalLevel, but return false for InfoLevel and DebugLevel.
  158. type LevelEnabler interface {
  159. Enabled(Level) bool
  160. }