logs.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/adapter/logs"
  5. "os"
  6. )
  7. var FileLog *logs.BeeLogger
  8. var FileLogLz *logs.BeeLogger
  9. var Binlog *logs.BeeLogger
  10. func init() {
  11. FileLog = logs.NewLogger(1000000)
  12. FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/hongze_task.log"}`)
  13. FileLogLz = logs.NewLogger(1000000)
  14. FileLogLz.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/hongze_task_lz.log"}`)
  15. //初始化binlog日志
  16. initBinlog()
  17. }
  18. func initBinlog() {
  19. //binlog日志
  20. binLogDir := `./binlog`
  21. os.MkdirAll(binLogDir, os.ModePerm)
  22. Binlog = logs.NewLogger(1000000)
  23. logConfig := getDefaultLogConfig()
  24. logConfig.FileName = "./binlog/binlog.log"
  25. logConfig.MaxLines = 10000000
  26. logConfig.Rotate = true
  27. b, _ := json.Marshal(logConfig)
  28. Binlog.SetLogger(logs.AdapterFile, string(b))
  29. Binlog.EnableFuncCallDepth(true)
  30. }
  31. type logConfig struct {
  32. FileName string `json:"filename" description:"保存的文件名"`
  33. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  34. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  35. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  36. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  37. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  38. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  39. Color bool `json:"color" description:"日志是否输出颜色"`
  40. //Perm string `json:"perm" description:"日志文件权限"`
  41. }
  42. func getDefaultLogConfig() logConfig {
  43. return logConfig{
  44. FileName: "",
  45. MaxLines: 0,
  46. MaxSize: 1 << 28,
  47. Daily: true,
  48. MaxDays: 31, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  49. Rotate: true,
  50. Level: logs.LevelTrace,
  51. //Perm: "",
  52. }
  53. }