log.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package logger
  2. import (
  3. // "path"
  4. "fmt"
  5. "net/http"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "time"
  10. // "time"
  11. "github.com/gin-gonic/gin"
  12. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  13. "github.com/rifflock/lfshook"
  14. "github.com/sirupsen/logrus"
  15. )
  16. const filename = "./logs/console"
  17. func init() {
  18. logrus.SetFormatter(&DFormatter{})
  19. writer, _ := rotatelogs.New(
  20. filename+".%Y%m%d.log",
  21. // rotatelogs.WithLinkName(filename),
  22. rotatelogs.WithRotationCount(50),
  23. rotatelogs.WithRotationTime(time.Hour*24),
  24. )
  25. logrus.AddHook(lfshook.NewHook(
  26. lfshook.WriterMap{
  27. logrus.DebugLevel: writer,
  28. logrus.InfoLevel: writer,
  29. logrus.WarnLevel: writer,
  30. logrus.ErrorLevel: writer,
  31. logrus.FatalLevel: writer,
  32. logrus.PanicLevel: writer,
  33. }, &logrus.TextFormatter{}),
  34. )
  35. // logrus.SetOutput(writer)
  36. // logrus.SetLevel(logrus.InfoLevel)
  37. }
  38. type DFormatter struct {
  39. TimestampFormat string
  40. }
  41. func (f *DFormatter) Format(entry *logrus.Entry) ([]byte, error) {
  42. timestampFormat := f.TimestampFormat
  43. if timestampFormat == "" {
  44. timestampFormat = "2006/01/02 15:04:05"
  45. }
  46. _, file, line, ok := runtime.Caller(9)
  47. if !ok {
  48. file = "???"
  49. line = 0
  50. }
  51. // _, filename := path.Split(file)
  52. msg := entry.Time.Format(timestampFormat) +
  53. " " + strings.ToUpper(entry.Level.String()) +
  54. " [" + file + ":" + strconv.Itoa(line) + "] " +
  55. entry.Message + "\n"
  56. return []byte(msg), nil
  57. }
  58. func Debugf(format string, args ...interface{}) {
  59. logrus.Debugf(format, args...)
  60. }
  61. func LogErr(err error, format string, args ...interface{}) bool {
  62. if err != nil {
  63. args = append(args, err.Error())
  64. logrus.Infof(format+" %s", args...)
  65. return false
  66. }
  67. return true
  68. }
  69. func Infof(format string, args ...interface{}) {
  70. logrus.Infof(format, args...)
  71. }
  72. func Warnf(format string, args ...interface{}) {
  73. logrus.Warnf(format, args...)
  74. }
  75. func Errorf(format string, args ...interface{}) {
  76. logrus.Errorf(format, args...)
  77. }
  78. func Fatalf(format string, args ...interface{}) {
  79. logrus.Fatalf(format, args...)
  80. }
  81. func Panicf(format string, args ...interface{}) {
  82. logrus.Panicf(format, args...)
  83. }
  84. func Debug(args ...interface{}) {
  85. logrus.Debug(args...)
  86. }
  87. func Info(args ...interface{}) {
  88. logrus.Info(args...)
  89. }
  90. func Warn(args ...interface{}) {
  91. logrus.Warn(args...)
  92. }
  93. func Error(args ...interface{}) {
  94. logrus.Error(args...)
  95. }
  96. func Fatal(args ...interface{}) {
  97. logrus.Fatal(args...)
  98. }
  99. func Panic(args ...interface{}) {
  100. logrus.Panic(args...)
  101. }
  102. func SetGinLogger() gin.LoggerConfig {
  103. var c gin.LoggerConfig
  104. c.Output = gin.DefaultWriter
  105. c.Formatter = func(params gin.LogFormatterParams) string {
  106. userName := GetStr("userId", params.Keys, "")
  107. var formatStr string
  108. if params.StatusCode == http.StatusOK {
  109. formatStr = fmt.Sprintf("%s %-15s %-16s %-4s %s %d %s %s\n",
  110. params.TimeStamp.Format("2006-01-02 15:04:05"),
  111. params.ClientIP,
  112. userName,
  113. params.Method,
  114. params.Path,
  115. params.StatusCode,
  116. params.Latency,
  117. params.ErrorMessage,
  118. )
  119. } else {
  120. formatStr = fmt.Sprintf("\x1b[93m%s %-15s %-16s %-4s %s %d %s %s\n\x1b[0m",
  121. params.TimeStamp.Format("2006-01-02 15:04:05"),
  122. params.ClientIP,
  123. userName,
  124. params.Method,
  125. params.Path,
  126. params.StatusCode,
  127. params.Latency,
  128. params.ErrorMessage,
  129. )
  130. }
  131. // logrus.Infof(" %-15s %-16s %-4s %s %d %s %s", params.ClientIP, userName,
  132. // params.Method,
  133. // params.Path,
  134. // params.StatusCode,
  135. // params.Latency,
  136. // params.ErrorMessage,
  137. // )
  138. return formatStr
  139. }
  140. return c
  141. }
  142. func GetStr(key string, obj map[string]interface{}, def string) string {
  143. if val, flag := obj[key]; flag {
  144. if sVal, flag2 := val.(string); flag2 {
  145. return sVal
  146. }
  147. if sVal, flag2 := val.(int); flag2 {
  148. return strconv.Itoa(sVal)
  149. }
  150. }
  151. return def
  152. }