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

FilterKey log filtering is not effective #3554

Closed
0x00-ketsu opened this issue Feb 25, 2025 · 1 comment
Closed

FilterKey log filtering is not effective #3554

0x00-ketsu opened this issue Feb 25, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@0x00-ketsu
Copy link

What happened:

设置 log.FilterKey 后调用 log.Info 时,log.FilterKey 无效,但是通过 log.Log() 指定日志级别会生效

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

  1. kratos new helloworld

  2. 编辑 helloworld/cmd/helloworld/main.go

    logger := log.With(log.NewStdLogger(os.Stdout),
    	"ts", log.DefaultTimestamp,
    	"caller", log.DefaultCaller,
    	"service.id", id,
    	"service.name", Name,
    	"service.version", Version,
    	"trace.id", tracing.TraceID(),
    	"span.id", tracing.SpanID(),
    )
    l := log.NewHelper(
    	log.NewFilter(logger, log.FilterKey("password")),
    )
    fmt.Println("-----------------")
    l.Info("helloworld", "password", "123456") // 密码未被替换
    fmt.Println("-----------------")
    l.Log(log.LevelInfo, "password", "123456") // 密码被替换为 ***
    fmt.Println("-----------------")
Image

Anything else we need to know?:

Environment:

  • Kratos version (use kratos -v):
    kratos version v2.8.3

  • Go version (use go version):
    go version go1.24.0 darwin/arm64

  • OS (e.g: cat /etc/os-release):
    Darwin

  • Others:

@0x00-ketsu 0x00-ketsu added the bug Something isn't working label Feb 25, 2025
@kratos-ci-bot kratos-ci-bot changed the title FilterKey日志过滤没有生效 FilterKey log filtering is not effective Feb 25, 2025
Copy link

dosubot bot commented Feb 25, 2025

Hello @0x00-ketsu! I'm here to assist you with any bugs, questions, or contributions you may have. Let me know how I can help with the issue you've raised.

The issue you're experiencing with log.FilterKey not working as expected when using log.Info but working with log.Log is likely due to how these methods handle filters differently.

In the Kratos framework, log.Info is a convenience method for logging at the INFO level, while log.Log allows you to specify the log level explicitly. The log.FilterKey function is designed to filter or mask specific keys in log entries, but its behavior might differ based on how the logging methods are implemented internally.

To address this, you can use log.Log with the INFO level explicitly, as you've done, to ensure that the FilterKey is applied correctly:

l.Log(log.LevelInfo, "password", "123456") // This will mask the password

This approach ensures that the FilterKey functionality is applied as expected. If you prefer using log.Info, you might need to check if there are any updates or patches in newer versions of Kratos that address this inconsistency.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant