1 Star 0 Fork 0

linxing/youye-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
zap.go 4.67 KB
一键复制 编辑 原始数据 按行查看 历史
Mark 提交于 2024-04-26 12:14 . feat:修改session信息,添加thrid类型
package zap
import (
"context"
"fmt"
"io"
"os"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gitee.com/linxing_3/youye-core/logger"
)
type zaplog struct {
cfg zap.Config
zap *zap.Logger
opts logger.Options
sync.RWMutex
fields map[string]interface{}
}
func (l *zaplog) Init(opts ...logger.Option) error {
//var err error
for _, o := range opts {
o(&l.opts)
}
zapConfig := zap.NewProductionConfig()
if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
zapConfig = zconfig
}
if zcconfig, ok := l.opts.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok {
zapConfig.EncoderConfig = zcconfig
}
writer, ok := l.opts.Context.Value(writerKey{}).(io.Writer)
if !ok {
writer = os.Stdout
}
skip, ok := l.opts.Context.Value(callerSkipKey{}).(int)
if !ok || skip < 1 {
skip = 1
}
// Set log Level if not default
zapConfig.Level = zap.NewAtomicLevel()
if l.opts.Level != logger.InfoLevel {
zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
}
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logCore := zapcore.NewCore(
zapcore.NewConsoleEncoder(zapConfig.EncoderConfig),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(writer)),
zapConfig.Level)
log := zap.New(logCore, zap.AddCaller(), zap.AddCallerSkip(skip), zap.AddStacktrace(zap.DPanicLevel))
//log, err := zapConfig.Build(zap.AddCallerSkip(skip))
//if err != nil {
// return err
//}
// Adding seed fields if exist
if l.opts.Fields != nil {
data := []zap.Field{}
for k, v := range l.opts.Fields {
data = append(data, zap.Any(k, v))
}
log = log.With(data...)
}
// Adding namespace
if namespace, ok := l.opts.Context.Value(namespaceKey{}).(string); ok {
log = log.With(zap.Namespace(namespace))
}
// defer log.Sync() ??
l.cfg = zapConfig
l.zap = log
l.fields = make(map[string]interface{})
return nil
}
func (l *zaplog) Fields(fields logger.LogFields) logger.Logger {
l.Lock()
nfields := make(map[string]interface{}, len(l.fields))
for k, v := range l.fields {
nfields[k] = v
}
l.Unlock()
for k, v := range fields {
nfields[k] = v
}
data := make([]zap.Field, 0, len(nfields))
for k, v := range fields {
data = append(data, zap.Any(k, v))
}
zl := &zaplog{
cfg: l.cfg,
zap: l.zap,
opts: l.opts,
fields: nfields,
}
return zl
}
func (l *zaplog) Error(err error) logger.Logger {
return l.Fields(map[string]interface{}{"error": err})
}
func (l *zaplog) Log(level logger.Level, args ...interface{}) {
l.RLock()
data := make([]zap.Field, 0, len(l.fields))
for k, v := range l.fields {
data = append(data, zap.Any(k, v))
}
l.RUnlock()
lvl := loggerToZapLevel(level)
msg := fmt.Sprint(args...)
switch lvl {
case zap.DebugLevel:
l.zap.Debug(msg, data...)
case zap.InfoLevel:
l.zap.Info(msg, data...)
case zap.WarnLevel:
l.zap.Warn(msg, data...)
case zap.ErrorLevel:
l.zap.Error(msg, data...)
case zap.FatalLevel:
l.zap.Fatal(msg, data...)
}
}
func (l *zaplog) Logf(level logger.Level, format string, args ...interface{}) {
l.RLock()
data := make([]zap.Field, 0, len(l.fields))
for k, v := range l.fields {
data = append(data, zap.Any(k, v))
}
l.RUnlock()
lvl := loggerToZapLevel(level)
msg := fmt.Sprintf(format, args...)
switch lvl {
case zap.DebugLevel:
l.zap.Debug(msg, data...)
case zap.InfoLevel:
l.zap.Info(msg, data...)
case zap.WarnLevel:
l.zap.Warn(msg, data...)
case zap.ErrorLevel:
l.zap.Error(msg, data...)
case zap.FatalLevel:
l.zap.Fatal(msg, data...)
}
}
func (l *zaplog) String() string {
return "zap"
}
func (l *zaplog) Options() logger.Options {
return l.opts
}
// New builds a new logger based on options
func NewLogger(opts ...logger.Option) (logger.Logger, error) {
// Default options
options := logger.Options{
Level: logger.InfoLevel,
Fields: make(map[string]interface{}),
Out: os.Stderr,
Context: context.Background(),
}
l := &zaplog{opts: options}
if err := l.Init(opts...); err != nil {
return nil, err
}
return l, nil
}
func loggerToZapLevel(level logger.Level) zapcore.Level {
switch level {
case logger.TraceLevel, logger.DebugLevel:
return zap.DebugLevel
case logger.InfoLevel:
return zap.InfoLevel
case logger.WarnLevel:
return zap.WarnLevel
case logger.ErrorLevel:
return zap.ErrorLevel
case logger.FatalLevel:
return zap.FatalLevel
default:
return zap.InfoLevel
}
}
func zapToLoggerLevel(level zapcore.Level) logger.Level {
switch level {
case zap.DebugLevel:
return logger.DebugLevel
case zap.InfoLevel:
return logger.InfoLevel
case zap.WarnLevel:
return logger.WarnLevel
case zap.ErrorLevel:
return logger.ErrorLevel
case zap.FatalLevel:
return logger.FatalLevel
default:
return logger.InfoLevel
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linxing_3/youye-core.git
git@gitee.com:linxing_3/youye-core.git
linxing_3
youye-core
youye-core
v0.0.1-202406121004

搜索帮助