Fetch the repository succeeded.
package logger
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
kratoszap "github.com/go-kratos/kratos/contrib/log/zap/v2"
"github.com/go-kratos/kratos/v2/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
var logConfig *LogConfig
func Trace() log.Valuer {
return func(ctx context.Context) interface{} {
if ctx, ok := ctx.(*gin.Context); ok {
return ctx.GetString("x-md-global-request-id")
}
return ""
}
}
func NewLoggerHelper(logger log.Logger, module string, path string) *log.Helper {
return log.NewHelper(log.NewFilter(log.With(logger, "requestId", Trace(), module, path), log.FilterLevel(log.Level(logConfig.Level))))
}
func NewAliyunLogger(config *SlsConfig) log.Logger {
return NewAliyunLog(
WithEndpoint(config.Endpoint),
WithAccessKey(config.AccessKey),
WithAccessSecret(config.AccessSecret),
WithLogStore(config.LogStore),
WithRegion(config.Region),
WithProject(config.Project),
)
}
func newFileLogger(config *LogConfig) log.Logger {
lumberJackLogger := &lumberjack.Logger{
Filename: "./" + config.FilePath + "log.json",
MaxSize: 10, // megabytes 单位M
MaxBackups: 100,
MaxAge: 7, //days
Compress: true, // disabled by default
}
defer func() {
_ = lumberJackLogger.Close()
}()
cfg := zap.NewProductionEncoderConfig()
cfg.EncodeTime = zapcore.ISO8601TimeEncoder // 设置时间格式
cfg.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime) // 设置时间格式
cfg.CallerKey = "caller"
encoder := zapcore.NewJSONEncoder(cfg)
core := zapcore.NewCore(encoder, zapcore.AddSync(lumberJackLogger), zapcore.DebugLevel)
z := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
logger := kratoszap.NewLogger(z)
return logger
}
func newConsoleLogger(config *LogConfig) log.Logger {
l := log.NewFilter(log.With(log.NewStdLogger(os.Stdout),
"ts", log.Timestamp(time.DateTime),
"caller", log.Caller(5),
//"trace.id", tracing.TraceID(),
//"span.id", tracing.SpanID(),
), log.FilterLevel(log.Level(config.Level)))
return l
}
func NewLogger(config *LogConfig, slsConfig *SlsConfig) log.Logger {
logConfig = config
if config.Output == "file" {
return newFileLogger(config)
} else if config.Output == "sls" {
return NewAliyunLogger(slsConfig)
} else if config.Output == "console" {
return newConsoleLogger(config)
} else {
panic(fmt.Sprintf("未知的日志输出方式:config output is %s", config.Output))
}
}
func NewDefaultHelper(logger log.Logger) *log.Helper {
return log.NewHelper(log.NewFilter(log.With(logger), log.FilterLevel(log.LevelDebug)))
}
type LogConfig struct {
Level int `json:"level"`
Output string `json:"output"`
Formatter string `json:"formatter"`
FileOpen bool `json:"fileOpen"`
FilePath string `json:"filePath"`
ElasticOpen bool `json:"elasticOpen"`
ElasticIndex string `json:"elasticIndex"`
ElasticLevel string `json:"elasticLevel"`
Channel []string `json:"channel"`
RequestLog bool `json:"requestLog"`
RequestLogType string `json:"requestLogType"`
}
type SlsConfig struct {
Endpoint string `json:"endpoint"`
AccessKey string `json:"accessKey"`
AccessKeyId string `json:"accessKeyId"`
AccessSecret string `json:"accessSecret"`
AccessKeySecret string `json:"accessKeySecret"`
LogStore string `json:"logStore"`
LogStoreName string `json:"logStoreName,"`
Region string `json:"region"`
Project string `json:"project"`
ProjectName string `json:"projectName"`
}
func NewTestLogger() *log.Helper {
return NewDefaultHelper(NewLogger(&LogConfig{Output: "console", Level: 0}, &SlsConfig{}))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。