logger.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package hclog
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. "strings"
  7. "sync"
  8. )
  9. var (
  10. //DefaultOutput is used as the default log output.
  11. DefaultOutput io.Writer = os.Stderr
  12. // DefaultLevel is used as the default log level.
  13. DefaultLevel = Info
  14. )
  15. // Level represents a log level.
  16. type Level int32
  17. const (
  18. // NoLevel is a special level used to indicate that no level has been
  19. // set and allow for a default to be used.
  20. NoLevel Level = 0
  21. // Trace is the most verbose level. Intended to be used for the tracing
  22. // of actions in code, such as function enters/exits, etc.
  23. Trace Level = 1
  24. // Debug information for programmer lowlevel analysis.
  25. Debug Level = 2
  26. // Info information about steady state operations.
  27. Info Level = 3
  28. // Warn information about rare but handled events.
  29. Warn Level = 4
  30. // Error information about unrecoverable events.
  31. Error Level = 5
  32. )
  33. // Format is a simple convience type for when formatting is required. When
  34. // processing a value of this type, the logger automatically treats the first
  35. // argument as a Printf formatting string and passes the rest as the values
  36. // to be formatted. For example: L.Info(Fmt{"%d beans/day", beans}).
  37. type Format []interface{}
  38. // Fmt returns a Format type. This is a convience function for creating a Format
  39. // type.
  40. func Fmt(str string, args ...interface{}) Format {
  41. return append(Format{str}, args...)
  42. }
  43. // LevelFromString returns a Level type for the named log level, or "NoLevel" if
  44. // the level string is invalid. This facilitates setting the log level via
  45. // config or environment variable by name in a predictable way.
  46. func LevelFromString(levelStr string) Level {
  47. // We don't care about case. Accept both "INFO" and "info".
  48. levelStr = strings.ToLower(strings.TrimSpace(levelStr))
  49. switch levelStr {
  50. case "trace":
  51. return Trace
  52. case "debug":
  53. return Debug
  54. case "info":
  55. return Info
  56. case "warn":
  57. return Warn
  58. case "error":
  59. return Error
  60. default:
  61. return NoLevel
  62. }
  63. }
  64. // Logger describes the interface that must be implemeted by all loggers.
  65. type Logger interface {
  66. // Args are alternating key, val pairs
  67. // keys must be strings
  68. // vals can be any type, but display is implementation specific
  69. // Emit a message and key/value pairs at the TRACE level
  70. Trace(msg string, args ...interface{})
  71. // Emit a message and key/value pairs at the DEBUG level
  72. Debug(msg string, args ...interface{})
  73. // Emit a message and key/value pairs at the INFO level
  74. Info(msg string, args ...interface{})
  75. // Emit a message and key/value pairs at the WARN level
  76. Warn(msg string, args ...interface{})
  77. // Emit a message and key/value pairs at the ERROR level
  78. Error(msg string, args ...interface{})
  79. // Indicate if TRACE logs would be emitted. This and the other Is* guards
  80. // are used to elide expensive logging code based on the current level.
  81. IsTrace() bool
  82. // Indicate if DEBUG logs would be emitted. This and the other Is* guards
  83. IsDebug() bool
  84. // Indicate if INFO logs would be emitted. This and the other Is* guards
  85. IsInfo() bool
  86. // Indicate if WARN logs would be emitted. This and the other Is* guards
  87. IsWarn() bool
  88. // Indicate if ERROR logs would be emitted. This and the other Is* guards
  89. IsError() bool
  90. // Creates a sublogger that will always have the given key/value pairs
  91. With(args ...interface{}) Logger
  92. // Create a logger that will prepend the name string on the front of all messages.
  93. // If the logger already has a name, the new value will be appended to the current
  94. // name. That way, a major subsystem can use this to decorate all it's own logs
  95. // without losing context.
  96. Named(name string) Logger
  97. // Create a logger that will prepend the name string on the front of all messages.
  98. // This sets the name of the logger to the value directly, unlike Named which honor
  99. // the current name as well.
  100. ResetNamed(name string) Logger
  101. // Updates the level. This should affect all sub-loggers as well. If an
  102. // implementation cannot update the level on the fly, it should no-op.
  103. SetLevel(level Level)
  104. // Return a value that conforms to the stdlib log.Logger interface
  105. StandardLogger(opts *StandardLoggerOptions) *log.Logger
  106. // Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
  107. StandardWriter(opts *StandardLoggerOptions) io.Writer
  108. }
  109. // StandardLoggerOptions can be used to configure a new standard logger.
  110. type StandardLoggerOptions struct {
  111. // Indicate that some minimal parsing should be done on strings to try
  112. // and detect their level and re-emit them.
  113. // This supports the strings like [ERROR], [ERR] [TRACE], [WARN], [INFO],
  114. // [DEBUG] and strip it off before reapplying it.
  115. InferLevels bool
  116. }
  117. // LoggerOptions can be used to configure a new logger.
  118. type LoggerOptions struct {
  119. // Name of the subsystem to prefix logs with
  120. Name string
  121. // The threshold for the logger. Anything less severe is supressed
  122. Level Level
  123. // Where to write the logs to. Defaults to os.Stderr if nil
  124. Output io.Writer
  125. // An optional mutex pointer in case Output is shared
  126. Mutex *sync.Mutex
  127. // Control if the output should be in JSON.
  128. JSONFormat bool
  129. // Include file and line information in each log line
  130. IncludeLocation bool
  131. // The time format to use instead of the default
  132. TimeFormat string
  133. }