logs.go 2.9 KB

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