logs.go 3.8 KB

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