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

Confused with the slogr level #306

Open
zwpaper opened this issue Jul 26, 2024 · 4 comments
Open

Confused with the slogr level #306

zwpaper opened this issue Jul 26, 2024 · 4 comments
Assignees

Comments

@zwpaper
Copy link

zwpaper commented Jul 26, 2024

as from the comment:

logr/slogr.go

Lines 55 to 58 in 26eb1a2

// slog.New(ToSlogHandler(logger)).Debug(...) -> logger.GetSink().Info(level=4, ...)
// slog.New(ToSlogHandler(logger)).Warning(...) -> logger.GetSink().Info(level=0, ...)
// slog.New(ToSlogHandler(logger)).Info(...) -> logger.GetSink().Info(level=0, ...)
// slog.New(ToSlogHandler(logger.V(4))).Info(...) -> logger.GetSink().Info(level=4, ...)

it seems that we have only 0-4 that could be used as non-error levels, and 0 as the default(log.Info() without V()).

this makes all the non-error as default printed levels, which leads to printing all Debug levels.

if I increase the level to 1-4, I will lose all the default Info() level logs.

this really confused me, may I ask how you use the level to switch different level detailed logs?

@pohly
Copy link
Contributor

pohly commented Jul 26, 2024

I'm not sure I am following. Are you trying to configure a logr backend so that it prints slog Warning calls, but not slog Info calls?

That is indeed not possible because the level range from logr is more limited than the one from slog. slog supports levels that go towards negative infinity (= less important) and towards positive infinity (= more important). logr only has "less important than info/error".

@zwpaper
Copy link
Author

zwpaper commented Jul 26, 2024

Let me detail my case more,

when I am logging, I have the following choices:

  1. Error() when error happened
  2. Info() when it should always be printed
  3. Debug, or some leveled choice, when it should only be printed when manually enabled

when doing Debug logs, the Info() logs should also be printed, right?


In the slogr case I read, It matches the following levels:

  1. logr.Error(): 5
  2. logr.Info(): 0

I can only use 1-4 for other levels logging,
but when I increase the enabled level, the default Info() logs will be gone

@zwpaper
Copy link
Author

zwpaper commented Jul 26, 2024

I have tried locally and believed that I found the correct usage.

we should pass the level to slog using 0-level, and this will let us use the logr like the original way.

  1. Info(): always logs all the info logs
  2. Error(): always print all leveled logs
  3. V(N): logs as Debug and controlled by the level we set

just like this, if I set the level=-4:

Jul 26 16:37:34.595 INF draft-go/main.go:28 hello world
Jul 26 16:37:34.596 ERR draft-go/main.go:29 hello world, err
Jul 26 16:37:34.596 INF draft-go/main.go:31 hello level level=0
Jul 26 16:37:34.596 DBG+3 draft-go/main.go:31 hello level level=1
Jul 26 16:37:34.596 DBG+2 draft-go/main.go:31 hello level level=2
Jul 26 16:37:34.596 DBG+1 draft-go/main.go:31 hello level level=3
Jul 26 16:37:34.596 DBG draft-go/main.go:31 hello level level=4
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=0
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=1
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=2
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=3
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=4
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=5
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=6
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=7
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=8
Jul 26 16:37:34.596 ERR draft-go/main.go:34 hello level err level=9

is that right? if this is the correct usage, it may be worth raising a PR to add an example and docs, and I am glad to help

@pohly
Copy link
Contributor

pohly commented Jul 26, 2024

I'm getting lost when you say Error without being specific whether that is a logr or slog method. I am also not sure I understand what "level we set" refers to.

A PR with example and docs might help to clarify, so yes, please create one.

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

3 participants