micro-account/pkg/common/log/log.go

142 lines
3.8 KiB
Go
Raw Normal View History

2025-02-20 08:18:23 +00:00
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package log
import (
"fmt"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var zapFakeLog *ZapLoggerFake
type DuInfo struct {
Config *Config `yaml:"logger"`
}
type Config struct {
LumberjackConfig *lumberjack.Logger `yaml:"lumberjack-config"`
ZapConfig *zap.Config `yaml:"zap-config"`
CallerSkip int
}
type ZapLoggerFake struct {
logger *zap.Logger
}
// InitLogger use for init logger by @conf
func InitLogger(conf *Config) {
var (
_ *zap.Logger
config = &Config{}
)
if conf == nil || conf.ZapConfig == nil {
zapLoggerEncoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
config.ZapConfig = &zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Encoding: "console",
EncoderConfig: zapLoggerEncoderConfig,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
} else {
config.ZapConfig = conf.ZapConfig
}
if conf != nil {
config.CallerSkip = conf.CallerSkip
}
if config.CallerSkip == 0 { //因为包装了两层所以设置3次
config.CallerSkip = 3
}
var zapLogger *zap.Logger
if conf == nil || conf.LumberjackConfig == nil {
zapLogger, _ = config.ZapConfig.Build(zap.AddCaller(), zap.AddCallerSkip(config.CallerSkip))
} else {
config.LumberjackConfig = conf.LumberjackConfig
zapLogger = initZapLoggerWithSyncer(config)
}
zapFakeLog = &ZapLoggerFake{logger: zapLogger}
return
}
func GetFakeLogger() *ZapLoggerFake {
return zapFakeLog
}
func GetZapLog() *zap.Logger {
return zapFakeLog.logger
}
// initZapLoggerWithSyncer init zap Logger with syncer
func initZapLoggerWithSyncer(conf *Config) *zap.Logger {
var fields []zapcore.Field
if len(conf.ZapConfig.InitialFields) > 0 {
for key, value := range conf.ZapConfig.InitialFields {
fields = append(fields, zap.Any(key, value))
}
}
fmt.Println("配置lumber", conf.LumberjackConfig)
core := zapcore.NewCore(
conf.getEncoder(),
conf.getLogWriter(),
zap.NewAtomicLevelAt(conf.ZapConfig.Level.Level()),
)
if len(fields) >= 1 {
core = core.With(fields)
}
return zap.New(core, zap.AddCaller(), zap.AddCallerSkip(conf.CallerSkip))
}
// getEncoder get encoder by config, zapcore support json and console encoder
func (c *Config) getEncoder() zapcore.Encoder {
if c.ZapConfig.Encoding == "json" {
return zapcore.NewJSONEncoder(c.ZapConfig.EncoderConfig)
} else if c.ZapConfig.Encoding == "console" {
return zapcore.NewConsoleEncoder(c.ZapConfig.EncoderConfig)
}
return nil
}
// getLogWriter get Lumberjack writer by LumberjackConfig
func (c *Config) getLogWriter() zapcore.WriteSyncer {
return zapcore.AddSync(c.LumberjackConfig)
}