level.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 zap
  21. import (
  22. "go.uber.org/atomic"
  23. "go.uber.org/zap/internal"
  24. "go.uber.org/zap/zapcore"
  25. )
  26. const (
  27. // DebugLevel logs are typically voluminous, and are usually disabled in
  28. // production.
  29. DebugLevel = zapcore.DebugLevel
  30. // InfoLevel is the default logging priority.
  31. InfoLevel = zapcore.InfoLevel
  32. // WarnLevel logs are more important than Info, but don't need individual
  33. // human review.
  34. WarnLevel = zapcore.WarnLevel
  35. // ErrorLevel logs are high-priority. If an application is running smoothly,
  36. // it shouldn't generate any error-level logs.
  37. ErrorLevel = zapcore.ErrorLevel
  38. // DPanicLevel logs are particularly important errors. In development the
  39. // logger panics after writing the message.
  40. DPanicLevel = zapcore.DPanicLevel
  41. // PanicLevel logs a message, then panics.
  42. PanicLevel = zapcore.PanicLevel
  43. // FatalLevel logs a message, then calls os.Exit(1).
  44. FatalLevel = zapcore.FatalLevel
  45. )
  46. // LevelEnablerFunc is a convenient way to implement zapcore.LevelEnabler with
  47. // an anonymous function.
  48. //
  49. // It's particularly useful when splitting log output between different
  50. // outputs (e.g., standard error and standard out). For sample code, see the
  51. // package-level AdvancedConfiguration example.
  52. type LevelEnablerFunc func(zapcore.Level) bool
  53. // Enabled calls the wrapped function.
  54. func (f LevelEnablerFunc) Enabled(lvl zapcore.Level) bool { return f(lvl) }
  55. // An AtomicLevel is an atomically changeable, dynamic logging level. It lets
  56. // you safely change the log level of a tree of loggers (the root logger and
  57. // any children created by adding context) at runtime.
  58. //
  59. // The AtomicLevel itself is an http.Handler that serves a JSON endpoint to
  60. // alter its level.
  61. //
  62. // AtomicLevels must be created with the NewAtomicLevel constructor to allocate
  63. // their internal atomic pointer.
  64. type AtomicLevel struct {
  65. l *atomic.Int32
  66. }
  67. var _ internal.LeveledEnabler = AtomicLevel{}
  68. // NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging
  69. // enabled.
  70. func NewAtomicLevel() AtomicLevel {
  71. return AtomicLevel{
  72. l: atomic.NewInt32(int32(InfoLevel)),
  73. }
  74. }
  75. // NewAtomicLevelAt is a convenience function that creates an AtomicLevel
  76. // and then calls SetLevel with the given level.
  77. func NewAtomicLevelAt(l zapcore.Level) AtomicLevel {
  78. a := NewAtomicLevel()
  79. a.SetLevel(l)
  80. return a
  81. }
  82. // ParseAtomicLevel parses an AtomicLevel based on a lowercase or all-caps ASCII
  83. // representation of the log level. If the provided ASCII representation is
  84. // invalid an error is returned.
  85. //
  86. // This is particularly useful when dealing with text input to configure log
  87. // levels.
  88. func ParseAtomicLevel(text string) (AtomicLevel, error) {
  89. a := NewAtomicLevel()
  90. l, err := zapcore.ParseLevel(text)
  91. if err != nil {
  92. return a, err
  93. }
  94. a.SetLevel(l)
  95. return a, nil
  96. }
  97. // Enabled implements the zapcore.LevelEnabler interface, which allows the
  98. // AtomicLevel to be used in place of traditional static levels.
  99. func (lvl AtomicLevel) Enabled(l zapcore.Level) bool {
  100. return lvl.Level().Enabled(l)
  101. }
  102. // Level returns the minimum enabled log level.
  103. func (lvl AtomicLevel) Level() zapcore.Level {
  104. return zapcore.Level(int8(lvl.l.Load()))
  105. }
  106. // SetLevel alters the logging level.
  107. func (lvl AtomicLevel) SetLevel(l zapcore.Level) {
  108. lvl.l.Store(int32(l))
  109. }
  110. // String returns the string representation of the underlying Level.
  111. func (lvl AtomicLevel) String() string {
  112. return lvl.Level().String()
  113. }
  114. // UnmarshalText unmarshals the text to an AtomicLevel. It uses the same text
  115. // representations as the static zapcore.Levels ("debug", "info", "warn",
  116. // "error", "dpanic", "panic", and "fatal").
  117. func (lvl *AtomicLevel) UnmarshalText(text []byte) error {
  118. if lvl.l == nil {
  119. lvl.l = &atomic.Int32{}
  120. }
  121. var l zapcore.Level
  122. if err := l.UnmarshalText(text); err != nil {
  123. return err
  124. }
  125. lvl.SetLevel(l)
  126. return nil
  127. }
  128. // MarshalText marshals the AtomicLevel to a byte slice. It uses the same
  129. // text representation as the static zapcore.Levels ("debug", "info", "warn",
  130. // "error", "dpanic", "panic", and "fatal").
  131. func (lvl AtomicLevel) MarshalText() (text []byte, err error) {
  132. return lvl.Level().MarshalText()
  133. }