Files
open-im-server/pkg/common/log/logrus.go
T

203 lines
5.4 KiB
Go
Raw Normal View History

2021-05-26 19:37:10 +08:00
package log
import (
"Open_IM/pkg/common/config"
2022-08-12 11:24:21 +08:00
"bufio"
2022-08-11 19:47:59 +08:00
//"bufio"
2021-05-26 19:37:10 +08:00
"fmt"
2022-01-27 01:08:02 +08:00
"os"
"time"
2021-05-26 19:37:10 +08:00
nested "github.com/antonfisher/nested-logrus-formatter"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
var logger *Logger
type Logger struct {
*logrus.Logger
Pid int
}
func init() {
logger = loggerInit("")
}
func NewPrivateLog(moduleName string) {
logger = loggerInit(moduleName)
}
func loggerInit(moduleName string) *Logger {
var logger = logrus.New()
//All logs will be printed
2021-10-11 18:18:50 +08:00
logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
//Close std console output
2022-09-01 20:51:53 +08:00
//os.O_WRONLY | os.O_CREATE | os.O_APPEND
2022-09-01 21:15:30 +08:00
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
2022-08-12 11:24:21 +08:00
if err != nil {
panic(err.Error())
}
writer := bufio.NewWriter(src)
logger.SetOutput(writer)
2022-08-29 16:11:56 +08:00
// logger.SetOutput(os.Stdout)
2021-10-11 18:18:50 +08:00
//Log Console Print Style Setting
2021-05-26 19:37:10 +08:00
logger.SetFormatter(&nested.Formatter{
2021-08-06 14:56:41 +08:00
TimestampFormat: "2006-01-02 15:04:05.000",
2021-05-26 19:37:10 +08:00
HideKeys: false,
2021-10-11 18:18:50 +08:00
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
2021-05-26 19:37:10 +08:00
})
//File name and line number display hook
logger.AddHook(newFileHook())
//Send logs to elasticsearch hook
2021-10-11 18:18:50 +08:00
if config.Config.Log.ElasticSearchSwitch {
2021-05-26 19:37:10 +08:00
logger.AddHook(newEsHook(moduleName))
}
//Log file segmentation hook
2021-05-31 10:03:57 +08:00
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
2021-05-26 19:37:10 +08:00
logger.AddHook(hook)
return &Logger{
logger,
os.Getpid(),
}
}
2021-05-31 10:03:57 +08:00
func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
2021-05-26 19:37:10 +08:00
lfsHook := lfshook.NewHook(lfshook.WriterMap{
2022-03-31 11:15:06 +08:00
logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
2021-05-26 19:37:10 +08:00
}, &nested.Formatter{
2021-08-06 14:56:41 +08:00
TimestampFormat: "2006-01-02 15:04:05.000",
2021-05-26 19:37:10 +08:00
HideKeys: false,
2021-10-11 18:18:50 +08:00
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
2021-05-26 19:37:10 +08:00
})
return lfsHook
}
2021-05-31 10:03:57 +08:00
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
2021-10-11 18:18:50 +08:00
if moduleName != "" {
moduleName = moduleName + "."
}
2021-05-31 10:03:57 +08:00
writer, err := rotatelogs.New(
2021-10-11 18:18:50 +08:00
config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d",
2021-05-31 10:03:57 +08:00
rotatelogs.WithRotationTime(rotationTime),
rotatelogs.WithRotationCount(maxRemainNum),
)
if err != nil {
2021-11-26 14:20:50 +08:00
panic(err.Error())
2021-05-31 10:03:57 +08:00
} else {
return writer
}
}
2021-05-26 19:37:10 +08:00
2022-01-14 16:11:23 +08:00
func Info(OperationID string, args ...interface{}) {
2021-10-11 18:18:50 +08:00
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
2022-01-14 16:11:23 +08:00
"PID": logger.Pid,
}).Infoln(args)
2021-05-26 19:37:10 +08:00
}
2022-01-14 16:11:23 +08:00
func Error(OperationID string, args ...interface{}) {
2021-10-11 18:18:50 +08:00
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
2022-01-14 16:11:23 +08:00
"PID": logger.Pid,
}).Errorln(args)
2021-05-26 19:37:10 +08:00
}
2022-01-14 16:11:23 +08:00
func Debug(OperationID string, args ...interface{}) {
2021-10-11 18:18:50 +08:00
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
2022-01-14 16:11:23 +08:00
"PID": logger.Pid,
}).Debugln(args)
2021-05-26 19:37:10 +08:00
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func Warning(token, OperationID, format string, args ...interface{}) {
2021-10-11 18:18:50 +08:00
logger.WithFields(logrus.Fields{
"PID": logger.Pid,
"OperationID": OperationID,
}).Warningf(format, args...)
2021-05-26 19:37:10 +08:00
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func InfoByArgs(format string, args ...interface{}) {
logger.WithFields(logrus.Fields{}).Infof(format, args)
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func ErrorByArgs(format string, args ...interface{}) {
logger.WithFields(logrus.Fields{}).Errorf(format, args...)
}
//Print log information in k, v format,
//kv is best to appear in pairs. tipInfo is the log prompt information for printing,
//and kv is the key and value for printing.
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func InfoByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Info(tipInfo)
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func ErrorByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Error(tipInfo)
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func DebugByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Debug(tipInfo)
}
2021-10-11 18:18:50 +08:00
//Deprecated
2021-05-26 19:37:10 +08:00
func WarnByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Warn(tipInfo)
}
//internal method
func argsHandle(OperationID string, fields logrus.Fields, args []interface{}) {
for i := 0; i < len(args); i += 2 {
if i+1 < len(args) {
fields[fmt.Sprintf("%v", args[i])] = args[i+1]
} else {
fields[fmt.Sprintf("%v", args[i])] = ""
}
}
2021-10-11 18:18:50 +08:00
fields["OperationID"] = OperationID
2021-05-26 19:37:10 +08:00
fields["PID"] = logger.Pid
}
2021-10-11 18:18:50 +08:00
func NewInfo(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
"PID": logger.Pid,
}).Infoln(args)
}
func NewError(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
"PID": logger.Pid,
}).Errorln(args)
}
func NewDebug(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
"PID": logger.Pid,
}).Debugln(args)
}
func NewWarn(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
"PID": logger.Pid,
}).Warnln(args)
}