logs.go 2.1 KB

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