logs.go 3.5 KB

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