通用包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
utils/glog/v1/logger.go

65 lines
1.2 KiB

2 years ago
package glog
2 years ago
import (
"gitea.drugeyes.vip/pharnexbase/utils/enum"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"os"
)
type Logger interface {
log.Logger
Close() error
}
type LoggerConfig struct {
2 years ago
Env string
Id string
Name string
Version string
AliLogConfig AliLogConfig
2 years ago
}
type AliLogConfig struct {
AccessKey string
AccessSecret string
Endpoint string
Project string
LogStore string
}
func NewLogger(c *LoggerConfig) (log.Logger, func()) {
var l Logger
2 years ago
switch c.Env {
2 years ago
default:
l = NewStdLogger(os.Stdout)
case enum.EnvLocal:
l = NewStdLogger(os.Stdout)
case enum.EnvDevelop, enum.EnvPre, enum.EnvProduct:
l = NewAliyunLog(
2 years ago
WithAccessKey(c.AliLogConfig.AccessKey),
WithAccessSecret(c.AliLogConfig.AccessSecret),
WithEndpoint(c.AliLogConfig.Endpoint),
WithProject(c.AliLogConfig.Project),
WithLogstore(c.AliLogConfig.LogStore),
2 years ago
)
}
logger := log.With(l,
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
2 years ago
"env", c.Env,
"service.id", c.Id,
"service.name", c.Name,
"service.version", c.Version,
2 years ago
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)
return logger, func() {
l.Close()
}
}