golog.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package golog
  2. import (
  3. "io"
  4. "github.com/kataras/pio"
  5. )
  6. // NewLine can override the default package-level line breaker, "\n".
  7. // It should be called (in-sync) before the print or leveled functions.
  8. //
  9. // See `github.com/kataras/pio#NewLine` and `Logger#NewLine` too.
  10. func NewLine(newLineChar string) {
  11. pio.NewLine = []byte(newLineChar)
  12. }
  13. // Default is the package-level ready-to-use logger,
  14. // level had set to "info", is changeable.
  15. var Default = New()
  16. // Reset re-sets the default logger to an empty one.
  17. func Reset() {
  18. Default = New()
  19. }
  20. // SetOutput overrides the Default Logger's Printer's output with another `io.Writer`.
  21. func SetOutput(w io.Writer) {
  22. Default.SetOutput(w)
  23. }
  24. // AddOutput adds one or more `io.Writer` to the Default Logger's Printer.
  25. //
  26. // If one of the "writers" is not a terminal-based (i.e File)
  27. // then colors will be disabled for all outputs.
  28. func AddOutput(writers ...io.Writer) {
  29. Default.AddOutput(writers...)
  30. }
  31. // SetPrefix sets a prefix for the default package-level Logger.
  32. //
  33. // The prefix is the first space-separated
  34. // word that is being presented to the output.
  35. // It's written even before the log level text.
  36. //
  37. // Returns itself.
  38. func SetPrefix(s string) *Logger {
  39. return Default.SetPrefix(s)
  40. }
  41. // SetTimeFormat sets time format for logs,
  42. // if "s" is empty then time representation will be off.
  43. func SetTimeFormat(s string) {
  44. Default.SetTimeFormat(s)
  45. }
  46. // SetLevel accepts a string representation of
  47. // a `Level` and returns a `Level` value based on that "levelName".
  48. //
  49. // Available level names are:
  50. // "disable"
  51. // "fatal"
  52. // "error"
  53. // "warn"
  54. // "info"
  55. // "debug"
  56. //
  57. // Alternatively you can use the exported `Default.Level` field, i.e `Default.Level = golog.ErrorLevel`
  58. func SetLevel(levelName string) {
  59. Default.SetLevel(levelName)
  60. }
  61. // Print prints a log message without levels and colors.
  62. func Print(v ...interface{}) {
  63. Default.Print(v...)
  64. }
  65. // Println prints a log message without levels and colors.
  66. // It adds a new line at the end.
  67. func Println(v ...interface{}) {
  68. Default.Println(v...)
  69. }
  70. // Logf prints a leveled log message to the output.
  71. // This method can be used to use custom log levels if needed.
  72. // It adds a new line in the end.
  73. func Logf(level Level, format string, args ...interface{}) {
  74. Default.Logf(level, format, args...)
  75. }
  76. // Fatal `os.Exit(1)` exit no matter the level of the logger.
  77. // If the logger's level is fatal, error, warn, info or debug
  78. // then it will print the log message too.
  79. func Fatal(v ...interface{}) {
  80. Default.Fatal(v...)
  81. }
  82. // Fatalf will `os.Exit(1)` no matter the level of the logger.
  83. // If the logger's level is fatal, error, warn, info or debug
  84. // then it will print the log message too.
  85. func Fatalf(format string, args ...interface{}) {
  86. Default.Fatalf(format, args...)
  87. }
  88. // Error will print only when logger's Level is error, warn, info or debug.
  89. func Error(v ...interface{}) {
  90. Default.Error(v...)
  91. }
  92. // Errorf will print only when logger's Level is error, warn, info or debug.
  93. func Errorf(format string, args ...interface{}) {
  94. Default.Errorf(format, args...)
  95. }
  96. // Warn will print when logger's Level is warn, info or debug.
  97. func Warn(v ...interface{}) {
  98. Default.Warn(v...)
  99. }
  100. // Warnf will print when logger's Level is warn, info or debug.
  101. func Warnf(format string, args ...interface{}) {
  102. Default.Warnf(format, args...)
  103. }
  104. // Info will print when logger's Level is info or debug.
  105. func Info(v ...interface{}) {
  106. Default.Info(v...)
  107. }
  108. // Infof will print when logger's Level is info or debug.
  109. func Infof(format string, args ...interface{}) {
  110. Default.Infof(format, args...)
  111. }
  112. // Debug will print when logger's Level is debug.
  113. func Debug(v ...interface{}) {
  114. Default.Debug(v...)
  115. }
  116. // Debugf will print when logger's Level is debug.
  117. func Debugf(format string, args ...interface{}) {
  118. Default.Debugf(format, args...)
  119. }
  120. // Install receives an external logger
  121. // and automatically adapts its print functions.
  122. //
  123. // Install adds a golog handler to support third-party integrations,
  124. // it can be used only once per `golog#Logger` instance.
  125. //
  126. // For example, if you want to print using a logrus
  127. // logger you can do the following:
  128. // `golog.Install(logrus.StandardLogger())`
  129. //
  130. // Look `golog#Handle` for more.
  131. func Install(logger ExternalLogger) {
  132. Default.Install(logger)
  133. }
  134. // InstallStd receives a standard logger
  135. // and automatically adapts its print functions.
  136. //
  137. // Install adds a golog handler to support third-party integrations,
  138. // it can be used only once per `golog#Logger` instance.
  139. //
  140. // Example Code:
  141. // import "log"
  142. // myLogger := log.New(os.Stdout, "", 0)
  143. // InstallStd(myLogger)
  144. //
  145. // Look `golog#Handle` for more.
  146. func InstallStd(logger StdLogger) {
  147. Default.InstallStd(logger)
  148. }
  149. // Handle adds a log handler to the default logger.
  150. //
  151. // Handlers can be used to intercept the message between a log value
  152. // and the actual print operation, it's called
  153. // when one of the print functions called.
  154. // If it's return value is true then it means that the specific
  155. // handler handled the log by itself therefore no need to
  156. // proceed with the default behavior of printing the log
  157. // to the specified logger's output.
  158. //
  159. // It stops on the handler which returns true firstly.
  160. // The `Log` value holds the level of the print operation as well.
  161. func Handle(handler Handler) {
  162. Default.Handle(handler)
  163. }
  164. // Hijack adds a hijacker to the low-level logger's Printer.
  165. // If you need to implement such as a low-level hijacker manually,
  166. // then you have to make use of the pio library.
  167. func Hijack(hijacker func(ctx *pio.Ctx)) {
  168. Default.Hijack(hijacker)
  169. }
  170. // Scan scans everything from "r" and prints
  171. // its new contents to the logger's Printer's Output,
  172. // forever or until the returning "cancel" is fired, once.
  173. func Scan(r io.Reader) (cancel func()) {
  174. return Default.Scan(r)
  175. }
  176. // Child (creates if not exists and) returns a new child
  177. // Logger based on the default package-level logger instance.
  178. //
  179. // Can be used to separate logs by category.
  180. func Child(name string) *Logger {
  181. return Default.Child(name)
  182. }