log.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package logrus
  2. import (
  3. "fmt"
  4. "kpt-tmr-group/pkg/tool"
  5. "path"
  6. "runtime"
  7. "time"
  8. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  9. "github.com/sirupsen/logrus"
  10. )
  11. var (
  12. logPath = "./logger"
  13. logFileName = fmt.Sprintf("/logrus-%s.log", time.Now().Format(tool.DateTime))
  14. )
  15. func init() {
  16. // Log as JSON instead of the default ASCII formatter.
  17. logrus.SetFormatter(&logrus.JSONFormatter{
  18. CallerPrettyfier: func(f *runtime.Frame) (string, string) {
  19. filename := path.Base(f.File)
  20. return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
  21. },
  22. })
  23. // Output to stdout instead of the default stderr
  24. // Can be any io.Writer, see below for File example
  25. writer, _ := rotatelogs.New(
  26. fmt.Sprintf("%s%s", logPath, logFileName),
  27. // rotatelogs.WithLinkName(logPath),
  28. rotatelogs.WithMaxAge(time.Duration(7*24)*time.Hour), // 备份7天的日志
  29. rotatelogs.WithRotationTime(time.Duration(24)*time.Hour), // 24小时切割一次日志
  30. )
  31. logrus.SetOutput(writer) // logrus 设置日志的输出方式
  32. // Only log the warning severity or above.
  33. logrus.SetLevel(DebugLevel)
  34. }
  35. // These are the different logging levels. You can set the logging level to log
  36. // on your instance of logger, obtained with `logrus.New()`.
  37. const (
  38. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  39. // message passed to Debug, Info, ...
  40. PanicLevel Level = iota
  41. // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
  42. // logging level is set to Panic.
  43. FatalLevel
  44. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  45. // Commonly used for hooks to send errors to an error tracking service.
  46. ErrorLevel
  47. // WarnLevel level. Non-critical entries that deserve eyes.
  48. WarnLevel
  49. // InfoLevel level. General operational entries about what's going on inside the
  50. // application.
  51. InfoLevel
  52. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  53. DebugLevel
  54. // TraceLevel level. Designates finer-grained informational events than the Debug.
  55. TraceLevel
  56. )
  57. // Entry is the final or intermediate Logrus logging entry. It contains all
  58. // the fields passed with WithField{,s}. It's finally logged when Debug, Info,
  59. // Warn, Error, Fatal or Panic is called on it. These objects can be reused and
  60. // passed around as much as you wish to avoid field duplication.
  61. type Entry = logrus.Entry
  62. // Fields type, used to pass to `WithFields`.
  63. type Fields = logrus.Fields
  64. // FieldMap allows customization of the key names for default fields.
  65. type FieldMap = logrus.FieldMap
  66. // Level type
  67. type Level = logrus.Level
  68. // Logger type
  69. type Logger = logrus.Logger
  70. // JSONFormatter formats logs into parsable json
  71. type JSONFormatter struct {
  72. logrus.JSONFormatter
  73. }
  74. // TextFormatter formats logs into text
  75. type TextFormatter struct {
  76. logrus.TextFormatter
  77. }
  78. // Formatter
  79. // The Formatter interface is used to implement a custom Formatter. It takes an
  80. // `Entry`. It exposes all the fields, including the default ones:
  81. //
  82. // * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
  83. // * `entry.Data["time"]`. The timestamp.
  84. // * `entry.Data["level"]. The level the entry was logged at.
  85. //
  86. // Any additional fields added with `WithField` or `WithFields` are also in
  87. // `entry.Data`. Format is expected to return an array of bytes which are then
  88. // logged to `logger.Out`.
  89. // SetLevel ...
  90. func SetLevel(level Level) {
  91. logrus.SetLevel(level)
  92. }
  93. // NewWithFields returns a logrus Entry with fields
  94. func NewWithFields(fields Fields) *Entry {
  95. return logrus.WithFields(fields)
  96. }
  97. // NewEntry return an entry is the final or intermediate Logrus logging entry
  98. func NewEntry(logger *Logger) *Entry {
  99. return logrus.NewEntry(logger)
  100. }
  101. // Exported from logrus
  102. var (
  103. // Creates a new logger. Configuration should be set by changing `Formatter`,
  104. // `Out` and `Hooks` directly on the default logger instance. You can also just
  105. // instantiate your own:
  106. //
  107. // var log = &Logger{
  108. // Out: os.Stderr,
  109. // Formatter: new(JSONFormatter),
  110. // Level: logrus.DebugLevel,
  111. // }
  112. //
  113. // It's recommended to make this a global instance called `log`.
  114. New = logrus.New
  115. // StandardLogger default logger
  116. StandardLogger = logrus.StandardLogger
  117. // SetOutput sets the standard logger output.
  118. SetOutput = logrus.SetOutput
  119. // SetFormatter sets the standard logger formatter.
  120. SetFormatter = logrus.SetFormatter
  121. // WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
  122. WithError = logrus.WithError
  123. // WithField creates an entry from the standard logger and adds a field to
  124. // it. If you want multiple fields, use `WithFields`.
  125. //
  126. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  127. // or Panic on the Entry it returns.
  128. WithField = logrus.WithField
  129. // WithFields creates an entry from the standard logger and adds multiple
  130. // fields to it. This is simply a helper for `WithField`, invoking it
  131. // once for each field.
  132. //
  133. // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
  134. // or Panic on the Entry it returns.
  135. WithFields = logrus.WithFields
  136. // Debug logs a message at level Debug on the standard logger.
  137. Debug = logrus.Debug
  138. // Print logs a message at level Info on the standard logger.
  139. Print = logrus.Print
  140. // Info logs a message at level Info on the standard logger.
  141. Info = logrus.Info
  142. // Warn logs a message at level Warn on the standard logger.
  143. Warn = logrus.Warn
  144. // Warning logs a message at level Warn on the standard logger.
  145. Warning = logrus.Warning
  146. // Error logs a message at level Error on the standard logger.
  147. Error = logrus.Error
  148. // Panic logs a message at level Panic on the standard logger.
  149. Panic = logrus.Panic
  150. // Fatal logs a message at level Fatal on the standard logger.
  151. Fatal = logrus.Fatal
  152. // Debugf logs a message at level Debug on the standard logger.
  153. Debugf = logrus.Debugf
  154. // Printf logs a message at level Info on the standard logger.
  155. Printf = logrus.Printf
  156. // Infof logs a message at level Info on the standard logger.
  157. Infof = logrus.Infof
  158. // Warnf logs a message at level Warn on the standard logger.
  159. Warnf = logrus.Warnf
  160. // Warningf logs a message at level Warn on the standard logger.
  161. Warningf = logrus.Warningf
  162. // Errorf logs a message at level Error on the standard logger.
  163. Errorf = logrus.Errorf
  164. // Panicf logs a message at level Panic on the standard logger.
  165. Panicf = logrus.Panicf
  166. // Fatalf logs a message at level Fatal on the standard logger.
  167. Fatalf = logrus.Fatalf
  168. // Debugln logs a message at level Debug on the standard logger.
  169. Debugln = logrus.Debugln
  170. // Println logs a message at level Info on the standard logger.
  171. Println = logrus.Println
  172. // Infoln logs a message at level Info on the standard logger.
  173. Infoln = logrus.Infoln
  174. // Warnln logs a message at level Warn on the standard logger.
  175. Warnln = logrus.Warnln
  176. // Warningln logs a message at level Warn on the standard logger.
  177. Warningln = logrus.Warningln
  178. // Errorln logs a message at level Error on the standard logger.
  179. Errorln = logrus.Errorln
  180. // Panicln logs a message at level Panic on the standard logger.
  181. Panicln = logrus.Panicln
  182. // Fatalln logs a message at level Fatal on the standard logger.
  183. Fatalln = logrus.Fatalln
  184. )