level.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package level
  2. import (
  3. "github.com/go-kit/log"
  4. "github.com/go-kit/log/level"
  5. )
  6. // Error returns a logger that includes a Key/ErrorValue pair.
  7. func Error(logger log.Logger) log.Logger {
  8. return level.Error(logger)
  9. }
  10. // Warn returns a logger that includes a Key/WarnValue pair.
  11. func Warn(logger log.Logger) log.Logger {
  12. return level.Warn(logger)
  13. }
  14. // Info returns a logger that includes a Key/InfoValue pair.
  15. func Info(logger log.Logger) log.Logger {
  16. return level.Info(logger)
  17. }
  18. // Debug returns a logger that includes a Key/DebugValue pair.
  19. func Debug(logger log.Logger) log.Logger {
  20. return level.Debug(logger)
  21. }
  22. // NewFilter wraps next and implements level filtering. See the commentary on
  23. // the Option functions for a detailed description of how to configure levels.
  24. // If no options are provided, all leveled log events created with Debug,
  25. // Info, Warn or Error helper methods are squelched and non-leveled log
  26. // events are passed to next unmodified.
  27. func NewFilter(next log.Logger, options ...Option) log.Logger {
  28. return level.NewFilter(next, options...)
  29. }
  30. // Option sets a parameter for the leveled logger.
  31. type Option = level.Option
  32. // AllowAll is an alias for AllowDebug.
  33. func AllowAll() Option {
  34. return level.AllowAll()
  35. }
  36. // AllowDebug allows error, warn, info and debug level log events to pass.
  37. func AllowDebug() Option {
  38. return level.AllowDebug()
  39. }
  40. // AllowInfo allows error, warn and info level log events to pass.
  41. func AllowInfo() Option {
  42. return level.AllowInfo()
  43. }
  44. // AllowWarn allows error and warn level log events to pass.
  45. func AllowWarn() Option {
  46. return level.AllowWarn()
  47. }
  48. // AllowError allows only error level log events to pass.
  49. func AllowError() Option {
  50. return level.AllowError()
  51. }
  52. // AllowNone allows no leveled log events to pass.
  53. func AllowNone() Option {
  54. return level.AllowNone()
  55. }
  56. // ErrNotAllowed sets the error to return from Log when it squelches a log
  57. // event disallowed by the configured Allow[Level] option. By default,
  58. // ErrNotAllowed is nil; in this case the log event is squelched with no
  59. // error.
  60. func ErrNotAllowed(err error) Option {
  61. return level.ErrNotAllowed(err)
  62. }
  63. // SquelchNoLevel instructs Log to squelch log events with no level, so that
  64. // they don't proceed through to the wrapped logger. If SquelchNoLevel is set
  65. // to true and a log event is squelched in this way, the error value
  66. // configured with ErrNoLevel is returned to the caller.
  67. func SquelchNoLevel(squelch bool) Option {
  68. return level.SquelchNoLevel(squelch)
  69. }
  70. // ErrNoLevel sets the error to return from Log when it squelches a log event
  71. // with no level. By default, ErrNoLevel is nil; in this case the log event is
  72. // squelched with no error.
  73. func ErrNoLevel(err error) Option {
  74. return level.ErrNoLevel(err)
  75. }
  76. // NewInjector wraps next and returns a logger that adds a Key/level pair to
  77. // the beginning of log events that don't already contain a level. In effect,
  78. // this gives a default level to logs without a level.
  79. func NewInjector(next log.Logger, lvl Value) log.Logger {
  80. return level.NewInjector(next, lvl)
  81. }
  82. // Value is the interface that each of the canonical level values implement.
  83. // It contains unexported methods that prevent types from other packages from
  84. // implementing it and guaranteeing that NewFilter can distinguish the levels
  85. // defined in this package from all other values.
  86. type Value = level.Value
  87. // Key returns the unique key added to log events by the loggers in this
  88. // package.
  89. func Key() interface{} { return level.Key() }
  90. // ErrorValue returns the unique value added to log events by Error.
  91. func ErrorValue() Value { return level.ErrorValue() }
  92. // WarnValue returns the unique value added to log events by Warn.
  93. func WarnValue() Value { return level.WarnValue() }
  94. // InfoValue returns the unique value added to log events by Info.
  95. func InfoValue() Value { return level.InfoValue() }
  96. // DebugValue returns the unique value added to log events by Debug.
  97. func DebugValue() Value { return level.DebugValue() }