logs.go 4.0 KB

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