Skip to content

Commit

Permalink
opt: prepend [gnet] prefix to every log entry
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed May 16, 2023
1 parent a9c2638 commit 650f731
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ func TestServerOptionsCheck(t *testing.T) {
assert.EqualError(t, err, gerr.ErrTooManyEventLoopThreads.Error(), "error returned with LockOSThread option")
}

func TestStop(t *testing.T) {
func TestStopServer(t *testing.T) {
testStop(t, "tcp", ":9997")
}

Expand Down
74 changes: 59 additions & 15 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"strconv"

"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
Expand All @@ -68,22 +69,22 @@ type Level = zapcore.Level
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
DebugLevel = zapcore.DebugLevel
// InfoLevel is the default logging priority.
InfoLevel
InfoLevel = zapcore.InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
WarnLevel = zapcore.WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
ErrorLevel = zapcore.ErrorLevel
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel
DPanicLevel = zapcore.DPanicLevel
// PanicLevel logs a message, then panics.
PanicLevel
PanicLevel = zapcore.PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel
FatalLevel = zapcore.FatalLevel
)

func init() {
Expand All @@ -105,19 +106,62 @@ func init() {
panic("invalid GNET_LOGGING_FILE, " + err.Error())
}
} else {
cfg := zap.NewDevelopmentConfig()
cfg.Level = zap.NewAtomicLevelAt(defaultLoggingLevel)
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
zapLogger, _ := cfg.Build()
core := zapcore.NewCore(getDevEncoder(), zapcore.Lock(os.Stdout), defaultLoggingLevel)
zapLogger := zap.New(core,
zap.Development(),
zap.AddCaller(),
zap.AddStacktrace(ErrorLevel),
zap.ErrorOutput(zapcore.Lock(os.Stderr)))
defaultLogger = zapLogger.Sugar()
}
}

func getEncoder() zapcore.Encoder {
type prefixEncoder struct {
zapcore.Encoder

prefix string
bufPool buffer.Pool
}

func (e *prefixEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
buf := e.bufPool.Get()

buf.AppendString(e.prefix)
buf.AppendString(" ")

logEntry, err := e.Encoder.EncodeEntry(entry, fields)
if err != nil {
return nil, err
}

_, err = buf.Write(logEntry.Bytes())
if err != nil {
return nil, err
}

return buf, nil
}

func getDevEncoder() zapcore.Encoder {
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return &prefixEncoder{
Encoder: zapcore.NewConsoleEncoder(encoderConfig),
prefix: "[gnet]",
bufPool: buffer.NewPool(),
}
}

func getProdEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
return &prefixEncoder{
Encoder: zapcore.NewConsoleEncoder(encoderConfig),
prefix: "[gnet]",
bufPool: buffer.NewPool(),
}
}

// GetDefaultLogger returns the default logger.
Expand All @@ -144,15 +188,15 @@ func CreateLoggerAsLocalFile(localFilePath string, logLevel Level) (logger Logge
MaxAge: 15, // days
}

encoder := getEncoder()
encoder := getProdEncoder()
ws := zapcore.AddSync(lumberJackLogger)
zapcore.Lock(ws)

levelEnabler := zap.LevelEnablerFunc(func(level Level) bool {
return level >= logLevel
})
core := zapcore.NewCore(encoder, ws, levelEnabler)
zapLogger := zap.New(core, zap.AddCaller())
zapLogger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(ErrorLevel))
logger = zapLogger.Sugar()
flush = zapLogger.Sync
return
Expand Down

0 comments on commit 650f731

Please sign in to comment.