logs.go 1.8 KB

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