add JRZQ09J8、FLXGDEA8、FLXGDEA9、JRZQ1D09
add external_services log
This commit is contained in:
147
internal/shared/logger/factory.go
Normal file
147
internal/shared/logger/factory.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// LoggerFactory 日志器工厂 - 基于 Zap 官方推荐
|
||||
type LoggerFactory struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
// NewLoggerFactory 创建日志器工厂
|
||||
func NewLoggerFactory(config Config) *LoggerFactory {
|
||||
return &LoggerFactory{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateLogger 创建普通日志器
|
||||
func (f *LoggerFactory) CreateLogger() (Logger, error) {
|
||||
return NewLogger(f.config)
|
||||
}
|
||||
|
||||
// CreateProductionLogger 创建生产环境日志器 - 使用 Zap 官方推荐
|
||||
func (f *LoggerFactory) CreateProductionLogger() (*zap.Logger, error) {
|
||||
// 使用 Zap 官方的生产环境预设
|
||||
logger, err := zap.NewProduction(
|
||||
zap.AddCaller(),
|
||||
zap.AddCallerSkip(1),
|
||||
zap.AddStacktrace(zapcore.ErrorLevel),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 如果配置为文件输出,需要手动设置 Core
|
||||
if f.config.Output == "file" {
|
||||
writeSyncer, err := createFileWriteSyncer(f.config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建新的 Core 并替换
|
||||
encoder := getEncoder(f.config.Format, f.config)
|
||||
level := getLogLevel(f.config.Level)
|
||||
core := zapcore.NewCore(encoder, writeSyncer, level)
|
||||
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(zapcore.ErrorLevel))
|
||||
}
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
// CreateDevelopmentLogger 创建开发环境日志器 - 使用 Zap 官方推荐
|
||||
func (f *LoggerFactory) CreateDevelopmentLogger() (*zap.Logger, error) {
|
||||
// 使用 Zap 官方的开发环境预设
|
||||
logger, err := zap.NewDevelopment(
|
||||
zap.AddCaller(),
|
||||
zap.AddCallerSkip(1),
|
||||
zap.AddStacktrace(zapcore.ErrorLevel),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 如果配置为文件输出,需要手动设置 Core
|
||||
if f.config.Output == "file" {
|
||||
writeSyncer, err := createFileWriteSyncer(f.config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建新的 Core 并替换
|
||||
encoder := getEncoder(f.config.Format, f.config)
|
||||
level := getLogLevel(f.config.Level)
|
||||
core := zapcore.NewCore(encoder, writeSyncer, level)
|
||||
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(zapcore.ErrorLevel))
|
||||
}
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
// CreateCustomLogger 创建自定义配置日志器
|
||||
func (f *LoggerFactory) CreateCustomLogger() (*zap.Logger, error) {
|
||||
// 根据环境选择预设
|
||||
if f.config.Development {
|
||||
return f.CreateDevelopmentLogger()
|
||||
}
|
||||
return f.CreateProductionLogger()
|
||||
}
|
||||
|
||||
// CreateLoggerByEnvironment 根据环境创建合适的日志器
|
||||
func (f *LoggerFactory) CreateLoggerByEnvironment() (*zap.Logger, error) {
|
||||
if f.config.Development {
|
||||
return f.CreateDevelopmentLogger()
|
||||
}
|
||||
return f.CreateProductionLogger()
|
||||
}
|
||||
|
||||
// CreateLoggerWithOptions 使用选项模式创建日志器
|
||||
func (f *LoggerFactory) CreateLoggerWithOptions(options ...LoggerOption) (*zap.Logger, error) {
|
||||
// 应用选项
|
||||
for _, option := range options {
|
||||
option(&f.config)
|
||||
}
|
||||
|
||||
// 创建日志器
|
||||
return f.CreateLoggerByEnvironment()
|
||||
}
|
||||
|
||||
// LoggerOption 日志器选项函数
|
||||
type LoggerOption func(*Config)
|
||||
|
||||
// WithLevel 设置日志级别
|
||||
func WithLevel(level string) LoggerOption {
|
||||
return func(config *Config) {
|
||||
config.Level = level
|
||||
}
|
||||
}
|
||||
|
||||
// WithFormat 设置日志格式
|
||||
func WithFormat(format string) LoggerOption {
|
||||
return func(config *Config) {
|
||||
config.Format = format
|
||||
}
|
||||
}
|
||||
|
||||
// WithOutput 设置输出目标
|
||||
func WithOutput(output string) LoggerOption {
|
||||
return func(config *Config) {
|
||||
config.Output = output
|
||||
}
|
||||
}
|
||||
|
||||
// WithDevelopment 设置是否为开发环境
|
||||
func WithDevelopment(development bool) LoggerOption {
|
||||
return func(config *Config) {
|
||||
config.Development = development
|
||||
}
|
||||
}
|
||||
|
||||
// WithColor 设置是否使用彩色输出
|
||||
func WithColor(useColor bool) LoggerOption {
|
||||
return func(config *Config) {
|
||||
config.UseColor = useColor
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user