logs.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. DefaultDataPath = "./etalogs/datalog"
  12. DefaultApiLogPath = "./etalogs/apilog"
  13. )
  14. var FileLog *logs.BeeLogger
  15. var ApiLog *logs.BeeLogger
  16. var FileLogData *logs.BeeLogger
  17. var Binlog *logs.BeeLogger
  18. var MongoLog *logs.BeeLogger
  19. func init() {
  20. if LogMaxDays == 0 {
  21. LogMaxDays = 30
  22. }
  23. logPath := LogPath
  24. if logPath == "" {
  25. logPath = DefaultLogPath
  26. }
  27. logFile := LogFile
  28. if logFile == "" {
  29. logFile = "filelog.log"
  30. }
  31. os.MkdirAll(logPath, os.ModePerm)
  32. // 打开文件
  33. logFileName := path.Join(logPath, logFile)
  34. FileLog = logs.NewLogger(1000000)
  35. logConf := getDefaultLogConfig()
  36. logConf.FileName = logFileName
  37. b, _ := json.Marshal(logConf)
  38. FileLog.SetLogger(logs.AdapterFile, string(b))
  39. FileLog.EnableFuncCallDepth(true)
  40. initBinlog()
  41. initApiLog()
  42. initFileLogData()
  43. initMgoLog()
  44. }
  45. type logConfig struct {
  46. FileName string `json:"filename" description:"保存的文件名"`
  47. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  48. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  49. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  50. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  51. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  52. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  53. Color bool `json:"color" description:"日志是否输出颜色"`
  54. //Perm string `json:"perm" description:"日志文件权限"`
  55. }
  56. func initBinlog() {
  57. //binlog日志
  58. //binlog日志
  59. binlogPath := BinLogPath
  60. if binlogPath == "" {
  61. binlogPath = DefaultBinlogPath
  62. }
  63. binlogFile := BinLogFile
  64. if binlogFile == "" {
  65. binlogFile = "binlog.log"
  66. }
  67. os.MkdirAll(binlogPath, os.ModePerm)
  68. logFileName := path.Join(binlogPath, binlogFile)
  69. Binlog = logs.NewLogger(1000000)
  70. logConf := getDefaultLogConfig()
  71. logConf.FileName = logFileName
  72. //logConf.MaxLines = 10000000
  73. //logConf.Rotate = true
  74. b, _ := json.Marshal(logConf)
  75. Binlog.SetLogger(logs.AdapterFile, string(b))
  76. Binlog.EnableFuncCallDepth(true)
  77. }
  78. func initApiLog() {
  79. logPath := ApiLogPath
  80. if logPath == "" {
  81. logPath = DefaultApiLogPath
  82. }
  83. logFile := ApiLogFile
  84. if logFile == "" {
  85. logFile = "apilog.log"
  86. }
  87. os.MkdirAll(logPath, os.ModePerm)
  88. // 打开文件
  89. logFileName := path.Join(logPath, logFile)
  90. ApiLog = logs.NewLogger(1000000)
  91. logConf := getDefaultLogConfig()
  92. logConf.FileName = logFileName
  93. b, _ := json.Marshal(logConf)
  94. ApiLog.SetLogger(logs.AdapterFile, string(b))
  95. ApiLog.EnableFuncCallDepth(true)
  96. }
  97. func initFileLogData() {
  98. logPath := LogDataPath
  99. if logPath == "" {
  100. logPath = DefaultDataPath
  101. }
  102. logFile := LogDataFile
  103. if logFile == "" {
  104. logFile = "datalog.log"
  105. }
  106. os.MkdirAll(logPath, os.ModePerm)
  107. // 打开文件
  108. logFileName := path.Join(logPath, logFile)
  109. FileLogData = logs.NewLogger(1000000)
  110. logConf := getDefaultLogConfig()
  111. logConf.FileName = logFileName
  112. b, _ := json.Marshal(logConf)
  113. FileLogData.SetLogger(logs.AdapterFile, string(b))
  114. FileLogData.EnableFuncCallDepth(true)
  115. }
  116. // initMgoLog
  117. // @Description: mongo日志
  118. // @author: Roc
  119. // @datetime 2024-05-06 16:42:47
  120. func initMgoLog() {
  121. //mgo日志
  122. var logPath string
  123. {
  124. binlogPath := BinLogPath
  125. if binlogPath == "" {
  126. binlogPath = DefaultBinlogPath
  127. }
  128. logPath = path.Dir(binlogPath)
  129. }
  130. if logPath == `` {
  131. logPath = "./etalogs"
  132. }
  133. mongoLogPath := logPath + "/mongolog"
  134. mongoLogFile := "mongolog.log"
  135. os.MkdirAll(mongoLogPath, os.ModePerm)
  136. logFileName := path.Join(mongoLogPath, mongoLogFile)
  137. MongoLog = logs.NewLogger(1000000)
  138. logConf := getDefaultLogConfig()
  139. logConf.FileName = logFileName
  140. //logConf.MaxLines = 10000000
  141. //logConf.Rotate = true
  142. b, _ := json.Marshal(logConf)
  143. MongoLog.SetLogger(logs.AdapterFile, string(b))
  144. MongoLog.EnableFuncCallDepth(true)
  145. }
  146. func getDefaultLogConfig() logConfig {
  147. return logConfig{
  148. FileName: "",
  149. MaxLines: 10000000,
  150. MaxSize: 1 << 28,
  151. Daily: true,
  152. MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  153. Rotate: true,
  154. Level: logs.LevelTrace,
  155. //Perm: "",
  156. }
  157. }