通用包
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

64 lines
1.3 KiB

package glog
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 {
Env string
Id string
Name string
Version string
AliLogConfig AliLogConfig
}
type AliLogConfig struct {
AccessKey string
AccessSecret string
Endpoint string
Project string
LogStore string
}
func NewLogger(c *LoggerConfig) (log.Logger, func()) {
var l Logger
switch c.Env {
default:
l = NewStdLogger(os.Stdout)
case enum.Env_local.String():
l = NewStdLogger(os.Stdout)
case enum.Env_develop.String(), enum.Env_preview.String(), enum.Env_production.String():
l = NewAliyunLog(
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", c.Id,
"service.name", c.Name,
"service.version", c.Version,
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)
return logger, func() {
l.Close()
}
}