logs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. 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. // initMgoLog
  93. // @Description: mongo日志
  94. // @author: Roc
  95. // @datetime 2024-05-06 16:42:47
  96. func initMgoLog() {
  97. //mgo日志
  98. var logPath string
  99. {
  100. binlogPath := BinLogPath
  101. if binlogPath == "" {
  102. binlogPath = DefaultBinlogPath
  103. }
  104. logPath = path.Dir(binlogPath)
  105. }
  106. if logPath == `` {
  107. logPath = "./etalogs"
  108. }
  109. mongoLogPath := logPath + "/mongolog"
  110. mongoLogFile := "mongolog.log"
  111. os.MkdirAll(mongoLogPath, os.ModePerm)
  112. logFileName := path.Join(mongoLogPath, mongoLogFile)
  113. MongoLog = logs.NewLogger(1000000)
  114. logConf := getDefaultLogConfig()
  115. logConf.FileName = logFileName
  116. //logConf.MaxLines = 10000000
  117. //logConf.Rotate = true
  118. b, _ := json.Marshal(logConf)
  119. MongoLog.SetLogger(logs.AdapterFile, string(b))
  120. MongoLog.EnableFuncCallDepth(true)
  121. }
  122. func getDefaultLogConfig() logConfig {
  123. return logConfig{
  124. FileName: "",
  125. MaxLines: 10000000,
  126. MaxSize: 1 << 28,
  127. Daily: true,
  128. MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  129. Rotate: true,
  130. Level: logs.LevelTrace,
  131. //Perm: "",
  132. }
  133. }