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.

107 lines
2.8 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "gin-vue-admin/config"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/utils"
  7. "github.com/gin-gonic/gin"
  8. rotatelogs "github.com/lestrrat/go-file-rotatelogs"
  9. oplogging "github.com/op/go-logging"
  10. "io"
  11. "os"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. logDir = "log"
  17. logSoftLink = "latest_log"
  18. module = "gin-vue-admin"
  19. )
  20. var (
  21. defaultFormatter = `%{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}`
  22. )
  23. func init() {
  24. c := global.GVA_CONFIG.Log
  25. if c.Prefix == "" {
  26. _ = fmt.Errorf("logger prefix not found")
  27. }
  28. logger := oplogging.MustGetLogger(module)
  29. var backends []oplogging.Backend
  30. registerStdout(c, &backends)
  31. if fileWriter := registerFile(c, &backends); fileWriter != nil {
  32. gin.DefaultWriter = io.MultiWriter(fileWriter, os.Stdout)
  33. }
  34. oplogging.SetBackend(backends...)
  35. global.GVA_LOG = logger
  36. }
  37. func registerStdout(c config.Log, backends *[]oplogging.Backend) {
  38. if c.Stdout != "" {
  39. level, err := oplogging.LogLevel(c.Stdout)
  40. if err != nil {
  41. fmt.Println(err)
  42. }
  43. *backends = append(*backends, createBackend(os.Stdout, c, level))
  44. }
  45. }
  46. func registerFile(c config.Log, backends *[]oplogging.Backend) io.Writer {
  47. if c.File != "" {
  48. if ok, _ := utils.PathExists(logDir); !ok {
  49. // directory not exist
  50. fmt.Println("create log directory")
  51. _ = os.Mkdir(logDir, os.ModePerm)
  52. }
  53. fileWriter, err := rotatelogs.New(
  54. logDir+string(os.PathSeparator)+"%Y-%m-%d-%H-%M.log",
  55. // generate soft link, point to latest log file
  56. rotatelogs.WithLinkName(logSoftLink),
  57. // maximum time to save log files
  58. rotatelogs.WithMaxAge(7*24*time.Hour),
  59. // time period of log file switching
  60. rotatelogs.WithRotationTime(24*time.Hour),
  61. )
  62. if err != nil {
  63. fmt.Println(err)
  64. }
  65. level, err := oplogging.LogLevel(c.File)
  66. if err != nil {
  67. fmt.Println(err)
  68. }
  69. *backends = append(*backends, createBackend(fileWriter, c, level))
  70. return fileWriter
  71. }
  72. return nil
  73. }
  74. func createBackend(w io.Writer, c config.Log, level oplogging.Level) oplogging.Backend {
  75. backend := oplogging.NewLogBackend(w, c.Prefix, 0)
  76. stdoutWriter := false
  77. if w == os.Stdout {
  78. stdoutWriter = true
  79. }
  80. format := getLogFormatter(c, stdoutWriter)
  81. backendLeveled := oplogging.AddModuleLevel(oplogging.NewBackendFormatter(backend, format))
  82. backendLeveled.SetLevel(level, module)
  83. return backendLeveled
  84. }
  85. func getLogFormatter(c config.Log, stdoutWriter bool) oplogging.Formatter {
  86. pattern := defaultFormatter
  87. if !stdoutWriter {
  88. // Color is only required for console output
  89. // Other writers don't need %{color} tag
  90. pattern = strings.Replace(pattern, "%{color:bold}", "", -1)
  91. pattern = strings.Replace(pattern, "%{color:reset}", "", -1)
  92. }
  93. if !c.LogFile {
  94. // Remove %{logfile} tag
  95. pattern = strings.Replace(pattern, "%{longfile}", "", -1)
  96. }
  97. return oplogging.MustStringFormatter(pattern)
  98. }