log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.RWMutex{}
  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. // Timing runs f and records the time if took to execute it in "time_taken_ms"
  67. func Timing(f func()) *Event {
  68. return newEvent().Timing(f)
  69. }
  70. // CurrentLevel returns the current log level
  71. func CurrentLevel() Level {
  72. mu.RLock()
  73. defer mu.RUnlock()
  74. return level
  75. }
  76. // SetLevel sets a new log level
  77. func SetLevel(newLevel Level) {
  78. mu.Lock()
  79. defer mu.Unlock()
  80. level = newLevel
  81. }
  82. // SetLevelOverride adds a log override for the given field
  83. func SetLevelOverride(field string, value string, level Level) {
  84. mu.Lock()
  85. defer mu.Unlock()
  86. overrides[field] = &levelOverride{value: value, level: level}
  87. }
  88. // ResetLevelOverrides removes all log level overrides
  89. func ResetLevelOverrides() {
  90. mu.Lock()
  91. defer mu.Unlock()
  92. overrides = make(map[string]*levelOverride)
  93. }
  94. // CurrentFormat returns the current log format
  95. func CurrentFormat() Format {
  96. mu.RLock()
  97. defer mu.RUnlock()
  98. return format
  99. }
  100. // SetFormat sets a new log format
  101. func SetFormat(newFormat Format) {
  102. mu.Lock()
  103. defer mu.Unlock()
  104. format = newFormat
  105. if newFormat == JSONFormat {
  106. DisableDates()
  107. }
  108. }
  109. // SetOutput sets the log output writer
  110. func SetOutput(w io.Writer) {
  111. mu.Lock()
  112. defer mu.Unlock()
  113. log.SetOutput(w)
  114. output = w
  115. }
  116. // File returns the log file, if any, or an empty string otherwise
  117. func File() string {
  118. mu.RLock()
  119. defer mu.RUnlock()
  120. if f, ok := output.(*os.File); ok {
  121. return f.Name()
  122. }
  123. return ""
  124. }
  125. // IsFile returns true if the output is a non-default file
  126. func IsFile() bool {
  127. mu.RLock()
  128. defer mu.RUnlock()
  129. if _, ok := output.(*os.File); ok && output != DefaultOutput {
  130. return true
  131. }
  132. return false
  133. }
  134. // DisableDates disables the date/time prefix
  135. func DisableDates() {
  136. log.SetFlags(0)
  137. }
  138. // Loggable returns true if the given log level is lower or equal to the current log level
  139. func Loggable(l Level) bool {
  140. return CurrentLevel() <= l
  141. }
  142. // IsTrace returns true if the current log level is TraceLevel
  143. func IsTrace() bool {
  144. return Loggable(TraceLevel)
  145. }
  146. // IsDebug returns true if the current log level is DebugLevel or below
  147. func IsDebug() bool {
  148. return Loggable(DebugLevel)
  149. }