log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "time"
  10. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  11. "github.com/sirupsen/logrus"
  12. )
  13. func init() {
  14. logrus.SetFormatter(&DFormatter{})
  15. logrus.SetLevel(DebugLevel)
  16. logrus.SetOutput(os.Stdout)
  17. }
  18. // type PanicLevel = logrus.PanicLevel
  19. const (
  20. PanicLevel logrus.Level = logrus.PanicLevel
  21. FatalLevel logrus.Level = logrus.FatalLevel
  22. ErrorLevel logrus.Level = logrus.ErrorLevel
  23. WarnLevel logrus.Level = logrus.WarnLevel
  24. InfoLevel logrus.Level = logrus.InfoLevel
  25. DebugLevel logrus.Level = logrus.DebugLevel
  26. )
  27. var (
  28. fileConn *rotatelogs.RotateLogs
  29. )
  30. type LogConfig struct {
  31. Filename string // 日志文件
  32. RetainFileCount uint // 保留日志文件数量
  33. IsDevelop bool // 开发模式会输出日志到标准输出
  34. FileSplitTime time.Duration // 日志文件拆分时间间隔 time.Hour*24
  35. }
  36. func WithField(key string, value string) *logrus.Entry {
  37. return logrus.WithField(key, value)
  38. }
  39. // config need to be correct JSON as string: {"filename":"default.log","maxsize":100}
  40. func SetLogger(config LogConfig) *logrus.Logger {
  41. /* 日志轮转相关函数
  42. `WithLinkName` 为最新的日志建立软连接
  43. `WithRotationTime` 设置日志分割的时间,隔多久分割一次
  44. WithMaxAge 和 WithRotationCount二者只能设置一个
  45. `WithMaxAge` 设置文件清理前的最长保存时间
  46. `WithRotationCount` 设置文件清理前最多保存的个数
  47. */
  48. //fileName := config.Filename + ".%Y%m%d"
  49. fileName := ""
  50. if config.FileSplitTime > time.Hour {
  51. fileName = fmt.Sprintf(config.Filename, "%Y%m%d")
  52. } else if config.FileSplitTime > time.Minute*30 {
  53. fileName = fmt.Sprintf(config.Filename, "%Y%m%d_%H")
  54. } else {
  55. fileName = fmt.Sprintf(config.Filename, "%Y%m%d_%H_%M")
  56. }
  57. fileConn, _ := rotatelogs.New(
  58. fileName,
  59. //rotatelogs.WithLinkName(config.Filename),
  60. rotatelogs.WithRotationCount(config.RetainFileCount),
  61. rotatelogs.WithRotationTime(config.FileSplitTime),
  62. )
  63. if config.IsDevelop {
  64. fmt.Println("isDeubg=true; log out console")
  65. logrus.SetLevel(DebugLevel)
  66. logrus.SetOutput(os.Stdout)
  67. } else {
  68. fmt.Println("isDeubg=false; log out file")
  69. logrus.SetOutput(fileConn)
  70. }
  71. return logrus.StandardLogger()
  72. }
  73. func GetFileConn() *rotatelogs.RotateLogs {
  74. return fileConn
  75. }
  76. func SetLevel(logLevel logrus.Level) {
  77. logrus.SetLevel(logLevel)
  78. }
  79. type DFormatter struct {
  80. TimestampFormat string
  81. }
  82. func (f *DFormatter) Format(entry *logrus.Entry) ([]byte, error) {
  83. timestampFormat := f.TimestampFormat
  84. if timestampFormat == "" {
  85. timestampFormat = "2006/01/02 15:04:05"
  86. }
  87. _, file, line, ok := runtime.Caller(9)
  88. if !ok {
  89. file = "???"
  90. line = 0
  91. }
  92. file = filepath.Base(file) // 只取文件路径的文件名
  93. // _, filename := path.Split(file)
  94. msg := entry.Time.Format(timestampFormat) +
  95. " " + strings.ToUpper(entry.Level.String()) +
  96. " [" + file + ":" + strconv.Itoa(line) + "] " +
  97. entry.Message + "\n"
  98. return []byte(msg), nil
  99. }
  100. func Debugf(format string, args ...interface{}) {
  101. logrus.Debugf(format, args...)
  102. }
  103. func Infof(format string, args ...interface{}) {
  104. logrus.Infof(format, args...)
  105. }
  106. func Warnf(format string, args ...interface{}) {
  107. logrus.Warnf(format, args...)
  108. }
  109. func Errorf(format string, args ...interface{}) {
  110. logrus.Errorf(format, args...)
  111. }
  112. func Fatalf(format string, args ...interface{}) {
  113. logrus.Fatalf(format, args...)
  114. }
  115. func Panicf(format string, args ...interface{}) {
  116. logrus.Panicf(format, args...)
  117. }
  118. // 以下函数不可以写成这种形式
  119. // func Debug(args ...interface{}) {
  120. // logrus.Debug(args...)
  121. // }
  122. func Debug(args ...interface{}) {
  123. debug(args...)
  124. }
  125. func debug(args ...interface{}) {
  126. logrus.Debug(args...)
  127. }
  128. func Println(args ...interface{}) {
  129. println(args...)
  130. }
  131. func println(args ...interface{}) {
  132. logrus.Println(args...)
  133. }
  134. func Info(args ...interface{}) {
  135. info(args...)
  136. }
  137. func info(args ...interface{}) {
  138. logrus.Info(args...)
  139. }
  140. func Warn(args ...interface{}) {
  141. warn(args...)
  142. }
  143. func warn(args ...interface{}) {
  144. logrus.Warn(args...)
  145. }
  146. func Error(args ...interface{}) {
  147. ferror(args...)
  148. }
  149. func ferror(args ...interface{}) {
  150. logrus.Error(args...)
  151. }
  152. func Fatal(args ...interface{}) {
  153. fatal(args...)
  154. }
  155. func fatal(args ...interface{}) {
  156. logrus.Fatal(args...)
  157. }
  158. func Panic(args ...interface{}) {
  159. panic(args...)
  160. }
  161. func panic(args ...interface{}) {
  162. logrus.Panic(args...)
  163. }