From b3679f72707f117c25c1d7c0e71ee7e8d0930697 Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Wed, 22 Feb 2023 16:37:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?env=E5=92=8C=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- glog/v1/logger.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/glog/v1/logger.go b/glog/v1/logger.go index 3d4b702..6daf62b 100644 --- a/glog/v1/logger.go +++ b/glog/v1/logger.go @@ -13,11 +13,11 @@ type Logger interface { } type LoggerConfig struct { - env string - id string - name string - version string - aliLogConfig AliLogConfig + Env string + Id string + Name string + Version string + AliLogConfig AliLogConfig } type AliLogConfig struct { @@ -32,28 +32,28 @@ func NewLogger(c *LoggerConfig) (log.Logger, func()) { var l Logger - switch c.env { + switch c.Env { default: l = NewStdLogger(os.Stdout) case enum.EnvLocal: l = NewStdLogger(os.Stdout) case enum.EnvDevelop, enum.EnvPre, enum.EnvProduct: l = NewAliyunLog( - WithAccessKey(c.aliLogConfig.AccessKey), - WithAccessSecret(c.aliLogConfig.AccessSecret), - WithEndpoint(c.aliLogConfig.Endpoint), - WithProject(c.aliLogConfig.Project), - WithLogstore(c.aliLogConfig.LogStore), + WithAccessKey(c.AliLogConfig.AccessKey), + WithAccessSecret(c.AliLogConfig.AccessSecret), + WithEndpoint(c.AliLogConfig.Endpoint), + WithProject(c.AliLogConfig.Project), + WithLogstore(c.AliLogConfig.LogStore), ) } logger := log.With(l, "ts", log.DefaultTimestamp, "caller", log.DefaultCaller, - "env", c.env, - "service.id", "id", - "service.name", "Name", - "service.version", "Version", + "env", c.Env, + "service.id", c.Id, + "service.name", c.Name, + "service.version", c.Version, "trace.id", tracing.TraceID(), "span.id", tracing.SpanID(), ) From 4244fbe25fed7812f346b85637753a0f3b69dfbb Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Wed, 22 Feb 2023 17:05:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?env=E5=92=8C=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e745986..784d2bb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ * [transport:通用http ResponseEncoder](#transport通用http-responseencoder) * [meta:跨服务meta信息设置和获取](#meta跨服务meta信息设置和获取) * [encrypt:对称加密](#encrypt对称加密) - + ## 安装 @@ -55,4 +55,47 @@ go get gitea.drugeyes.vip/pharnexbase/tools@v1.0.0 * v1 - > 对称加密,目前包含:SHA1PRNG \ No newline at end of file + > 对称加密,目前包含: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("请输入正确手机号") + +``` + + + From 2c221193af322b19438913e64dfa402617704753 Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Wed, 22 Feb 2023 17:14:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?env=E5=92=8C=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- glog/v1/glog.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/glog/v1/glog.go b/glog/v1/glog.go index 6b7b4fd..2156ce8 100644 --- a/glog/v1/glog.go +++ b/glog/v1/glog.go @@ -1,9 +1,17 @@ package glog -import "github.com/go-kratos/kratos/v2/log" +import ( + "github.com/go-kratos/kratos/v2/log" + "sync" +) -var Glog *log.Helper +var ( + once sync.Once + Glog *log.Helper +) func NewHelper(logger log.Logger) { - Glog = log.NewHelper(logger) + once.Do(func() { + Glog = log.NewHelper(logger) + }) }