logs.go 3.4 KB

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