123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package core
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- rotatelogs "github.com/lestrrat-go/file-rotatelogs"
- oplogging "github.com/op/go-logging"
- "hongze/hongze_yb/config"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/utils"
- "io"
- "os"
- "strings"
- "time"
- )
- const (
- Module = "hongze_yb"
- )
- var (
- defaultFormatter = ` %{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}`
- )
- func init() {
- logConfig := global.CONFIG.Log
- logger := oplogging.MustGetLogger(Module)
- var backends []oplogging.Backend
-
- registerStdout(logConfig, &backends)
-
- fileWriter := registerFile(logConfig, &backends, logConfig.LogDirPath, logConfig.LogSoftLink)
- if fileWriter != nil {
- if global.CONFIG.Serve.RunMode == "debug" {
- gin.DefaultWriter = io.MultiWriter(fileWriter, os.Stdout)
- } else {
- gin.DefaultWriter = io.MultiWriter(fileWriter)
- }
- }
- oplogging.SetBackend(backends...)
- global.LOG = logger
-
- initMysqlLog()
-
- initFileLog()
-
- initMongoLog()
- }
- func initMysqlLog() {
- logConfig := global.CONFIG.Log
- var backends []oplogging.Backend
-
- registerStdout(logConfig, &backends)
-
- fileWriter := registerFile(logConfig, &backends, logConfig.BinlogDirPath, logConfig.BinlogSoftLink)
- global.MYSQL_LOG = fileWriter
- }
- func initFileLog() {
- logConfig := global.CONFIG.Log
-
- if logConfig.FilelogDirPath == "" || logConfig.FilelogSoftLink == `` {
- global.FILE_LOG = global.LOG
- return
- }
- logger := oplogging.MustGetLogger(Module)
- var backends []oplogging.Backend
-
-
-
- _ = registerFile(logConfig, &backends, logConfig.FilelogDirPath, logConfig.FilelogSoftLink)
- defaultBackend := oplogging.AddModuleLevel(backends[0])
- logger.SetBackend(defaultBackend)
- global.FILE_LOG = logger
- }
- func initMongoLog() {
- logConfig := global.CONFIG.Log
-
- if logConfig.MongologDirPath == "" || logConfig.MongologSoftLink == `` {
- global.MONGO_LOG = global.LOG
- return
- }
- logger := oplogging.MustGetLogger(Module)
- var backends []oplogging.Backend
-
-
-
- _ = registerFile(logConfig, &backends, logConfig.MongologDirPath, logConfig.MongologSoftLink)
- defaultBackend := oplogging.AddModuleLevel(backends[0])
- logger.SetBackend(defaultBackend)
- global.MONGO_LOG = logger
- }
- func registerStdout(c config.Log, backends *[]oplogging.Backend) {
- if c.Stdout != "" {
- level, err := oplogging.LogLevel(c.Stdout)
- if err != nil {
- fmt.Println(err)
- }
- *backends = append(*backends, createBackend(os.Stdout, c, level))
- }
- }
- func registerFile(c config.Log, backends *[]oplogging.Backend, logDir, logSoftLink string) io.Writer {
- if c.FileStdout != "" {
- if ok, _ := utils.PathExists(logDir); !ok {
-
- fmt.Println("create log directory")
- _ = os.Mkdir(logDir, os.ModePerm)
- }
-
- saveMaxTime := time.Duration(global.CONFIG.Log.SaveMaxDay) * 24 * time.Hour
-
- cuttingTime := time.Duration(global.CONFIG.Log.CuttingDay) * 24 * time.Hour
-
- fileWriter, err := rotatelogs.New(
- logDir+string(os.PathSeparator)+"%Y-%m-%d-%H-%M.log",
-
- rotatelogs.WithLinkName(logSoftLink),
-
- rotatelogs.WithMaxAge(saveMaxTime),
-
-
- rotatelogs.WithRotationTime(cuttingTime),
- )
- if err != nil {
- fmt.Println(err)
- }
- level, err := oplogging.LogLevel(c.FileStdout)
- if err != nil {
- fmt.Println(err)
- }
- *backends = append(*backends, createBackend(fileWriter, c, level))
- return fileWriter
- }
- return nil
- }
- func createBackend(w io.Writer, c config.Log, level oplogging.Level) oplogging.Backend {
- backend := oplogging.NewLogBackend(w, c.Prefix, 0)
- stdoutWriter := false
- if w == os.Stdout {
- stdoutWriter = true
- }
- format := getLogFormatter(c, stdoutWriter)
- backendLeveled := oplogging.AddModuleLevel(oplogging.NewBackendFormatter(backend, format))
- backendLeveled.SetLevel(level, Module)
- return backendLeveled
- }
- func getLogFormatter(c config.Log, stdoutWriter bool) oplogging.Formatter {
- pattern := defaultFormatter
- if !stdoutWriter {
-
-
- pattern = strings.Replace(pattern, "%{color:bold}", "", -1)
- pattern = strings.Replace(pattern, "%{color:reset}", "", -1)
- }
- if !c.LogFile {
-
- pattern = strings.Replace(pattern, "%{longfile}", "", -1)
- }
- return oplogging.MustStringFormatter(pattern)
- }
|