logs.go 1.8 KB

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