News for hackers. Stuff that matters.
Re:Go 1.21 Released by kiitos on Tuesday August 08, 2023 @03:06PM (#37053974)
Even after seeing the API?
Re:Go 1.21 Released by cube2222 on Tuesday August 08, 2023 @04:16PM (#37054895)
Could you expand what you mean?

When skimming it I mostly saw the common API structure that I usually see in libraries / write myself when I need a logger, so I generally welcome the standardization.

Re:Go 1.21 Released by kiitos on Tuesday August 08, 2023 @05:43PM (#37055848)
slog defines the concepts of

- Logger -- created by New, accepting a Handler, providing a fixed set of level-based logging methods, asserting concepts of Attrs and Groups, not parameterizable by consumers

- Handler -- with two default implementations, asserting concepts of Attrs Groups and Records, parameterizable by consumers as long as they follow the semantics defined by the interface

- Attr -- arbitrary concept that maps to a k/v pair in a log record

- Group -- arbitrary concept that namespaces a set of k/v pairs in a log record

- Record -- arbitrary concept that requires a timestamp (expensive to compute), a level (one of a specifically defined enum which cannot be changed), and a PC stack pointer (obvious issues there)

I've never seen a logging package which meets these requirements.

Re:Go 1.21 Released by metaltyphoon on Tuesday August 08, 2023 @07:09PM (#37056674)
If you meant on other ecosystems Microsoft.Extensions.Logging
Re:Go 1.21 Released by cube2222 on Tuesday August 08, 2023 @07:50PM (#37057016)
What else would you expect from a structured logging package?

To me it absolutely makes sense as the default and standard for 99% of applications, and the API isn't much unlike something like Zap[0] (a popular Go structured logger).

The attributes aren't an "arbitrary" concept, they're a completely normal concept for structured loggers. Groups are maybe less standard, but reasonable nevertheless. The timestamp is not required - the documentation specifies it can be left as the zero value and shall be ignored in that case.

I'm not sure if you're aware that this is specifically a structured logging package. There already is a "simple" logging package[1] in the stdlib, has been there for ages, and isn't particularly fast either to my knowledge. If you want really fast you take a library (which would also make sure to optimize allocations heavily).

[0]: https://pkg.go.dev/go.uber.org/zap

[1]: https://pkg.go.dev/log

Re:Go 1.21 Released by kiitos on Tuesday August 08, 2023 @07:54PM (#37057051)
I'm intimately familiar with structured logging.

The domain concepts defined by slog are unquestionably abnormal.

From a structured logging package I would expect a far simpler Logger API with a Log method something like

    Log(pairs ...KeyValuePair) error
or maybe

    Log(r Record) error
and no concept of Attrs or Groups or (explicit) Handlers.
Re:Go 1.21 Released by cube2222 on Tuesday August 08, 2023 @08:05PM (#37057133)
> An Attr is a key-value pair.

So s/KeyValuePair/Attr/g?

Re varargs vs record, varargs are usually used in Go because they're very ergonomic to write inline, so they work very nice with loggers.

Group sounds like a fairly arbitrary concept, I agree, but still very reasonable for something that's supposed to standardize things. It would totally make sense for e.g. different libraries to each inject their own group with key-values into the context.

I'm not sure what's the problem with Handlers? This is supposed to be a standard library package that is setting... well, standards that other libraries will adhere to. Handlers let you plug your own output formats.

The package is not supposed to be "as bare bones as possible". Just "good default others will be able to integrate and compose with".

I've used my fair share of structured loggers too, and this really is par for the course and a completely reasonable set of things to include in a structured logging package.

---

It's worth noting that the previously-mentioned Zap, which is probably the most popular structured logging library in go right now, contains all the same concepts, just differently named:

Attr -> Field

Group -> Namespace

Record -> Entry

Handler -> Encoder/Writer

Re:Go 1.21 Released by kiitos on Tuesday August 08, 2023 @09:05PM (#37057541)
There is no reason to distinguish a Handler from a Logger. A Handler is just a Logger which has a side effect, nothing more.

There is no reason to distinguish an Attr or Group or Record from a (set of) KeyValuePairs. They're all the same thing.

And I don't agree that Zap is the most popular structured logging library. It's one of many, none are the clear winner.

But, whatever.

Re:Go 1.21 Released by morelisp on Wednesday August 09, 2023 @03:01AM (#37059509)
> There is no reason to distinguish a Handler from a Logger.

A Handler is an abstract type, a Logger is a concrete one. A Logger lets you have a large user surface (and without virtual calls); and Handler, a small implementer surface.

The concepts also map fairly directly to logrus and zerolog (which has an absolutely enormous surface to avoid boxing).

Re:Go 1.21 Released by kiitos on Wednesday August 09, 2023 @10:46AM (#37063508)
If slog needs to avoid virtual calls or boxing or whatever else for reasons of performance, I guess I feel very strongly that it should do so in a way that doesn't impact users, and shouldn't let these concerns influence the API design.
Re:Go 1.21 Released by morelisp on Wednesday August 09, 2023 @01:45PM (#37066175)
"Ignore performance when designing your API" is an obvious non-starter. I don't think you're serious.

More specifically, users want the surface area Logger has. They do not want a single function with a complex object specification, just like they don't want a function per type.

Re:Go 1.21 Released by kiitos on Thursday August 10, 2023 @04:28PM (#37081322)
There is obviously an enormous difference between "don't let performance influence your API (to become awkward or nonidiomatic)" and "ignore performance when designing your API".
Re:Go 1.21 Released by morelisp on Thursday August 10, 2023 @05:14PM (#37081881)
But you've offered no actual criteria why the API is bad! Again, most logging users want something to handle nested record contexts for them and per-level methods, most logging implementers want a minimum number of entry points, and that's exactly what this API offers. (It's definitely not zerolog-style where it sacrifices ease of use for performance.)
Re:Go 1.21 Released by kiitos on Thursday August 10, 2023 @10:08PM (#37084269)
Structured logging means logging where discrete events are sets of key=val pairs, and nothing more.

What is a record context? What is a level? These are concepts built on top of a structured logger, which manifest as specific key=val pairs in a given structured log event. They don't need -- and shouldn't have! -- first-order representation in a low-level structured logger API.

What is an entry point? If I'm writing some structured logger implementation, I expect that I should need to provide precisely one method:

    func (x *MyThing) Log(<set of key=val pairs>) error
Anything more is cruft.
Re:Go 1.21 Released by kiitos on Saturday August 12, 2023 @06:47PM (#37104915)
I don't think so.

A Handler is something that transforms log events to concrete outputs (files, disks, writes to stderr, etc.) via side effects.

A Logger is what programs use to produce, transform, etc. log events.

Both are abstract interfaces with arbitrarily many possible implementations. Both are defined in terms of their user-facing capabilities. I'm not sure how those definitions would differ. Both accept log events and do something with them. That interface is the same.

Re:Go 1.21 Released by akshayshah on Wednesday August 09, 2023 @03:19AM (#37059604)
Zap author here :) Logrus remains quite a bit more popular than zap. (Does anyone remember the epic tire fire when Sirupsen briefly changed the casing of his GitHub username?)

From my perspective, the problem with the KeyValuePairs API is that it’s inescapably slow and allocation-heavy. I’m glad that a logging API built into the standard library is usable in more performance-sensitive contexts. It’s easy enough to wrap it up in a KeyValuePairs-style API if you’d prefer that, and you’ll now have the ability to unwrap your logger and interop with other libraries that expect the standard slog interface.

Re:Go 1.21 Released by badrequest on Tuesday August 08, 2023 @04:17PM (#37054907)
I've seen the API, and it's not either of those things if you've already used a structured logger?
Re:Go 1.21 Released by kiitos on Tuesday August 08, 2023 @07:52PM (#37057030)
I've never seen a structured logging library with concepts like Attrs, Groups, or Records like those defined by slog.
Re:Go 1.21 Released by yencabulator on Saturday August 12, 2023 @05:45PM (#37104520)
Attr is the same concept as https://pkg.go.dev/go.uber.org/zap#Field , and Zap pioneered this whole field.

Group is pretty much https://pkg.go.dev/go.uber.org/zap#Namespace

Record is the interface to log sinks, not something the typical programmer worries about.

Re:Go 1.21 Released by kiitos on Sunday August 13, 2023 @02:02AM (#37107125)
Haha, zap did not pioneer anything, what are you talking about.
Re:Go 1.21 Released by yencabulator on Sunday August 13, 2023 @12:27PM (#37111411)
"Haha", great attitude dissing other people's work. https://news.ycombinator.com/newsguidelines.html

What Go structured logging libraries predate Zap's 2016 creation? Only one I can remember is Logrus, which was using type Fields map[string]interface{}, the bad qualities of which are kinda the whole reason for Zap's creation, and slog follows the Zap-style API[1].

[1]: Though ignoring many of the optimizations..