|
@@ -1,18 +1,25 @@
|
|
|
package utils
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/beego/beego/v2/core/logs"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
|
"os"
|
|
|
"path"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- DefaultLogPath = "./rdlucklog"
|
|
|
+ DefaultLogPath = "./rdlucklog/filelog"
|
|
|
+ DefaultBinlogPath = "./rdlucklog/binlog"
|
|
|
+ DefaultApiLogPath = "./rdlucklog/apilog"
|
|
|
)
|
|
|
|
|
|
var FileLog = logrus.New()
|
|
|
var ApiLog = logrus.New()
|
|
|
+var Binlog *logs.BeeLogger
|
|
|
|
|
|
func init() {
|
|
|
logPath := LogPath
|
|
@@ -21,14 +28,16 @@ func init() {
|
|
|
}
|
|
|
logFile := LogFile
|
|
|
if logFile == "" {
|
|
|
- logFile = "eta_pub.log"
|
|
|
+ logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
}
|
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
+ logConf := getDefaultLogrusConfig(logFileName)
|
|
|
+ logConf.MaxAge = 1
|
|
|
// 使用滚动压缩方式记录日志
|
|
|
- rolling(FileLog, logFileName)
|
|
|
+ rolling(FileLog, logConf)
|
|
|
//rolling(bLogFileName)
|
|
|
// 设置日志输出JSON格式
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
@@ -40,29 +49,64 @@ func init() {
|
|
|
// 设置日志记录级别
|
|
|
//FileLog.SetLevel(logrus.DebugLevel)
|
|
|
|
|
|
+ //初始化binlog日志
|
|
|
//FileLog.Info("abc")
|
|
|
+ initBinlog()
|
|
|
initApiLog()
|
|
|
}
|
|
|
|
|
|
+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:"日志是否输出颜色"`
|
|
|
+ //Perm string `json:"perm" description:"日志文件权限"`
|
|
|
+}
|
|
|
+
|
|
|
+func initBinlog() {
|
|
|
+ //binlog日志
|
|
|
+ //binlog日志
|
|
|
+ binlogPath := BinLogPath
|
|
|
+ if binlogPath == "" {
|
|
|
+ binlogPath = DefaultBinlogPath
|
|
|
+ }
|
|
|
+ binlogFile := BinLogFile
|
|
|
+ if binlogFile == "" {
|
|
|
+ binlogFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
+ }
|
|
|
+ os.MkdirAll(binlogPath, os.ModePerm)
|
|
|
+ logFileName := path.Join(binlogPath, binlogFile)
|
|
|
+ Binlog = logs.NewLogger(1000000)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+ logConf.MaxLines = 10000000
|
|
|
+ logConf.Rotate = true
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ Binlog.EnableFuncCallDepth(true)
|
|
|
+}
|
|
|
+
|
|
|
func initApiLog() {
|
|
|
logPath := ApiLogPath
|
|
|
if logPath == "" {
|
|
|
- if RunMode == "release" {
|
|
|
- logPath = `/data/etalogs/eta_pub`
|
|
|
- } else {
|
|
|
- logPath = `./rdlucklog/api`
|
|
|
- }
|
|
|
+ logPath = DefaultApiLogPath
|
|
|
}
|
|
|
logFile := ApiLogFile
|
|
|
if logFile == "" {
|
|
|
- logFile = "eta_pub_api.log"
|
|
|
+ logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
}
|
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
+ logConf := getDefaultLogrusConfig(logFileName)
|
|
|
// 使用滚动压缩方式记录日志
|
|
|
- rolling(ApiLog, logFileName)
|
|
|
+ rolling(ApiLog, logConf)
|
|
|
//rolling(bLogFileName)
|
|
|
// 设置日志输出JSON格式
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
@@ -72,14 +116,32 @@ func initApiLog() {
|
|
|
}
|
|
|
|
|
|
// 日志滚动设置
|
|
|
-func rolling(fLog *logrus.Logger, logFile string) {
|
|
|
+func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
|
|
|
// 设置输出
|
|
|
- fLog.SetOutput(&lumberjack.Logger{
|
|
|
+ fLog.SetOutput(config)
|
|
|
+}
|
|
|
+
|
|
|
+func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
|
|
|
+ config = &lumberjack.Logger{
|
|
|
Filename: logFile, //日志文件位置
|
|
|
- MaxSize: 100, // 单文件最大容量,单位是MB
|
|
|
+ MaxSize: 256, // 单文件最大容量,单位是MB
|
|
|
MaxBackups: 3, // 最大保留过期文件个数
|
|
|
MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
|
|
|
Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
|
|
|
LocalTime: true,
|
|
|
- })
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getDefaultLogConfig() logConfig {
|
|
|
+ return logConfig{
|
|
|
+ FileName: "",
|
|
|
+ MaxLines: 0,
|
|
|
+ MaxSize: 1 << 28,
|
|
|
+ Daily: true,
|
|
|
+ MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
|
+ Rotate: true,
|
|
|
+ Level: logs.LevelTrace,
|
|
|
+ //Perm: "",
|
|
|
+ }
|
|
|
}
|