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.
Open-IM-Server/pkg/common/log/logrus.go

176 lines
4.4 KiB

package log
import (
2 years ago
"OpenIM/pkg/common/config"
2 years ago
"OpenIM/pkg/common/tracelog"
2 years ago
"bufio"
2 years ago
"context"
2 years ago
//"bufio"
"fmt"
3 years ago
"os"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
)
2 years ago
var logger *LogrusLogger
var ctxLogger *LogrusLogger
2 years ago
type LogrusLogger struct {
*logrus.Logger
2 years ago
Pid int
Type string
}
func init() {
logger = loggerInit("")
2 years ago
ctxLogger = ctxLoggerInit("")
}
2 years ago
func NewPrivateLog(moduleName string) {
logger = loggerInit(moduleName)
}
2 years ago
func ctxLoggerInit(moduleName string) *LogrusLogger {
2 years ago
var ctxLogger = logrus.New()
ctxLogger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
panic(err.Error())
}
writer := bufio.NewWriter(src)
ctxLogger.SetOutput(writer)
2 years ago
ctxLogger.SetFormatter(&nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05.000",
HideKeys: false,
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
2 years ago
})
2 years ago
if config.Config.Log.ElasticSearchSwitch {
ctxLogger.AddHook(newEsHook(moduleName))
}
//Log file segmentation hook
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
ctxLogger.AddHook(hook)
2 years ago
return &LogrusLogger{
2 years ago
ctxLogger,
os.Getpid(),
2 years ago
"ctxLogger",
2 years ago
}
}
2 years ago
func loggerInit(moduleName string) *LogrusLogger {
var logger = logrus.New()
//All logs will be printed
logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
//Close std console output
//os.O_WRONLY | os.O_CREATE | os.O_APPEND
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
2 years ago
if err != nil {
panic(err.Error())
}
writer := bufio.NewWriter(src)
logger.SetOutput(writer)
2 years ago
// logger.SetOutput(os.Stdout)
//Log Console Print Style Setting
2 years ago
logger.SetFormatter(&nested.Formatter{
3 years ago
TimestampFormat: "2006-01-02 15:04:05.000",
HideKeys: false,
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
})
2 years ago
logger.SetFormatter(&logrus.JSONFormatter{})
//File name and line number display hook
logger.AddHook(newFileHook())
//Send logs to elasticsearch hook
if config.Config.Log.ElasticSearchSwitch {
logger.AddHook(newEsHook(moduleName))
}
//Log file segmentation hook
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
logger.AddHook(hook)
2 years ago
return &LogrusLogger{
logger,
os.Getpid(),
2 years ago
"",
}
}
2 years ago
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
operationID := tracelog.GetOperationID(ctx)
logger.WithFields(logrus.Fields{
"OperationID": operationID,
"PID": logger.Pid,
"Msg": msg,
}).Infoln(keysAndValues)
}
2 years ago
3 years ago
func Info(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
3 years ago
"PID": logger.Pid,
}).Infoln(args)
}
3 years ago
func Error(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
3 years ago
"PID": logger.Pid,
}).Errorln(args)
}
3 years ago
func Debug(OperationID string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"OperationID": OperationID,
3 years ago
"PID": logger.Pid,
}).Debugln(args)
}
//Deprecated
func Warning(token, OperationID, format string, args ...interface{}) {
logger.WithFields(logrus.Fields{
"PID": logger.Pid,
"OperationID": OperationID,
}).Warningf(format, args...)
}
//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])] = ""
}
}
fields["OperationID"] = OperationID
fields["PID"] = logger.Pid
}
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)
}