golog.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package golog
  2. import (
  3. "io"
  4. "time"
  5. "github.com/kataras/pio"
  6. )
  7. // Now is called to set the log's timestamp value.
  8. // It can be altered through initialization of the program
  9. // to customize the behavior of getting the current time.
  10. var Now func() time.Time = time.Now
  11. // NewLine can override the default package-level line breaker, "\n".
  12. // It should be called (in-sync) before the print or leveled functions.
  13. //
  14. // See `github.com/kataras/pio#NewLine` and `Logger#NewLine` too.
  15. func NewLine(newLineChar string) {
  16. pio.NewLine = []byte(newLineChar)
  17. }
  18. // Default is the package-level ready-to-use logger,
  19. // level had set to "info", is changeable.
  20. var Default = New()
  21. // Reset re-sets the default logger to an empty one.
  22. func Reset() {
  23. Default = New()
  24. }
  25. // SetOutput overrides the Default Logger's Printer's output with another `io.Writer`.
  26. func SetOutput(w io.Writer) {
  27. Default.SetOutput(w)
  28. }
  29. // AddOutput adds one or more `io.Writer` to the Default Logger's Printer.
  30. //
  31. // If one of the "writers" is not a terminal-based (i.e File)
  32. // then colors will be disabled for all outputs.
  33. func AddOutput(writers ...io.Writer) {
  34. Default.AddOutput(writers...)
  35. }
  36. // SetPrefix sets a prefix for the default package-level Logger.
  37. //
  38. // The prefix is the first space-separated
  39. // word that is being presented to the output.
  40. // It's written even before the log level text.
  41. //
  42. // Returns itself.
  43. func SetPrefix(s string) *Logger {
  44. return Default.SetPrefix(s)
  45. }
  46. // SetTimeFormat sets time format for logs,
  47. // if "s" is empty then time representation will be off.
  48. func SetTimeFormat(s string) *Logger {
  49. return Default.SetTimeFormat(s)
  50. }
  51. // SetStacktraceLimit sets a stacktrace entries limit
  52. // on `Debug` level.
  53. // Zero means all number of stack entries will be logged.
  54. // Negative value disables the stacktrace field.
  55. func SetStacktraceLimit(limit int) *Logger {
  56. return Default.SetStacktraceLimit(limit)
  57. }
  58. // RegisterFormatter registers a Formatter for this logger.
  59. func RegisterFormatter(f Formatter) *Logger {
  60. return Default.RegisterFormatter(f)
  61. }
  62. // SetFormat sets a default formatter for all log levels.
  63. func SetFormat(formatter string, opts ...interface{}) *Logger {
  64. return Default.SetFormat(formatter, opts...)
  65. }
  66. // SetLevelFormat changes the output format for the given "levelName".
  67. func SetLevelFormat(levelName string, formatter string, opts ...interface{}) *Logger {
  68. return Default.SetLevelFormat(levelName, formatter, opts...)
  69. }
  70. // SetLevelOutput sets a destination log output for the specific "levelName".
  71. // For multiple writers use the `io.Multiwriter` wrapper.
  72. func SetLevelOutput(levelName string, w io.Writer) *Logger {
  73. return Default.SetLevelOutput(levelName, w)
  74. }
  75. // GetLevelOutput returns the responsible writer for the given "levelName".
  76. // If not a registered writer is set for that level then it returns
  77. // the logger's default printer. It does NOT return nil.
  78. func GetLevelOutput(levelName string) io.Writer {
  79. return Default.GetLevelOutput(levelName)
  80. }
  81. // SetLevel accepts a string representation of
  82. // a `Level` and returns a `Level` value based on that "levelName".
  83. //
  84. // Available level names are:
  85. // "disable"
  86. // "fatal"
  87. // "error"
  88. // "warn"
  89. // "info"
  90. // "debug"
  91. //
  92. // Alternatively you can use the exported `Default.Level` field, i.e `Default.Level = golog.ErrorLevel`
  93. func SetLevel(levelName string) {
  94. Default.SetLevel(levelName)
  95. }
  96. // Print prints a log message without levels and colors.
  97. func Print(v ...interface{}) {
  98. Default.Print(v...)
  99. }
  100. // Println prints a log message without levels and colors.
  101. // It adds a new line at the end.
  102. func Println(v ...interface{}) {
  103. Default.Println(v...)
  104. }
  105. // Logf prints a leveled log message to the output.
  106. // This method can be used to use custom log levels if needed.
  107. // It adds a new line in the end.
  108. func Logf(level Level, format string, args ...interface{}) {
  109. Default.Logf(level, format, args...)
  110. }
  111. // Fatal `os.Exit(1)` exit no matter the level of the logger.
  112. // If the logger's level is fatal, error, warn, info or debug
  113. // then it will print the log message too.
  114. func Fatal(v ...interface{}) {
  115. Default.Fatal(v...)
  116. }
  117. // Fatalf will `os.Exit(1)` no matter the level of the logger.
  118. // If the logger's level is fatal, error, warn, info or debug
  119. // then it will print the log message too.
  120. func Fatalf(format string, args ...interface{}) {
  121. Default.Fatalf(format, args...)
  122. }
  123. // Error will print only when logger's Level is error, warn, info or debug.
  124. func Error(v ...interface{}) {
  125. Default.Error(v...)
  126. }
  127. // Errorf will print only when logger's Level is error, warn, info or debug.
  128. func Errorf(format string, args ...interface{}) {
  129. Default.Errorf(format, args...)
  130. }
  131. // Warn will print when logger's Level is warn, info or debug.
  132. func Warn(v ...interface{}) {
  133. Default.Warn(v...)
  134. }
  135. // Warnf will print when logger's Level is warn, info or debug.
  136. func Warnf(format string, args ...interface{}) {
  137. Default.Warnf(format, args...)
  138. }
  139. // Info will print when logger's Level is info or debug.
  140. func Info(v ...interface{}) {
  141. Default.Info(v...)
  142. }
  143. // Infof will print when logger's Level is info or debug.
  144. func Infof(format string, args ...interface{}) {
  145. Default.Infof(format, args...)
  146. }
  147. // Debug will print when logger's Level is debug.
  148. func Debug(v ...interface{}) {
  149. Default.Debug(v...)
  150. }
  151. // Debugf will print when logger's Level is debug.
  152. func Debugf(format string, args ...interface{}) {
  153. Default.Debugf(format, args...)
  154. }
  155. // Install receives an external logger
  156. // and automatically adapts its print functions.
  157. //
  158. // Install adds a golog handler to support third-party integrations,
  159. // it can be used only once per `golog#Logger` instance.
  160. //
  161. // For example, if you want to print using a logrus
  162. // logger you can do the following:
  163. //
  164. // Install(logrus.StandardLogger())
  165. //
  166. // Or the standard log's Logger:
  167. //
  168. // import "log"
  169. // myLogger := log.New(os.Stdout, "", 0)
  170. // Install(myLogger)
  171. //
  172. // Or even the slog/log's Logger:
  173. //
  174. // import "log/slog"
  175. // myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
  176. // Install(myLogger) OR Install(slog.Default())
  177. //
  178. // Look `golog#Logger.Handle` for more.
  179. func Install(logger any) {
  180. Default.Install(logger)
  181. }
  182. // Handle adds a log handler to the default logger.
  183. //
  184. // Handlers can be used to intercept the message between a log value
  185. // and the actual print operation, it's called
  186. // when one of the print functions called.
  187. // If it's return value is true then it means that the specific
  188. // handler handled the log by itself therefore no need to
  189. // proceed with the default behavior of printing the log
  190. // to the specified logger's output.
  191. //
  192. // It stops on the handler which returns true firstly.
  193. // The `Log` value holds the level of the print operation as well.
  194. func Handle(handler Handler) {
  195. Default.Handle(handler)
  196. }
  197. // Hijack adds a hijacker to the low-level logger's Printer.
  198. // If you need to implement such as a low-level hijacker manually,
  199. // then you have to make use of the pio library.
  200. func Hijack(hijacker func(ctx *pio.Ctx)) {
  201. Default.Hijack(hijacker)
  202. }
  203. // Scan scans everything from "r" and prints
  204. // its new contents to the logger's Printer's Output,
  205. // forever or until the returning "cancel" is fired, once.
  206. func Scan(r io.Reader) (cancel func()) {
  207. return Default.Scan(r)
  208. }
  209. // Child (creates if not exists and) returns a new child
  210. // Logger based on the current logger's fields.
  211. //
  212. // Can be used to separate logs by category.
  213. // If the "key" is string then it's used as prefix,
  214. // which is appended to the current prefix one.
  215. func Child(key interface{}) *Logger {
  216. return Default.Child(key)
  217. }
  218. // SetChildPrefix same as `SetPrefix` but it does NOT
  219. // override the existing, instead the given "s"
  220. // is appended to the current one. It's useful
  221. // to chian loggers with their own names/prefixes.
  222. // It does add the ": " in the end of "s" if it's missing.
  223. // It returns itself.
  224. func SetChildPrefix(s string) *Logger {
  225. return Default.SetChildPrefix(s)
  226. }
  227. // LastChild returns the last registered child Logger.
  228. func LastChild() *Logger {
  229. return Default.LastChild()
  230. }