Skip to content

Commit

Permalink
fix: log 包日志配置无效问题修复
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Nov 29, 2023
1 parent 7f3fc89 commit c6b929a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
20 changes: 3 additions & 17 deletions utils/log/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
)

type Encoder struct {
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions utils/log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func Default(opts ...Option) *Encoder {
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}

// 应用选项
Expand Down
10 changes: 10 additions & 0 deletions utils/super/super.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
)

var launchTime = time.Now()
var pid int

func init() {
pid = os.Getpid()
}

// LaunchTime 获取程序启动时间
func LaunchTime() time.Time {
Expand All @@ -16,3 +21,8 @@ func LaunchTime() time.Time {
func Hostname() string {
return os.Getenv("HOSTNAME")
}

// PID 获取进程 PID
func PID() int {
return pid
}

0 comments on commit c6b929a

Please sign in to comment.