This commit is contained in:
away
2021-06-28 15:31:36 +08:00
parent ed52d40d8d
commit 75d308de37
7 changed files with 113 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
** description("Hook to send logs to elasticsearch").
** description("将日志发送到elasticsearch的hook").
** copyright('tuoyun,www.tuoyun.net').
** author("fg,Gordon@tuoyun.net").
** time(2021/3/26 17:05).
@@ -18,13 +18,13 @@ import (
"time"
)
//esHook custom es hook
//esHook 自定义的ES hook
type esHook struct {
moduleName string
client *elasticV7.Client
}
//newEsHook initialization
//newEsHook 初始化
func newEsHook(moduleName string) *esHook {
//https://github.com/sohlich/elogrus
//client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
@@ -61,19 +61,19 @@ func newEsHook(moduleName string) *esHook {
return &esHook{client: es, moduleName: moduleName}
}
//Fire log hook interface method
//Fire log hook interface 方法
func (hook *esHook) Fire(entry *logrus.Entry) error {
doc := newEsLog(entry)
go hook.sendEs(doc)
return nil
}
//Levels log hook interface method, the log affected by this hook
//Levels log hook interface 方法,此hook影响的日志
func (hook *esHook) Levels() []logrus.Level {
return logrus.AllLevels
}
//sendEs Asynchronously send logs to es
//sendEs 异步发送日志到es
func (hook *esHook) sendEs(doc appLogDocModel) {
defer func() {
if r := recover(); r != nil {
@@ -102,7 +102,7 @@ func newEsLog(e *logrus.Entry) appLogDocModel {
return ins
}
// indexName es index name time division
// indexName es index name 时间分割
func (m *appLogDocModel) indexName() string {
return time.Now().Format("2006-01-02")
}

View File

@@ -1,5 +1,5 @@
/*
** description("Get the hook of the calling file name and line number").
** description("得到调用文件名字和行号的hook").
** copyright('tuoyun,www.tuoyun.net').
** author("fg,Gordon@tuoyun.net").
** time(2021/3/16 11:26).

View File

@@ -12,42 +12,42 @@ import (
)
const (
TimeOffset = 8 * 3600 //8 hour offset
HalfOffset = 12 * 3600 //Half-day hourly offset
TimeOffset = 8 * 3600 //8个小时的偏移量
HalfOffset = 12 * 3600 //半天的小时偏移量
)
//Get the current timestamp
//获取当前的时间戳
func GetCurrentTimestamp() int64 {
return time.Now().Unix()
}
//Get the timestamp at 0 o'clock of the day
//获取当天0点的时间戳
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
//获取当天12点的时间戳
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"
//获取当天0点格式化时间格式为"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"
//获取当天12点格式化时间格式为"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" //Template required for transformation
loc, _ := time.LoadLocation("Local") //Get time zone
timeLayout := "2006-01-02 15:04:05" //转化所需模板
loc, _ := time.LoadLocation("Local") //获取时区
tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
timestamp := tmp.Unix() //Converted to timestamp type is int64
timestamp := tmp.Unix() //转化为时间戳 类型是int64
return strconv.FormatInt(timestamp, 10)
}