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

177 lines
4.4 KiB
Go
Raw Normal View History

2021-05-26 19:37:10 +08:00
package log
import (
2022-08-12 11:24:21 +08:00
"bufio"
2023-03-13 12:34:56 +08:00
"context"
2023-04-18 14:43:54 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
2023-03-21 12:28:21 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-13 12:34:56 +08:00
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"
"github.com/sirupsen/logrus"
)
2023-03-13 10:21:50 +08:00
var logger *LogrusLogger
var ctxLogger *LogrusLogger
2021-05-26 19:37:10 +08:00
2023-03-13 10:03:23 +08:00
type LogrusLogger struct {
2021-05-26 19:37:10 +08:00
*logrus.Logger
2023-01-12 16:45:50 +08:00
Pid int
Type string
2021-05-26 19:37:10 +08:00
}
func init() {
logger = loggerInit("")
2023-01-12 15:55:44 +08:00
ctxLogger = ctxLoggerInit("")
2021-05-26 19:37:10 +08:00
}
2023-01-12 15:55:44 +08:00
2021-05-26 19:37:10 +08:00
func NewPrivateLog(moduleName string) {
logger = loggerInit(moduleName)
}
2023-03-13 10:03:23 +08:00
func ctxLoggerInit(moduleName string) *LogrusLogger {
2023-01-12 15:55:44 +08:00
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)
2023-01-12 16:06:21 +08:00
ctxLogger.SetFormatter(&nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05.000",
HideKeys: false,
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
2023-01-12 15:55:44 +08:00
})
2023-01-12 16:06:21 +08:00
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)
2023-03-13 10:03:23 +08:00
return &LogrusLogger{
2023-01-12 15:55:44 +08:00
ctxLogger,
os.Getpid(),
2023-01-12 16:45:50 +08:00
"ctxLogger",
2023-01-12 15:55:44 +08:00
}
}
2023-03-13 10:03:23 +08:00
func loggerInit(moduleName string) *LogrusLogger {
2021-05-26 19:37:10 +08:00
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
2023-01-12 15:55:44 +08:00
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
})
2023-03-13 10:10:50 +08:00
logger.SetFormatter(&logrus.JSONFormatter{})
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)
2023-03-13 10:03:23 +08:00
return &LogrusLogger{
2021-05-26 19:37:10 +08:00
logger,
os.Getpid(),
2023-01-12 16:45:50 +08:00
"",
2021-05-26 19:37:10 +08:00
}
}
2023-03-13 12:34:56 +08:00
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
2023-03-21 12:28:21 +08:00
operationID := mcontext.GetOperationID(ctx)
2023-03-13 12:34:56 +08:00
logger.WithFields(logrus.Fields{
"OperationID": operationID,
"PID": logger.Pid,
"Msg": msg,
}).Infoln(keysAndValues)
2021-05-26 19:37:10 +08:00
}
2023-03-13 12:34:56 +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
}
2023-03-16 10:46:06 +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
}
2023-03-16 10:46:06 +08:00
// internal method
2021-05-26 19:37:10 +08:00
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)
}