From 057470df1345620883a9f5c5f69d1ea730689d23 Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Thu, 9 Mar 2023 15:46:01 +0800 Subject: [PATCH] eslogger --- README.md | 38 -------------------------------------- glog/es/logger.go | 36 ++++++++++++++++++++++++++++++++++++ glog/v1/README.md | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 38 deletions(-) create mode 100644 glog/es/logger.go create mode 100644 glog/v1/README.md diff --git a/README.md b/README.md index 784d2bb..d064f5f 100644 --- a/README.md +++ b/README.md @@ -57,45 +57,7 @@ go get gitea.drugeyes.vip/pharnexbase/tools@v1.0.0 > 对称加密,目前包含:SHA1PRNG -### glog:全局日志 -* v1 -* 本地环境不变,dev,pre,pro 上传阿里 -```go -// main.go 加入 - -func initLog() (log.Logger, func()) { - - l, f := glog.NewLogger(&glog.LoggerConfig{ - Env: "dev", - Id: id, - Name: Name, - Version: Version, - AliLogConfig: glog.AliLogConfig{ - AccessKey: "", - AccessSecret: "", - Endpoint: "", - Project: "", - LogStore: "", - }, - }) - glog.NewHelper(l) - return l, f -} - -// main函数加入 -func main() { - ... - logger, f := initLog() - defer f() - ... - app, cleanup, err := wireApp(..., logger) -} - -// 使用 -glog.Glog.WithContext(ctx).Error("请输入正确手机号") - -``` diff --git a/glog/es/logger.go b/glog/es/logger.go new file mode 100644 index 0000000..216a6d2 --- /dev/null +++ b/glog/es/logger.go @@ -0,0 +1,36 @@ +package es + +import ( + "context" + "fmt" + "gitea.drugeyes.vip/pharnexbase/utils/glog/v1" + "strings" +) + +type Logger struct { +} + +// 这里es打印日志时需要实现的方法 +func (l *Logger) Printf(format string, v ...interface{}) { + return +} + +// 如果实现了此方法则不会调用上面的Printf +func (l *Logger) PrintfWithContext(ctx context.Context, format string, v ...interface{}) { + go func() { + defer func() { + if r := recover(); r != nil { + fmt.Println("PrintfWithContext recover", r) + } + }() + for _, item := range v { + switch itemData := item.(type) { + case string: + itemData = strings.Replace(itemData, "\r\n", "", -1) + glog.Glog.WithContext(ctx).Info(itemData) + default: + glog.Glog.WithContext(ctx).Infof(format, itemData) + } + } + }() +} diff --git a/glog/v1/README.md b/glog/v1/README.md new file mode 100644 index 0000000..3736a1f --- /dev/null +++ b/glog/v1/README.md @@ -0,0 +1,39 @@ +### glog:全局日志 + +* v1 +* 本地环境不变,dev,pre,pro 上传阿里 +```go +// main.go 加入 + +func initLog() (log.Logger, func()) { + + l, f := glog.NewLogger(&glog.LoggerConfig{ + Env: "dev", + Id: id, + Name: Name, + Version: Version, + AliLogConfig: glog.AliLogConfig{ + AccessKey: "", + AccessSecret: "", + Endpoint: "", + Project: "", + LogStore: "", + }, + }) + glog.NewHelper(l) + return l, f +} + +// main函数加入 +func main() { + ... + logger, f := initLog() + defer f() + ... + app, cleanup, err := wireApp(..., logger) +} + +// 使用 +glog.Glog.WithContext(ctx).Error("请输入正确手机号") + +``` \ No newline at end of file