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

[BUG] - Stringer types don't log the string representation #29

Closed
betamos opened this issue Dec 29, 2023 · 1 comment · Fixed by #34
Closed

[BUG] - Stringer types don't log the string representation #29

betamos opened this issue Dec 29, 2023 · 1 comment · Fixed by #34

Comments

@betamos
Copy link

betamos commented Dec 29, 2023

Describe the bug
I have types with useful string representations:

type ByteCount int // Note this could be a struct as well.

func (b ByteCount) String() string {
  // Returns a human readable string like "3.4MB"
}

With slog.TextHandler, their string representation is printed. With slog.JSONHandler, it's ignored (which is great).

However, with devslog they are not shown at all:

To Reproduce

slog.Info("stats", "mem", ByteCount(9503159))

Results in

 INFO  stats
# mem   : 9503159

Expected behavior

  1. Substitute the value with the string representation, or...
  2. Print the string representation as extra information:
 INFO  stats
# mem   : 9503159   "9.5MB"

Additional context

There are some variations to consider:

  • The type is a struct. Should it be unpacked if there is a String method?
  • The type also implements LogValuer, which should probably take precedence.
@david-littlefarmer david-littlefarmer mentioned this issue Jan 31, 2024
4 tasks
@david-littlefarmer
Copy link
Collaborator

Sorry for long silence.
I like the second proposal, here is solution: #32
LogValuer always take precedence.
I don't have solution for structs, yet.

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

Successfully merging a pull request may close this issue.

2 participants