service/library/logger/logger.go

194 lines
4.6 KiB
Go

package logger
import (
"bytes"
"fmt"
"io"
"os"
"path"
"runtime"
"time"
"service/library/configcenter"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
)
const (
red = 31
yellow = 33
blue = 36
gray = 37
)
var Global *logrus.Logger
func init() {
Global = logrus.New()
}
type DefaultFormatter struct{}
func (m *DefaultFormatter) Format(entry *logrus.Entry) ([]byte, error) {
// 根据不同的level展示颜色
//var levelColor int
//switch entry.Level {
//case logrus.DebugLevel, logrus.TraceLevel:
// levelColor = gray
//case logrus.WarnLevel:
// levelColor = yellow
//case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
// levelColor = red
//default:
// levelColor = blue
//}
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] %s \n", timestamp, entry.Level, entry.Message)
b.WriteString(newLog)
return b.Bytes(), nil
}
func InitLogger(config configcenter.LoggerConfig) {
Global = logrus.New()
Global.SetFormatter(&DefaultFormatter{})
lvl, _ := logrus.ParseLevel(config.StdoutLevel)
Global.SetLevel(lvl)
/* 日志轮转相关函数
WithLinkName 为最新的日志建立软连接
WithRotationTime 设置日志分割的时间,隔多久分割一次
WithMaxAge 和 WithRotationCount二者只能设置一个
WithMaxAge 设置文件清理前的最长保存时间
WithRotationCount` 设置文件清理前最多保存的个数
*/
fileWriter, _ := rotatelogs.New(
config.FileAbsPath+"_%Y%m%d.Global",
rotatelogs.WithLinkName(config.FileAbsPath),
rotatelogs.WithMaxAge(time.Duration(86400*7)*time.Second),
rotatelogs.WithRotationTime(time.Duration(86400)*time.Second),
//rotatelogs.WithRotationSize(1024*1024*500),
)
var writers io.Writer
if config.StdoutEnable && config.FileEnable {
writers = io.MultiWriter(os.Stdout, fileWriter)
} else if config.StdoutEnable {
writers = io.MultiWriter(os.Stdout)
} else if config.FileEnable {
writers = io.MultiWriter(fileWriter)
}
Global.SetOutput(writers)
}
func Trace(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Tracef(callInfo+format, args...)
}
func Debug(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Debugf(callInfo+format, args...)
}
func Info(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Infof(callInfo+format, args...)
}
func Warn(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Warnf(callInfo+format, args...)
}
func Error(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Errorf(callInfo+format, args...)
}
func Fatal(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Fatalf(callInfo+format, args...)
}
func Panic(format string, args ...interface{}) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return
}
callInfo := fmt.Sprintf("(%s %s:%d) ", path.Base(runtime.FuncForPC(pc).Name()), path.Base(file), line)
if Global == nil {
fmt.Printf(callInfo+format+"\n", args...)
return
}
Global.Panicf(callInfo+format, args...)
}
func Recover() {
err := recover()
if err != nil {
buf := make([]byte, 10240)
runtime.Stack(buf, false)
Error("panic: %v,\n%s", err, string(buf))
}
}