logs.go 4.0 KB

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