v3 - main to cut out

This commit is contained in:
Xinwei Xiong(cubxxw-openim)
2023-06-29 22:35:31 +08:00
commit 6d499032fa
293 changed files with 57778 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
/*
** 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 (
"Open_IM/pkg/common/config"
"context"
"fmt"
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")
}
+71
View File
@@ -0,0 +1,71 @@
/*
** 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 (
"fmt"
"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 {
entry.Data["FilePath"] = findCaller(6)
return nil
}
//func (f *fileHook) Fire(entry *logrus.Entry) error {
// var s string
// _, b, c, _ := runtime.Caller(10)
// i := strings.SplitAfter(b, "/")
// if len(i) > 3 {
// s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(c)
// }
// entry.Data["FilePath"] = s
// return nil
//}
func findCaller(skip int) string {
file := ""
line := 0
for i := 0; i < 10; i++ {
file, line = getCaller(skip + i)
if !strings.HasPrefix(file, "log") {
break
}
}
return fmt.Sprintf("%s:%d", file, line)
}
func getCaller(skip int) (string, int) {
_, file, line, ok := runtime.Caller(skip)
if !ok {
return "", 0
}
n := 0
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
n++
if n >= 2 {
file = file[i+1:]
break
}
}
}
return file, line
}
+199
View File
@@ -0,0 +1,199 @@
package log
import (
"Open_IM/pkg/common/config"
"bufio"
"fmt"
"os"
"time"
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
logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
//Close std console output
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"},
})
//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 &Logger{
logger,
os.Getpid(),
}
}
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"},
})
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
}
}
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...)
}
//Deprecated
func InfoByArgs(format string, args ...interface{}) {
logger.WithFields(logrus.Fields{}).Infof(format, args)
}
//Deprecated
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.
//Deprecated
func InfoByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Info(tipInfo)
}
//Deprecated
func ErrorByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Error(tipInfo)
}
//Deprecated
func DebugByKv(tipInfo, OperationID string, args ...interface{}) {
fields := make(logrus.Fields)
argsHandle(OperationID, fields, args)
logger.WithFields(fields).Debug(tipInfo)
}
//Deprecated
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])] = ""
}
}
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)
}
+57
View File
@@ -0,0 +1,57 @@
/*
** description("").
** copyright('tuoyun,www.tuoyun.net').
** author("fg,Gordon@tuoyun.net").
** time(2021/2/22 11:52).
*/
package log
import (
"strconv"
"time"
)
const (
TimeOffset = 8 * 3600 //8 hour offset
HalfOffset = 12 * 3600 //Half-day hourly offset
)
//Get the current timestamp
func GetCurrentTimestamp() int64 {
return time.Now().Unix()
}
//Get the current 0 o'clock timestamp
func GetCurDayZeroTimestamp() int64 {
timeStr := time.Now().Format("2006-01-02")
t, _ := time.Parse("2006-01-02", timeStr)
return t.Unix() - TimeOffset
}
//Get the timestamp at 12 o'clock on the day
func GetCurDayHalfTimestamp() int64 {
return GetCurDayZeroTimestamp() + HalfOffset
}
//Get the formatted time at 0 o'clock of the day, the format is "2006-01-02_00-00-00"
func GetCurDayZeroTimeFormat() string {
return time.Unix(GetCurDayZeroTimestamp(), 0).Format("2006-01-02_15-04-05")
}
//Get the formatted time at 12 o'clock of the day, the format is "2006-01-02_12-00-00"
func GetCurDayHalfTimeFormat() string {
return time.Unix(GetCurDayZeroTimestamp()+HalfOffset, 0).Format("2006-01-02_15-04-05")
}
func GetTimeStampByFormat(datetime string) string {
timeLayout := "2006-01-02 15:04:05" //转化所需模板
loc, _ := time.LoadLocation("Local") //获取时区
tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
timestamp := tmp.Unix() //转化为时间戳 类型是int64
return strconv.FormatInt(timestamp, 10)
}
func TimeStringFormatTimeUnix(timeFormat string, timeSrc string) int64 {
tm, _ := time.Parse(timeFormat, timeSrc)
return tm.Unix()
}