logs.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. "os"
  6. )
  7. var FileLog *logs.BeeLogger
  8. var FileLogData *logs.BeeLogger
  9. var FileLogCygx *logs.BeeLogger
  10. var Binlog *logs.BeeLogger
  11. func init() {
  12. FileLog = logs.NewLogger(1000000)
  13. FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_mobile.log"}`)
  14. FileLog.EnableFuncCallDepth(true)
  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. }