logs.go 2.8 KB

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