Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In multiple zapcore examples, how do I use the LevelEnablerFunc filter to make the debug file log only the Debug level and the Info file record only the INFO level log #26

Closed
hiPsyDuck opened this issue Apr 20, 2023 · 5 comments
Assignees

Comments

@hiPsyDuck
Copy link

In multiple zapcore examples, how do I use the LevelEnablerFunc filter to make the Info file record only the INFO level log,and also, other files also correspond to the level

@rogerogers
Copy link
Collaborator

rogerogers commented Apr 20, 2023

Just following the readme instructions.
It seems there is a missing line that needs to be added.
hlog.SetLogger(logger) after logger init.

@hiPsyDuck hiPsyDuck reopened this Apr 20, 2023
@hiPsyDuck
Copy link
Author

Just following the readme instructions. It seems there is a missing line that needs to be added. hlog.SetLogger(logger) after logger init.

My code follows the demo completely and adds hlog.SetLogger, but the info.log file contains both info and error level logs. I hope info.log only contains logs of info level, and debug.log only contains logs of debug level.

@rogerogers
Copy link
Collaborator

Here we use AtomicLevel to implement the SetLevel method of hlog. If you don't need the SetLevel method, you can use logger like this.

logger := hertzzap.NewLogger(
	hertzzap.WithZapOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		return zapcore.NewTee(
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/debug.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.DebugLevel
				}),
			),
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/info.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.InfoLevel
				}),
			),
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/warn.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.WarnLevel
				}),
			),
		)
	}),
	),
)
defer logger.Sync()

hlog.SetLogger(logger)

@hiPsyDuck
Copy link
Author

这里我们使用AtomicLevel来实现hlog的SetLevel方法。如果不需要 SetLevel 方法,可以像这样使用 logger。

logger := hertzzap.NewLogger(
	hertzzap.WithZapOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		return zapcore.NewTee(
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/debug.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.DebugLevel
				}),
			),
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/info.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.InfoLevel
				}),
			),
			zapcore.NewCore(
				zapcore.NewJSONEncoder(humanEncoderConfig()), getWriteSyncer("log/warn.log"), zap.LevelEnablerFunc(func(level zapcore.Level) bool {
					return level == zap.WarnLevel
				}),
			),
		)
	}),
	),
)
defer logger.Sync()

hlog.SetLogger(logger)

thanks, It works

@rogerogers
Copy link
Collaborator

#27

The new version fixed this issue, and you can also refer to the latest version readme for usage instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants