level.go 4.6 KB

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