From c6b929afe8dec07e210159b34a21ebb086dda8e5 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 29 Nov 2023 18:34:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20log=20=E5=8C=85=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/log/encoder.go | 20 +++----------------- utils/log/options.go | 2 ++ utils/super/super.go | 10 ++++++++++ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/utils/log/encoder.go b/utils/log/encoder.go index 442debf..218cc49 100644 --- a/utils/log/encoder.go +++ b/utils/log/encoder.go @@ -4,7 +4,6 @@ import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" - "os" ) type Encoder struct { @@ -29,23 +28,10 @@ func (slf *Encoder) Build(options ...LoggerOption) *Minotaur { panic(err) } options = append([]LoggerOption{zap.AddCaller(), zap.AddCallerSkip(1)}, options...) - l = l.WithOptions(options...) - if len(slf.cores) == 0 { - // 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...) + options = append(options, zap.WrapCore(func(core zapcore.Core) zapcore.Core { + return zapcore.NewTee(append(slf.cores, core)...) })) + l = l.WithOptions(options...) return &Minotaur{ Logger: l, Sugared: l.Sugar(), diff --git a/utils/log/options.go b/utils/log/options.go index 2938b28..37dd5a6 100644 --- a/utils/log/options.go +++ b/utils/log/options.go @@ -46,6 +46,8 @@ func Default(opts ...Option) *Encoder { EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }, + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, } // 应用选项 diff --git a/utils/super/super.go b/utils/super/super.go index 048df86..adcdbbf 100644 --- a/utils/super/super.go +++ b/utils/super/super.go @@ -6,6 +6,11 @@ import ( ) var launchTime = time.Now() +var pid int + +func init() { + pid = os.Getpid() +} // LaunchTime 获取程序启动时间 func LaunchTime() time.Time { @@ -16,3 +21,8 @@ func LaunchTime() time.Time { func Hostname() string { return os.Getenv("HOSTNAME") } + +// PID 获取进程 PID +func PID() int { + return pid +}