|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|