News for hackers. Stuff that matters.
Re:Structured Logging with Slog by grose on Tuesday August 22, 2023 @02:40PM (#37226893)
It’s not without precedence, for example: https://pkg.go.dev/strings#NewReplacer

I don’t mind it. You can use LogAttrs if you want to be explicit.

Although I do wonder if there’s anything tricky with the type system that is preventing something like this from being supported: https://go.dev/play/p/_YV7sYdnZ5V

Re:Structured Logging with Slog by gdprrrr on Tuesday August 22, 2023 @03:03PM (#37227143)
Is ordering of the keys guaranteed to be the same as in the literal?
Re:Structured Logging with Slog by grose on Tuesday August 22, 2023 @03:15PM (#37227302)
That’s a good point. I think it would be random without some sorting shenanigans.
Re:Structured Logging with Slog by kiitos on Wednesday August 23, 2023 @07:44AM (#37234394)
Order should be preserved up to the Handler. Some Handlers e.g. JSONHandler produce output with key order explicitly undefined.
Re:Structured Logging with Slog by ben0x539 on Tuesday August 22, 2023 @04:16PM (#37227981)
I think go loggers have tried to move away from passing log entries as maps for performance reasons.
Re:Structured Logging with Slog by mlhpdx on Tuesday August 22, 2023 @07:32PM (#37229950)
That seems like a problem that should be solved. Logging structured data is a very basic expectation.
Re:Structured Logging with Slog by infogulch on Tuesday August 22, 2023 @07:41PM (#37230017)
This is structured logging. Stubbornly insisting that "structured logging" === "map" is dumb and ignores a large fraction of use cases where performance matters.
Re:Structured Logging with Slog by mlhpdx on Tuesday August 22, 2023 @10:45PM (#37231297)
Are there languages that solve the “performance“ problem with maps? In fact, isn’t Go one of them?
Re:Structured Logging with Slog by infogulch on Tuesday August 22, 2023 @11:51PM (#37231637)
In this use case using maps doesn't solve any problem, requires at least one allocation, and requires hashing each key. This is not even an interesting discussion.
Re:Structured Logging with Slog by mlhpdx on Wednesday August 23, 2023 @12:23AM (#37231858)
I can see the case for flat logging, aka. key value logging, as an optimization for very, very performance critical code that needs to emit string logs. That however, isn’t mainstream in my experience. The far, far more common case is logging in code with complex data-driven behaviors where the data is structured (more than one level, not flat) and where forensic debugging via logs is a critical activity. If that’s not your world, you should only be interested in it if you’re interested in the community at large. If you’re not, that’s cool.
Re:Structured Logging with Slog by 59nadir on Wednesday August 23, 2023 @02:30AM (#37232482)
I think you seem to be arguing that the end result should be a "map"-like structure, whereas the other commenter is arguing about the interface to the logging library not being based on maps. These are not the same and taking maps in the interface is likely to incur allocations, yes. Having to specify your key-value pairs without maps is the only downside to not taking fully constructed maps in the interface.
Re:Structured Logging with Slog by mlhpdx on Wednesday August 23, 2023 @12:20PM (#37238360)
Adding the application/component code to do ”good logging” is tedious. Friction in the interface decreases the probability it will be done well, and consistently. I think the interface matters, and while it’s a second order problem it does impact system quality in the long run.

Just my opinions here. I don’t question the value of slog as it sits, just could have been better for the community at large is all.

Re:Structured Logging with Slog by yencabulator on Tuesday August 22, 2023 @07:50PM (#37230102)
Avoiding the map allocation & construction cost is way harder than avoiding the use of a map, like zap and slog.LogAttrs do.
Re:Structured Logging with Slog by masklinn on Wednesday August 23, 2023 @02:19AM (#37232431)
You don’t need LogAttrs to pass in Attr entries, it should work fine with normal functions

The reason it doesn’t use maps is that maps are significantly slower, TFA has an entire section on performances.

However if you prefer that interface and don’t mind the performance hit, nothing precludes writing your own Logger (it’s just a façade over the Handler interface) and taking maps.