parent
62d4a4dd9c
commit
568f6abe80
@ -1,107 +0,0 @@
|
|||||||
/*
|
|
||||||
** description("Send logs to elasticsearch hook").
|
|
||||||
** copyright('tuoyun,www.tuoyun.net').
|
|
||||||
** author("fg,Gordon@tuoyun.net").
|
|
||||||
** time(2021/3/26 17:05).
|
|
||||||
*/
|
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
|
||||||
elasticV7 "github.com/olivere/elastic/v7"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// esHook CUSTOMIZED ES hook
|
|
||||||
type esHook struct {
|
|
||||||
moduleName string
|
|
||||||
client *elasticV7.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// newEsHook Initialization
|
|
||||||
func newEsHook(moduleName string) *esHook {
|
|
||||||
//https://github.com/sohlich/elogrus
|
|
||||||
//client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
|
|
||||||
//if err != nil {
|
|
||||||
// log.Panic(err)
|
|
||||||
//}
|
|
||||||
//hook, err := elogrus.NewAsyncElasticHook(client, "localhost", logrus.DebugLevel, "mylog")
|
|
||||||
//if err != nil {
|
|
||||||
// log.Panic(err)
|
|
||||||
//}
|
|
||||||
es, err := elasticV7.NewClient(
|
|
||||||
elasticV7.SetURL(config.Config.Log.ElasticSearchAddr...),
|
|
||||||
elasticV7.SetBasicAuth(config.Config.Log.ElasticSearchUser, config.Config.Log.ElasticSearchPassword),
|
|
||||||
elasticV7.SetSniff(false),
|
|
||||||
elasticV7.SetHealthcheckInterval(60*time.Second),
|
|
||||||
elasticV7.SetErrorLog(log.New(os.Stderr, "ES:", log.LstdFlags)),
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("failed to create Elastic V7 Client: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//info, code, err := es.Ping(logConfig.ElasticSearch.EsAddr[0]).Do(context.Background())
|
|
||||||
//if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
//}
|
|
||||||
//fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
|
|
||||||
//
|
|
||||||
//esversion, err := es.ElasticsearchVersion(logConfig.ElasticSearch.EsAddr[0])
|
|
||||||
//if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
//}
|
|
||||||
//fmt.Printf("Elasticsearch version %s\n", esversion)
|
|
||||||
return &esHook{client: es, moduleName: moduleName}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire log hook interface
|
|
||||||
func (hook *esHook) Fire(entry *logrus.Entry) error {
|
|
||||||
doc := newEsLog(entry)
|
|
||||||
go hook.sendEs(doc)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hook *esHook) Levels() []logrus.Level {
|
|
||||||
return logrus.AllLevels
|
|
||||||
}
|
|
||||||
|
|
||||||
// sendEs
|
|
||||||
func (hook *esHook) sendEs(doc appLogDocModel) {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
fmt.Println("send entry to es failed: ", r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
_, err := hook.client.Index().Index(hook.moduleName).Type(doc.indexName()).BodyJson(doc).Do(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// appLogDocModel es model
|
|
||||||
type appLogDocModel map[string]interface{}
|
|
||||||
|
|
||||||
func newEsLog(e *logrus.Entry) appLogDocModel {
|
|
||||||
ins := make(map[string]interface{})
|
|
||||||
ins["level"] = strings.ToUpper(e.Level.String())
|
|
||||||
ins["time"] = e.Time.Format("2006-01-02 15:04:05")
|
|
||||||
for kk, vv := range e.Data {
|
|
||||||
ins[kk] = vv
|
|
||||||
}
|
|
||||||
ins["tipInfo"] = e.Message
|
|
||||||
|
|
||||||
return ins
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexName es index name
|
|
||||||
func (m *appLogDocModel) indexName() string {
|
|
||||||
return time.Now().Format("2006-01-02")
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
** description("get the name and line number of the calling file hook").
|
|
||||||
** copyright('tuoyun,www.tuoyun.net').
|
|
||||||
** author("fg,Gordon@tuoyun.net").
|
|
||||||
** time(2021/3/16 11:26).
|
|
||||||
*/
|
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fileHook struct{}
|
|
||||||
|
|
||||||
func newFileHook() *fileHook {
|
|
||||||
return &fileHook{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileHook) Levels() []logrus.Level {
|
|
||||||
return logrus.AllLevels
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileHook) Fire(entry *logrus.Entry) error {
|
|
||||||
var s string
|
|
||||||
_, file, line, _ := runtime.Caller(8)
|
|
||||||
i := strings.SplitAfter(file, "/")
|
|
||||||
if len(i) > 3 {
|
|
||||||
s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line)
|
|
||||||
}
|
|
||||||
entry.Data["FilePath"] = s
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
|
||||||
nested "github.com/antonfisher/nested-logrus-formatter"
|
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
||||||
"github.com/rifflock/lfshook"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
|
|
||||||
lfsHook := lfshook.NewHook(lfshook.WriterMap{
|
|
||||||
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),
|
|
||||||
}, &nested.Formatter{
|
|
||||||
TimestampFormat: "2006-01-02 15:04:05.000",
|
|
||||||
HideKeys: false,
|
|
||||||
FieldsOrder: []string{"PID", "FilePath", "OperationID", "Msg"},
|
|
||||||
})
|
|
||||||
return lfsHook
|
|
||||||
}
|
|
||||||
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
|
|
||||||
if moduleName != "" {
|
|
||||||
moduleName = moduleName + "."
|
|
||||||
}
|
|
||||||
writer, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d",
|
|
||||||
rotatelogs.WithRotationTime(rotationTime),
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
} else {
|
|
||||||
return writer
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,176 +0,0 @@
|
|||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
|
||||||
|
|
||||||
//"bufio"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
nested "github.com/antonfisher/nested-logrus-formatter"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
var logger *LogrusLogger
|
|
||||||
var ctxLogger *LogrusLogger
|
|
||||||
|
|
||||||
type LogrusLogger struct {
|
|
||||||
*logrus.Logger
|
|
||||||
Pid int
|
|
||||||
Type string
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
logger = loggerInit("")
|
|
||||||
ctxLogger = ctxLoggerInit("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPrivateLog(moduleName string) {
|
|
||||||
logger = loggerInit(moduleName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ctxLoggerInit(moduleName string) *LogrusLogger {
|
|
||||||
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)
|
|
||||||
ctxLogger.SetFormatter(&nested.Formatter{
|
|
||||||
TimestampFormat: "2006-01-02 15:04:05.000",
|
|
||||||
HideKeys: false,
|
|
||||||
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
|
|
||||||
})
|
|
||||||
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)
|
|
||||||
return &LogrusLogger{
|
|
||||||
ctxLogger,
|
|
||||||
os.Getpid(),
|
|
||||||
"ctxLogger",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
writer := bufio.NewWriter(src)
|
|
||||||
logger.SetOutput(writer)
|
|
||||||
// logger.SetOutput(os.Stdout)
|
|
||||||
//Log Console Print Style Setting
|
|
||||||
|
|
||||||
logger.SetFormatter(&nested.Formatter{
|
|
||||||
TimestampFormat: "2006-01-02 15:04:05.000",
|
|
||||||
HideKeys: false,
|
|
||||||
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
|
|
||||||
})
|
|
||||||
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)
|
|
||||||
return &LogrusLogger{
|
|
||||||
logger,
|
|
||||||
os.Getpid(),
|
|
||||||
"",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
|
||||||
operationID := mcontext.GetOperationID(ctx)
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": operationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
"Msg": msg,
|
|
||||||
}).Infoln(keysAndValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Info(OperationID string, args ...interface{}) {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Infoln(args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Error(OperationID string, args ...interface{}) {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Errorln(args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Debug(OperationID string, args ...interface{}) {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"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)
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package prome
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/peer"
|
|
||||||
)
|
|
||||||
|
|
||||||
func UnaryServerInterceptorPrometheus(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
||||||
remote, _ := peer.FromContext(ctx)
|
|
||||||
remoteAddr := remote.Addr.String()
|
|
||||||
|
|
||||||
in, _ := json.Marshal(req)
|
|
||||||
inStr := string(in)
|
|
||||||
log.NewInfo("ip", remoteAddr, "access_start", info.FullMethod, "in", inStr)
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
defer func() {
|
|
||||||
out, _ := json.Marshal(resp)
|
|
||||||
outStr := string(out)
|
|
||||||
duration := int64(time.Since(start) / time.Millisecond)
|
|
||||||
if duration >= 500 {
|
|
||||||
log.NewInfo("ip", remoteAddr, "access_end", info.FullMethod, "in", inStr, "out", outStr, "err", err, "duration/ms", duration)
|
|
||||||
} else {
|
|
||||||
log.NewInfo("ip", remoteAddr, "access_end", info.FullMethod, "in", inStr, "out", outStr, "err", err, "duration/ms", duration)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
resp, err = handler(ctx, req)
|
|
||||||
return
|
|
||||||
}
|
|
Loading…
Reference in new issue