logs.go 2.3 KB

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