logs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package utils
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path"
  6. "github.com/beego/beego/v2/core/logs"
  7. )
  8. const (
  9. DefaultBinlogPath = "./etalogs/binlog"
  10. DefaultApiLogPath = "./etalogs/apilog"
  11. DefaultLogPath = "./etalogs/filelog"
  12. )
  13. var (
  14. BinLog *logs.BeeLogger
  15. ApiLog *logs.BeeLogger
  16. FileLog *logs.BeeLogger
  17. )
  18. func init() {
  19. if LogMaxDays == 0 {
  20. LogMaxDays = 30
  21. }
  22. logPath := LogPath
  23. if logPath == "" {
  24. logPath = DefaultLogPath
  25. }
  26. logFile := LogFile
  27. if logFile == "" {
  28. logFile = "filelog.log"
  29. }
  30. os.MkdirAll(logPath, os.ModePerm)
  31. // 打开文件
  32. logFileName := path.Join(logPath, logFile)
  33. FileLog = logs.NewLogger(1000000)
  34. logConf := getDefaultLogConfig()
  35. logConf.FileName = logFileName
  36. b, _ := json.Marshal(logConf)
  37. FileLog.SetLogger(logs.AdapterFile, string(b))
  38. FileLog.EnableFuncCallDepth(true)
  39. initApiLog()
  40. initBinLog()
  41. }
  42. // 初始化apilog日志
  43. func initApiLog() {
  44. apilogPath := ApiLogPath
  45. if apilogPath == "" {
  46. apilogPath = DefaultApiLogPath
  47. }
  48. apilogFile := ApiLogFile
  49. if apilogFile == "" {
  50. apilogFile = "apilog.log"
  51. }
  52. os.MkdirAll(apilogPath, os.ModePerm)
  53. logFileName := path.Join(apilogPath, apilogFile)
  54. ApiLog = logs.NewLogger(1000000)
  55. logConf := getDefaultLogConfig()
  56. logConf.FileName = logFileName
  57. b, _ := json.Marshal(logConf)
  58. ApiLog.SetLogger(logs.AdapterFile, string(b))
  59. ApiLog.EnableFuncCallDepth(true)
  60. }
  61. // 初始化binlog日志
  62. func initBinLog() {
  63. binlogPath := BinLogPath
  64. if binlogPath == "" {
  65. binlogPath = DefaultBinlogPath
  66. }
  67. binlogFile := BinLogFile
  68. if binlogFile == "" {
  69. binlogFile = "binlog.log"
  70. }
  71. os.MkdirAll(binlogPath, os.ModePerm)
  72. logFileName := path.Join(binlogPath, binlogFile)
  73. BinLog = logs.NewLogger(1000000)
  74. logConf := getDefaultLogConfig()
  75. logConf.FileName = logFileName
  76. b, _ := json.Marshal(logConf)
  77. BinLog.SetLogger(logs.AdapterFile, string(b))
  78. BinLog.EnableFuncCallDepth(true)
  79. }
  80. type logConfig struct {
  81. FileName string `json:"filename" description:"保存的文件名"`
  82. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  83. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  84. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  85. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  86. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  87. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  88. Color bool `json:"color" description:"日志是否输出颜色"`
  89. }
  90. func getDefaultLogConfig() logConfig {
  91. return logConfig{
  92. FileName: "",
  93. MaxLines: 10000000,
  94. MaxSize: 1 << 28,
  95. Daily: true,
  96. MaxDays: LogMaxDays,
  97. Rotate: true,
  98. Level: logs.LevelTrace,
  99. //Perm: "",
  100. }
  101. }