log.go 4.0 KB

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