You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Package log implements a wrapper around the Go standard library's
  2. // logging package. Clients should set the current log level; only
  3. // messages below that level will actually be logged. For example, if
  4. // Level is set to LevelWarning, only log messages at the Warning,
  5. // Error, and Critical levels will be logged.
  6. package log
  7. import (
  8. "fmt"
  9. "log"
  10. "os"
  11. )
  12. // The following constants represent logging levels in increasing levels of seriousness.
  13. const (
  14. // LevelDebug is the log level for Debug statements.
  15. LevelDebug = iota
  16. // LevelInfo is the log level for Info statements.
  17. LevelInfo
  18. // LevelWarning is the log level for Warning statements.
  19. LevelWarning
  20. // LevelError is the log level for Error statements.
  21. LevelError
  22. // LevelCritical is the log level for Critical statements.
  23. LevelCritical
  24. // LevelFatal is the log level for Fatal statements.
  25. LevelFatal
  26. )
  27. var levelPrefix = [...]string{
  28. LevelDebug: "DEBUG",
  29. LevelInfo: "INFO",
  30. LevelWarning: "WARNING",
  31. LevelError: "ERROR",
  32. LevelCritical: "CRITICAL",
  33. LevelFatal: "FATAL",
  34. }
  35. // Level stores the current logging level.
  36. var Level = LevelInfo
  37. // SyslogWriter specifies the necessary methods for an alternate output
  38. // destination passed in via SetLogger.
  39. //
  40. // SyslogWriter is satisfied by *syslog.Writer.
  41. type SyslogWriter interface {
  42. Debug(string)
  43. Info(string)
  44. Warning(string)
  45. Err(string)
  46. Crit(string)
  47. Emerg(string)
  48. }
  49. // syslogWriter stores the SetLogger() parameter.
  50. var syslogWriter SyslogWriter
  51. // SetLogger sets the output used for output by this package.
  52. // A *syslog.Writer is a good choice for the logger parameter.
  53. // Call with a nil parameter to revert to default behavior.
  54. func SetLogger(logger SyslogWriter) {
  55. syslogWriter = logger
  56. }
  57. func print(l int, msg string) {
  58. if l >= Level {
  59. if syslogWriter != nil {
  60. switch l {
  61. case LevelDebug:
  62. syslogWriter.Debug(msg)
  63. case LevelInfo:
  64. syslogWriter.Info(msg)
  65. case LevelWarning:
  66. syslogWriter.Warning(msg)
  67. case LevelError:
  68. syslogWriter.Err(msg)
  69. case LevelCritical:
  70. syslogWriter.Crit(msg)
  71. case LevelFatal:
  72. syslogWriter.Emerg(msg)
  73. }
  74. } else {
  75. log.Printf("[%s] %s", levelPrefix[l], msg)
  76. }
  77. }
  78. }
  79. func outputf(l int, format string, v []interface{}) {
  80. print(l, fmt.Sprintf(format, v...))
  81. }
  82. func output(l int, v []interface{}) {
  83. print(l, fmt.Sprint(v...))
  84. }
  85. // Fatalf logs a formatted message at the "fatal" level and then exits. The
  86. // arguments are handled in the same manner as fmt.Printf.
  87. func Fatalf(format string, v ...interface{}) {
  88. outputf(LevelFatal, format, v)
  89. os.Exit(1)
  90. }
  91. // Fatal logs its arguments at the "fatal" level and then exits.
  92. func Fatal(v ...interface{}) {
  93. output(LevelFatal, v)
  94. os.Exit(1)
  95. }
  96. // Criticalf logs a formatted message at the "critical" level. The
  97. // arguments are handled in the same manner as fmt.Printf.
  98. func Criticalf(format string, v ...interface{}) {
  99. outputf(LevelCritical, format, v)
  100. }
  101. // Critical logs its arguments at the "critical" level.
  102. func Critical(v ...interface{}) {
  103. output(LevelCritical, v)
  104. }
  105. // Errorf logs a formatted message at the "error" level. The arguments
  106. // are handled in the same manner as fmt.Printf.
  107. func Errorf(format string, v ...interface{}) {
  108. outputf(LevelError, format, v)
  109. }
  110. // Error logs its arguments at the "error" level.
  111. func Error(v ...interface{}) {
  112. output(LevelError, v)
  113. }
  114. // Warningf logs a formatted message at the "warning" level. The
  115. // arguments are handled in the same manner as fmt.Printf.
  116. func Warningf(format string, v ...interface{}) {
  117. outputf(LevelWarning, format, v)
  118. }
  119. // Warning logs its arguments at the "warning" level.
  120. func Warning(v ...interface{}) {
  121. output(LevelWarning, v)
  122. }
  123. // Infof logs a formatted message at the "info" level. The arguments
  124. // are handled in the same manner as fmt.Printf.
  125. func Infof(format string, v ...interface{}) {
  126. outputf(LevelInfo, format, v)
  127. }
  128. // Info logs its arguments at the "info" level.
  129. func Info(v ...interface{}) {
  130. output(LevelInfo, v)
  131. }
  132. // Debugf logs a formatted message at the "debug" level. The arguments
  133. // are handled in the same manner as fmt.Printf.
  134. func Debugf(format string, v ...interface{}) {
  135. outputf(LevelDebug, format, v)
  136. }
  137. // Debug logs its arguments at the "debug" level.
  138. func Debug(v ...interface{}) {
  139. output(LevelDebug, v)
  140. }