log.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package log
  2. import (
  3. "io"
  4. "os"
  5. "github.com/Sirupsen/logrus"
  6. )
  7. // Default to using the logrus standard logger until log.InitLogger(logLevel) is called
  8. var appLog = logrus.StandardLogger()
  9. var userHook *ShowuserlogHook
  10. type Fields logrus.Fields
  11. type Level logrus.Level
  12. type Logger logrus.Logger
  13. const (
  14. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  15. // message passed to Debug, Info, ...
  16. PanicLevel Level = iota
  17. // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
  18. // logging level is set to Panic.
  19. FatalLevel
  20. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  21. // Commonly used for hooks to send errors to an error tracking service.
  22. ErrorLevel
  23. // WarnLevel level. Non-critical entries that deserve eyes.
  24. WarnLevel
  25. // InfoLevel level. General operational entries about what's going on inside the
  26. // application.
  27. InfoLevel
  28. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  29. DebugLevel
  30. )
  31. func SetOutput(out io.Writer) {
  32. appLog.Out = out
  33. }
  34. func SetLevel(level Level) {
  35. if userHook != nil {
  36. userHook.Level = logrus.Level(level)
  37. } else {
  38. appLog.Level = logrus.Level(level)
  39. logrus.SetLevel(logrus.Level(level))
  40. }
  41. }
  42. func GetLevel() Level {
  43. if userHook != nil {
  44. return Level(userHook.Level)
  45. }
  46. return Level(appLog.Level)
  47. }
  48. func Debugf(format string, args ...interface{}) {
  49. appLog.Debugf(format, args...)
  50. }
  51. func Infof(format string, args ...interface{}) {
  52. appLog.Infof(format, args...)
  53. }
  54. func Printf(format string, args ...interface{}) {
  55. appLog.Printf(format, args...)
  56. }
  57. func Warnf(format string, args ...interface{}) {
  58. appLog.Warnf(format, args...)
  59. }
  60. func Warningf(format string, args ...interface{}) {
  61. appLog.Warningf(format, args...)
  62. }
  63. func Errorf(format string, args ...interface{}) {
  64. appLog.Errorf(format, args...)
  65. }
  66. func Fatalf(format string, args ...interface{}) {
  67. appLog.Fatalf(format, args...)
  68. }
  69. func Panicf(format string, args ...interface{}) {
  70. appLog.Panicf(format, args...)
  71. }
  72. func Debug(args ...interface{}) {
  73. appLog.Debug(args...)
  74. }
  75. func Info(args ...interface{}) {
  76. appLog.Info(args...)
  77. }
  78. func Print(args ...interface{}) {
  79. appLog.Print(args...)
  80. }
  81. func Warn(args ...interface{}) {
  82. appLog.Warn(args...)
  83. }
  84. func Warning(args ...interface{}) {
  85. appLog.Warning(args...)
  86. }
  87. func Error(args ...interface{}) {
  88. appLog.Error(args...)
  89. }
  90. func Fatal(args ...interface{}) {
  91. appLog.Fatal(args...)
  92. }
  93. func Panic(args ...interface{}) {
  94. appLog.Panic(args...)
  95. }
  96. func WithField(key string, value interface{}) *logrus.Entry {
  97. return appLog.WithField(key, value)
  98. }
  99. func WithFields(fields Fields) *logrus.Entry {
  100. return appLog.WithFields(logrus.Fields(fields))
  101. }
  102. func InitLogger() {
  103. if userHook != nil {
  104. return // we've already initialised it
  105. }
  106. thisLog := logrus.New()
  107. // Filter what the user sees (info level, unless they set --debug)
  108. stdLogger := logrus.StandardLogger()
  109. showuserHook, err := NewShowuserlogHook(stdLogger.Level)
  110. if err != nil {
  111. logrus.Errorf("hook failure %s", err)
  112. return
  113. }
  114. filename := "/dev/kmsg"
  115. f, err := os.OpenFile(filename, os.O_WRONLY, 0644)
  116. if err != nil {
  117. logrus.Debugf("error opening %s: %s", filename, err)
  118. } else {
  119. // We're all set up, now we can make it global
  120. appLog = thisLog
  121. userHook = showuserHook
  122. thisLog.Hooks.Add(showuserHook)
  123. logrus.StandardLogger().Hooks.Add(showuserHook)
  124. thisLog.Out = f
  125. logrus.SetOutput(f)
  126. thisLog.Level = logrus.DebugLevel
  127. }
  128. pwd, err := os.Getwd()
  129. if err != nil {
  130. thisLog.Error(err)
  131. }
  132. thisLog.Debugf("START: %v in %s", os.Args, pwd)
  133. }