logs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. "os"
  6. "path"
  7. )
  8. const (
  9. DefaultLogPath = "./etalogs/filelog"
  10. DefaultBinlogPath = "./etalogs/binlog"
  11. DefaultApiLogPath = "./etalogs/apilog"
  12. )
  13. var FileLog *logs.BeeLogger
  14. var ApiLog *logs.BeeLogger
  15. var Binlog *logs.BeeLogger
  16. func init() {
  17. if LogMaxDays == 0 {
  18. LogMaxDays = 30
  19. }
  20. logPath := LogPath
  21. if logPath == "" {
  22. logPath = DefaultLogPath
  23. }
  24. logFile := LogFile
  25. if logFile == "" {
  26. logFile = "filelog.log"
  27. }
  28. os.MkdirAll(logPath, os.ModePerm)
  29. // 打开文件
  30. logFileName := path.Join(logPath, logFile)
  31. FileLog = logs.NewLogger(1000000)
  32. logConf := getDefaultLogConfig()
  33. logConf.FileName = logFileName
  34. b, _ := json.Marshal(logConf)
  35. FileLog.SetLogger(logs.AdapterFile, string(b))
  36. FileLog.EnableFuncCallDepth(true)
  37. initBinlog()
  38. initApiLog()
  39. }
  40. type logConfig struct {
  41. FileName string `json:"filename" description:"保存的文件名"`
  42. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  43. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  44. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  45. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  46. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  47. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  48. Color bool `json:"color" description:"日志是否输出颜色"`
  49. //Perm string `json:"perm" description:"日志文件权限"`
  50. }
  51. func initBinlog() {
  52. //binlog日志
  53. //binlog日志
  54. binlogPath := BinLogPath
  55. if binlogPath == "" {
  56. binlogPath = DefaultBinlogPath
  57. }
  58. binlogFile := BinLogFile
  59. if binlogFile == "" {
  60. binlogFile = "binlog.log"
  61. }
  62. os.MkdirAll(binlogPath, os.ModePerm)
  63. logFileName := path.Join(binlogPath, binlogFile)
  64. Binlog = logs.NewLogger(1000000)
  65. logConf := getDefaultLogConfig()
  66. logConf.FileName = logFileName
  67. //logConf.MaxLines = 10000000
  68. //logConf.Rotate = true
  69. b, _ := json.Marshal(logConf)
  70. Binlog.SetLogger(logs.AdapterFile, string(b))
  71. Binlog.EnableFuncCallDepth(true)
  72. }
  73. func initApiLog() {
  74. logPath := ApiLogPath
  75. if logPath == "" {
  76. logPath = DefaultApiLogPath
  77. }
  78. logFile := ApiLogFile
  79. if logFile == "" {
  80. logFile = "apilog.log"
  81. }
  82. os.MkdirAll(logPath, os.ModePerm)
  83. // 打开文件
  84. logFileName := path.Join(logPath, logFile)
  85. ApiLog = logs.NewLogger(1000000)
  86. logConf := getDefaultLogConfig()
  87. logConf.FileName = logFileName
  88. b, _ := json.Marshal(logConf)
  89. ApiLog.SetLogger(logs.AdapterFile, string(b))
  90. ApiLog.EnableFuncCallDepth(true)
  91. }
  92. func getDefaultLogConfig() logConfig {
  93. return logConfig{
  94. FileName: "",
  95. MaxLines: 10000000,
  96. MaxSize: 1 << 28,
  97. Daily: true,
  98. MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  99. Rotate: true,
  100. Level: logs.LevelTrace,
  101. //Perm: "",
  102. }
  103. }