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. 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 = "latest_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. fileWriter, err := rotatelogs.New(
  52. logDir+string(os.PathSeparator)+"%Y-%m-%d-%H-%M.log",
  53. // generate soft link, point to latest log file
  54. rotatelogs.WithLinkName(logSoftLink),
  55. // maximum time to save log files
  56. rotatelogs.WithMaxAge(7*24*time.Hour),
  57. // time period of log file switching
  58. rotatelogs.WithRotationTime(24*time.Hour),
  59. )
  60. if err != nil {
  61. fmt.Println(err)
  62. return backends
  63. }
  64. level, err := oplogging.LogLevel(c.File)
  65. if err != nil {
  66. fmt.Println(err)
  67. }
  68. backends = append(backends, createBackend(fileWriter, c, level))
  69. }
  70. return backends
  71. }
  72. func createBackend(w io.Writer, c config.Log, level oplogging.Level) oplogging.Backend {
  73. backend := oplogging.NewLogBackend(w, c.Prefix, 0)
  74. stdoutWriter := false
  75. if w == os.Stdout {
  76. stdoutWriter = true
  77. }
  78. format := getLogFormatter(c, stdoutWriter)
  79. backendLeveled := oplogging.AddModuleLevel(oplogging.NewBackendFormatter(backend, format))
  80. backendLeveled.SetLevel(level, module)
  81. return backendLeveled
  82. }
  83. func getLogFormatter(c config.Log, stdoutWriter bool) oplogging.Formatter {
  84. pattern := defaultFormatter
  85. if !stdoutWriter {
  86. // Color is only required for console output
  87. // Other writers don't need %{color} tag
  88. pattern = strings.Replace(pattern, "%{color:bold}", "", -1)
  89. pattern = strings.Replace(pattern, "%{color:reset}", "", -1)
  90. }
  91. if !c.LogFile {
  92. // Remove %{logfile} tag
  93. pattern = strings.Replace(pattern, "%{longfile}", "", -1)
  94. }
  95. return oplogging.MustStringFormatter(pattern)
  96. }