You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.8 KiB

package core
import (
"fmt"
"gin-vue-admin/config"
"gin-vue-admin/global"
"gin-vue-admin/utils"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
oplogging "github.com/op/go-logging"
"io"
"os"
"strings"
"time"
)
const (
logDir = "log"
logSoftLink = "api.log"
module = "gin-vue-admin"
)
var (
defaultFormatter = `%{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}`
)
func init() {
c := global.GVA_CONFIG.Log
if c.Prefix == "" {
_ = fmt.Errorf("logger prefix not found")
}
logger := oplogging.MustGetLogger(module)
var backends []oplogging.Backend
backends = registerStdout(c, backends)
backends = registerFile(c, backends)
oplogging.SetBackend(backends...)
global.GVA_LOG = logger
}
func registerStdout(c config.Log, backends []oplogging.Backend) []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))
}
return backends
}
func registerFile(c config.Log, backends []oplogging.Backend) []oplogging.Backend {
if c.File != "" {
if ok, _ := utils.PathExists(logDir); !ok {
// directory not exist
fmt.Println("create log directory")
_ = os.Mkdir(logDir, os.ModePerm)
}
apiLogPath := logDir + string(os.PathSeparator) + logSoftLink
fileWriter, err := rotatelogs.New(
apiLogPath+".%Y-%m-%d-%H-%M.log",
// generate soft link, point to latest log file
rotatelogs.WithLinkName(apiLogPath),
// maximum time to save log files
rotatelogs.WithMaxAge(7*24*time.Hour),
// time period of log file switching
rotatelogs.WithRotationTime(24*time.Hour),
)
if err != nil {
fmt.Println(err)
return backends
}
level, err := oplogging.LogLevel(c.File)
if err != nil {
fmt.Println(err)
}
backends = append(backends, createBackend(fileWriter, c, level))
}
return backends
}
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 {
// Color is only required for console output
// Other writers don't need %{color} tag
pattern = strings.Replace(pattern, "%{color:bold}", "", -1)
pattern = strings.Replace(pattern, "%{color:reset}", "", -1)
}
if !c.LogFile {
// Remove %{logfile} tag
pattern = strings.Replace(pattern, "%{longfile}", "", -1)
}
return oplogging.MustStringFormatter(pattern)
}