log.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package log
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. "sync"
  7. "time"
  8. )
  9. // Defaults for package level variables
  10. var (
  11. DefaultLevel = InfoLevel
  12. DefaultFormat = TextFormat
  13. DefaultOutput = os.Stderr
  14. )
  15. var (
  16. level = DefaultLevel
  17. format = DefaultFormat
  18. overrides = make(map[string]*levelOverride)
  19. output io.Writer = DefaultOutput
  20. mu = &sync.Mutex{}
  21. )
  22. // Fatal prints the given message, and exits the program
  23. func Fatal(message string, v ...any) {
  24. newEvent().Fatal(message, v...)
  25. }
  26. // Error prints the given message, if the current log level is ERROR or lower
  27. func Error(message string, v ...any) {
  28. newEvent().Error(message, v...)
  29. }
  30. // Warn prints the given message, if the current log level is WARN or lower
  31. func Warn(message string, v ...any) {
  32. newEvent().Warn(message, v...)
  33. }
  34. // Info prints the given message, if the current log level is INFO or lower
  35. func Info(message string, v ...any) {
  36. newEvent().Info(message, v...)
  37. }
  38. // Debug prints the given message, if the current log level is DEBUG or lower
  39. func Debug(message string, v ...any) {
  40. newEvent().Debug(message, v...)
  41. }
  42. // Trace prints the given message, if the current log level is TRACE
  43. func Trace(message string, v ...any) {
  44. newEvent().Trace(message, v...)
  45. }
  46. // With creates a new log event and adds the fields of the given Contexter structs
  47. func With(contexts ...Contexter) *Event {
  48. return newEvent().With(contexts...)
  49. }
  50. // Field creates a new log event and adds a custom field and value to it
  51. func Field(key string, value any) *Event {
  52. return newEvent().Field(key, value)
  53. }
  54. // Fields creates a new log event and adds a map of fields to it
  55. func Fields(fields Context) *Event {
  56. return newEvent().Fields(fields)
  57. }
  58. // Tag creates a new log event and adds a "tag" field to it
  59. func Tag(tag string) *Event {
  60. return newEvent().Tag(tag)
  61. }
  62. // Time creates a new log event and sets the time field
  63. func Time(time time.Time) *Event {
  64. return newEvent().Time(time)
  65. }
  66. // CurrentLevel returns the current log level
  67. func CurrentLevel() Level {
  68. mu.Lock()
  69. defer mu.Unlock()
  70. return level
  71. }
  72. // SetLevel sets a new log level
  73. func SetLevel(newLevel Level) {
  74. mu.Lock()
  75. defer mu.Unlock()
  76. level = newLevel
  77. }
  78. // SetLevelOverride adds a log override for the given field
  79. func SetLevelOverride(field string, value any, level Level) {
  80. mu.Lock()
  81. defer mu.Unlock()
  82. overrides[field] = &levelOverride{value: value, level: level}
  83. }
  84. // ResetLevelOverrides removes all log level overrides
  85. func ResetLevelOverrides() {
  86. mu.Lock()
  87. defer mu.Unlock()
  88. overrides = make(map[string]*levelOverride)
  89. }
  90. // CurrentFormat returns the current log formt
  91. func CurrentFormat() Format {
  92. mu.Lock()
  93. defer mu.Unlock()
  94. return format
  95. }
  96. // SetFormat sets a new log format
  97. func SetFormat(newFormat Format) {
  98. mu.Lock()
  99. defer mu.Unlock()
  100. format = newFormat
  101. if newFormat == JSONFormat {
  102. DisableDates()
  103. }
  104. }
  105. // SetOutput sets the log output writer
  106. func SetOutput(w io.Writer) {
  107. mu.Lock()
  108. defer mu.Unlock()
  109. log.SetOutput(w)
  110. output = w
  111. }
  112. // File returns the log file, if any, or an empty string otherwise
  113. func File() string {
  114. mu.Lock()
  115. defer mu.Unlock()
  116. if f, ok := output.(*os.File); ok {
  117. return f.Name()
  118. }
  119. return ""
  120. }
  121. // IsFile returns true if the output is a non-default file
  122. func IsFile() bool {
  123. mu.Lock()
  124. defer mu.Unlock()
  125. if _, ok := output.(*os.File); ok && output != DefaultOutput {
  126. return true
  127. }
  128. return false
  129. }
  130. // DisableDates disables the date/time prefix
  131. func DisableDates() {
  132. log.SetFlags(0)
  133. }
  134. // Loggable returns true if the given log level is lower or equal to the current log level
  135. func Loggable(l Level) bool {
  136. return CurrentLevel() <= l
  137. }
  138. // IsTrace returns true if the current log level is TraceLevel
  139. func IsTrace() bool {
  140. return Loggable(TraceLevel)
  141. }
  142. // IsDebug returns true if the current log level is DebugLevel or below
  143. func IsDebug() bool {
  144. return Loggable(DebugLevel)
  145. }