Merge branch 'master' of gitea.drugeyes.vip:pharnexbase/utils

master
commit ea3901866c
  1. 43
      README.md
  2. 12
      glog/v1/glog.go
  3. 30
      glog/v1/logger.go

@ -56,3 +56,46 @@ go get gitea.drugeyes.vip/pharnexbase/tools@v1.0.0
* v1 * v1
> 对称加密,目前包含:SHA1PRNG > 对称加密,目前包含: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("请输入正确手机号")
```

@ -1,9 +1,17 @@
package glog 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) { func NewHelper(logger log.Logger) {
once.Do(func() {
Glog = log.NewHelper(logger) Glog = log.NewHelper(logger)
})
} }

@ -13,11 +13,11 @@ type Logger interface {
} }
type LoggerConfig struct { type LoggerConfig struct {
env string Env string
id string Id string
name string Name string
version string Version string
aliLogConfig AliLogConfig AliLogConfig AliLogConfig
} }
type AliLogConfig struct { type AliLogConfig struct {
@ -32,28 +32,28 @@ func NewLogger(c *LoggerConfig) (log.Logger, func()) {
var l Logger var l Logger
switch c.env { switch c.Env {
default: default:
l = NewStdLogger(os.Stdout) l = NewStdLogger(os.Stdout)
case enum.EnvLocal: case enum.EnvLocal:
l = NewStdLogger(os.Stdout) l = NewStdLogger(os.Stdout)
case enum.EnvDevelop, enum.EnvPre, enum.EnvProduct: case enum.EnvDevelop, enum.EnvPre, enum.EnvProduct:
l = NewAliyunLog( l = NewAliyunLog(
WithAccessKey(c.aliLogConfig.AccessKey), WithAccessKey(c.AliLogConfig.AccessKey),
WithAccessSecret(c.aliLogConfig.AccessSecret), WithAccessSecret(c.AliLogConfig.AccessSecret),
WithEndpoint(c.aliLogConfig.Endpoint), WithEndpoint(c.AliLogConfig.Endpoint),
WithProject(c.aliLogConfig.Project), WithProject(c.AliLogConfig.Project),
WithLogstore(c.aliLogConfig.LogStore), WithLogstore(c.AliLogConfig.LogStore),
) )
} }
logger := log.With(l, logger := log.With(l,
"ts", log.DefaultTimestamp, "ts", log.DefaultTimestamp,
"caller", log.DefaultCaller, "caller", log.DefaultCaller,
"env", c.env, "env", c.Env,
"service.id", "id", "service.id", c.Id,
"service.name", "Name", "service.name", c.Name,
"service.version", "Version", "service.version", c.Version,
"trace.id", tracing.TraceID(), "trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(), "span.id", tracing.SpanID(),
) )

Loading…
Cancel
Save