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.

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