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.
153 lines
3.4 KiB
153 lines
3.4 KiB
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
|
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
"github.com/logrusorgru/aurora"
|
|
"github.com/rifflock/lfshook"
|
|
"github.com/sirupsen/logrus"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
const (
|
|
maxAgeHour = 168
|
|
rotationHour = 24
|
|
)
|
|
|
|
var isInit bool
|
|
var configFile string
|
|
var MyEnv EnvConfig
|
|
var Logger *logrus.Logger
|
|
|
|
func init() {
|
|
flag.StringVar(&configFile, "cf", "config.yml", "配置文件名")
|
|
isInit = false
|
|
}
|
|
|
|
func checkInitLog() bool {
|
|
return isInit
|
|
}
|
|
|
|
func InitConfig() {
|
|
flag.Parse()
|
|
//读取配置文件
|
|
data, _ := ioutil.ReadFile(configFile)
|
|
err := yaml.Unmarshal(data, &MyEnv)
|
|
if err != nil {
|
|
fmt.Println("读取配置文件错误...")
|
|
return
|
|
}
|
|
if checkInitLog() {
|
|
return
|
|
}
|
|
Logger = initLogger(MyEnv.Log.LogEnv, MyEnv.Log.LogPath)
|
|
}
|
|
|
|
func initLogger(env string, logPath string) *logrus.Logger {
|
|
logFilePath := ""
|
|
errFilePath := ""
|
|
if dir, err := os.Getwd(); err == nil {
|
|
logFilePath = dir + "/logs/" + logPath + "/all.log"
|
|
errFilePath = dir + "/logs/" + logPath + "/error.log"
|
|
}
|
|
|
|
accessWriter, err := rotatelogs.New(
|
|
logFilePath+".%Y-%m-%d",
|
|
rotatelogs.WithLinkName(logFilePath),
|
|
rotatelogs.WithRotationTime(time.Hour*rotationHour),
|
|
rotatelogs.WithMaxAge(time.Hour*maxAgeHour),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
errorWriter, err := rotatelogs.New(
|
|
errFilePath+".%Y-%m-%d",
|
|
rotatelogs.WithLinkName(errFilePath),
|
|
rotatelogs.WithRotationTime(time.Hour*rotationHour),
|
|
rotatelogs.WithMaxAge(time.Hour*maxAgeHour),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
allWriter := io.MultiWriter(accessWriter, errorWriter)
|
|
//实例化
|
|
logger := logrus.New()
|
|
logger.Out = os.Stdout
|
|
//设置日志格式
|
|
logger.SetFormatter(&ConsoleFormatter{})
|
|
logger.SetReportCaller(true)
|
|
//设置输出
|
|
if env != "dev" {
|
|
logger.AddHook(lfshook.NewHook(
|
|
lfshook.WriterMap{
|
|
logrus.DebugLevel: accessWriter,
|
|
logrus.InfoLevel: accessWriter,
|
|
logrus.ErrorLevel: allWriter,
|
|
logrus.PanicLevel: allWriter,
|
|
},
|
|
&SaveFormatter{},
|
|
))
|
|
}
|
|
//设置日志级别
|
|
logger.SetLevel(logrus.DebugLevel)
|
|
Logger = logger
|
|
isInit = true
|
|
return logger
|
|
}
|
|
|
|
type ConsoleFormatter struct {
|
|
}
|
|
|
|
func (m *ConsoleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
var b *bytes.Buffer
|
|
if entry.Buffer != nil {
|
|
b = entry.Buffer
|
|
} else {
|
|
b = &bytes.Buffer{}
|
|
}
|
|
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
|
var newLog string
|
|
var levelString aurora.Value
|
|
switch entry.Level.String() {
|
|
case "info":
|
|
levelString = aurora.Green(entry.Level)
|
|
case "warning":
|
|
levelString = aurora.Yellow(entry.Level)
|
|
case "debug":
|
|
levelString = aurora.Gray(16-1, entry.Level)
|
|
case "error":
|
|
levelString = aurora.Red(entry.Level)
|
|
case "fatal":
|
|
levelString = aurora.Red(entry.Level)
|
|
case "panic":
|
|
levelString = aurora.Red(entry.Level)
|
|
}
|
|
newLog = fmt.Sprintf("[%s] [%s:%d] [%s] %s \n", timestamp, path.Base(entry.Caller.File), entry.Caller.Line, levelString, entry.Message)
|
|
b.WriteString(newLog)
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
type SaveFormatter struct {
|
|
}
|
|
|
|
func (m *SaveFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
var b *bytes.Buffer
|
|
if entry.Buffer != nil {
|
|
b = entry.Buffer
|
|
} else {
|
|
b = &bytes.Buffer{}
|
|
}
|
|
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
|
var newLog string
|
|
newLog = fmt.Sprintf("[%s] [%s:%d] [%s] %s \n", timestamp, path.Base(entry.Caller.File), entry.Caller.Line, entry.Level, entry.Message)
|
|
b.WriteString(newLog)
|
|
return b.Bytes(), nil
|
|
}
|
|
|