|
@@ -1,20 +1,155 @@
|
|
|
package utils
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
"os"
|
|
|
- "time"
|
|
|
+ "path"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const (
|
|
|
+ DefaultLogPath = "./cygxlogs/filelog"
|
|
|
+ DefaultBinlogPath = "./cygxlogs/binlog"
|
|
|
+ DefaultDataPath = "./cygxlogs/datalog"
|
|
|
+ DefaultApiLogPath = "./cygxlogs/apilog"
|
|
|
)
|
|
|
|
|
|
var FileLog *logs.BeeLogger
|
|
|
-var BinLog *logs.BeeLogger
|
|
|
+var ApiLog *logs.BeeLogger
|
|
|
+var FileLogData *logs.BeeLogger
|
|
|
+var Binlog *logs.BeeLogger
|
|
|
|
|
|
func init() {
|
|
|
+ if LogMaxDays == 0 {
|
|
|
+ LogMaxDays = 7
|
|
|
+ }
|
|
|
+ logPath := LogPath
|
|
|
+ if logPath == "" {
|
|
|
+ logPath = DefaultLogPath
|
|
|
+ }
|
|
|
+ logFile := LogFile
|
|
|
+ if logFile == "" {
|
|
|
+ logFile = "filelog.log"
|
|
|
+ }
|
|
|
+ os.MkdirAll(logPath, os.ModePerm)
|
|
|
+
|
|
|
+
|
|
|
+ logFileName := path.Join(logPath, logFile)
|
|
|
FileLog = logs.NewLogger(1000000)
|
|
|
- FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/hongze_clpt.log"}`)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ FileLog.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ FileLog.EnableFuncCallDepth(true)
|
|
|
+
|
|
|
+ initBinlog()
|
|
|
+ initApiLog()
|
|
|
+ initFileLogData()
|
|
|
+}
|
|
|
+
|
|
|
+type logConfig struct {
|
|
|
+ FileName string `json:"filename" description:"保存的文件名"`
|
|
|
+ MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
|
|
|
+ MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
|
|
|
+ Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
|
|
|
+ MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
|
|
|
+ Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
|
|
|
+ Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
|
|
|
+ Color bool `json:"color" description:"日志是否输出颜色"`
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func initBinlog() {
|
|
|
+
|
|
|
+
|
|
|
+ binlogPath := BinLogPath
|
|
|
+ if binlogPath == "" {
|
|
|
+ binlogPath = DefaultBinlogPath
|
|
|
+ }
|
|
|
+ binlogFile := BinLogFile
|
|
|
+ if binlogFile == "" {
|
|
|
+ binlogFile = "binlog.log"
|
|
|
+ }
|
|
|
+ os.MkdirAll(binlogPath, os.ModePerm)
|
|
|
+ logFileName := path.Join(binlogPath, binlogFile)
|
|
|
+ Binlog = logs.NewLogger(1000000)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+
|
|
|
+
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ Binlog.EnableFuncCallDepth(true)
|
|
|
+}
|
|
|
+
|
|
|
+func initApiLog() {
|
|
|
+ logPath := ApiLogPath
|
|
|
+ if logPath == "" {
|
|
|
+ logPath = DefaultApiLogPath
|
|
|
+ }
|
|
|
+ logFile := ApiLogFile
|
|
|
+ if logFile == "" {
|
|
|
+ logFile = "apilog.log"
|
|
|
+ }
|
|
|
+ os.MkdirAll(logPath, os.ModePerm)
|
|
|
+
|
|
|
+
|
|
|
+ logFileName := path.Join(logPath, logFile)
|
|
|
+ ApiLog = logs.NewLogger(1000000)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ ApiLog.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ ApiLog.EnableFuncCallDepth(true)
|
|
|
+}
|
|
|
+
|
|
|
+func initFileLogData() {
|
|
|
+ logPath := LogDataPath
|
|
|
+ if logPath == "" {
|
|
|
+ logPath = DefaultDataPath
|
|
|
+ }
|
|
|
+ logFile := LogDataFile
|
|
|
+ if logFile == "" {
|
|
|
+ logFile = "datalog.log"
|
|
|
+ }
|
|
|
+ os.MkdirAll(logPath, os.ModePerm)
|
|
|
+
|
|
|
+
|
|
|
+ logFileName := path.Join(logPath, logFile)
|
|
|
+ FileLogData = logs.NewLogger(1000000)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ FileLogData.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ FileLogData.EnableFuncCallDepth(true)
|
|
|
+}
|
|
|
|
|
|
- binLogDir := `./binlog`
|
|
|
- os.MkdirAll(binLogDir, os.ModePerm)
|
|
|
- BinLog = logs.NewLogger(1000000)
|
|
|
- BinLog.SetLogger(logs.AdapterMultiFile, `{"filename":"./binlog/binlog.`+time.Now().Format(FormatDate)+`.log","maxdays":30}`)
|
|
|
+func getDefaultLogConfig() logConfig {
|
|
|
+ return logConfig{
|
|
|
+ FileName: "",
|
|
|
+ MaxLines: 10000000,
|
|
|
+ MaxSize: 1 << 28,
|
|
|
+ Daily: true,
|
|
|
+ MaxDays: LogMaxDays,
|
|
|
+ Rotate: true,
|
|
|
+ Level: logs.LevelTrace,
|
|
|
+
|
|
|
+ }
|
|
|
}
|