fix: log 包日志配置无效问题修复

This commit is contained in:
kercylan98 2023-11-29 18:34:29 +08:00
parent 7f3fc89c9d
commit c6b929afe8
3 changed files with 15 additions and 17 deletions

View File

@ -4,7 +4,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2" "gopkg.in/natefinch/lumberjack.v2"
"os"
) )
type Encoder struct { type Encoder struct {
@ -29,23 +28,10 @@ func (slf *Encoder) Build(options ...LoggerOption) *Minotaur {
panic(err) panic(err)
} }
options = append([]LoggerOption{zap.AddCaller(), zap.AddCallerSkip(1)}, options...) options = append([]LoggerOption{zap.AddCaller(), zap.AddCallerSkip(1)}, options...)
l = l.WithOptions(options...) options = append(options, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
if len(slf.cores) == 0 { return zapcore.NewTee(append(slf.cores, core)...)
// stdout、stderr不使用 lumberjack.Logger
slf.cores = append(slf.cores, zapcore.NewCore(
slf.e,
zapcore.Lock(os.Stdout),
zapcore.InfoLevel,
))
slf.cores = append(slf.cores, zapcore.NewCore(
slf.e,
zapcore.Lock(os.Stderr),
zapcore.ErrorLevel,
))
}
l = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewTee(slf.cores...)
})) }))
l = l.WithOptions(options...)
return &Minotaur{ return &Minotaur{
Logger: l, Logger: l,
Sugared: l.Sugar(), Sugared: l.Sugar(),

View File

@ -46,6 +46,8 @@ func Default(opts ...Option) *Encoder {
EncodeDuration: zapcore.StringDurationEncoder, EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder, EncodeCaller: zapcore.ShortCallerEncoder,
}, },
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
} }
// 应用选项 // 应用选项

View File

@ -6,6 +6,11 @@ import (
) )
var launchTime = time.Now() var launchTime = time.Now()
var pid int
func init() {
pid = os.Getpid()
}
// LaunchTime 获取程序启动时间 // LaunchTime 获取程序启动时间
func LaunchTime() time.Time { func LaunchTime() time.Time {
@ -16,3 +21,8 @@ func LaunchTime() time.Time {
func Hostname() string { func Hostname() string {
return os.Getenv("HOSTNAME") return os.Getenv("HOSTNAME")
} }
// PID 获取进程 PID
func PID() int {
return pid
}