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.
65 lines
1.2 KiB
65 lines
1.2 KiB
2 years ago
|
package pkg
|
||
|
|
||
|
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.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),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
logger := log.With(l,
|
||
|
"ts", log.DefaultTimestamp,
|
||
|
"caller", log.DefaultCaller,
|
||
|
"env", c.env,
|
||
|
"service.id", "id",
|
||
|
"service.name", "Name",
|
||
|
"service.version", "Version",
|
||
|
"trace.id", tracing.TraceID(),
|
||
|
"span.id", tracing.SpanID(),
|
||
|
)
|
||
|
|
||
|
return logger, func() {
|
||
|
l.Close()
|
||
|
}
|
||
|
}
|