frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Logging in Go with Slog: A Practitioner's Guide

https://www.dash0.com/guides/logging-in-go-with-slog
28•ayoisaiah•3d ago

Comments

codeduck•2h ago
I'm surprised this isn't a standard base pattern in languages, to be honest. Apache's commons-logging library was a standard part of enterprise java placements for many years, and only started to go away when Log4J came along.
lmz•2h ago
Log4j is one of the possible backends for commons logging (and was basically the reason for it - choosing between log4j and the built-in java logging). I think you mean SLF4J?
codeduck•1h ago
I may be remembering it wrong, but I think log4j only became a commons logging backend several years after it became mainstream; before that I remember the two being entirely different and no interchangeable. It's a long time ago!
imiric•2h ago
I'm a big fan of slog, and this is a great overview.

The fact it is so flexible and composable, while still maintaining a simple API is just great design. I wasn't aware of the performance overhead compared to something like zerolog, but this shouldn't be a concern for most applications.

aleksi•2h ago
My biggest gripe with slog is that there is no clear guidance on supported types of attributes.

One could argue that supported types are the ones provided by Attr "construct" functions (like slog.String, slog.Duration, etc), but it is not enough. For example, there is no function for int32 – does it mean it is not supported? Then there is slog.Any and some support in some handlers for error and fmt.Stringer interfaces. The end result is a bit of a mess.

0x696C6961•1h ago
All values are supported.
aleksi•46m ago
Well, is fmt.Stringer supported? The result might surprise you:

  req := expvar.NewInt("requests")
  req.Add(1)
  
  attr := slog.Any("requests", req)
  
  slog.New(slog.NewTextHandler(os.Stderr, nil)).Info("text", attr)
  slog.New(slog.NewJSONHandler(os.Stderr, nil)).Info("json", attr)
This code produces

  time=2025-09-12T13:15:42.125+02:00 level=INFO msg=text requests=1
  {"time":"2025-09-12T13:15:42.125555+02:00","level":"INFO","msg":"json","requests":{}}
So the code that uses slog but does not know what handler will be used can't rely on it lazily calling the `String() string` method: half of the standard handlers do that, half don't.
0x696C6961•24m ago
If you need more control, you can create a wrapper type that implements `slog.LogValuer`

    type StringerValue struct {
        fmt.Stringer
    }

    func (v StringerValue) LogValue() slog.Value {
        return slog.StringValue(v.String())
    }

Usage example:

    slog.Any("requests", StringerValue{req})

There might be a case for making the expvar types implement `slog.LogValuer` directly.
aleksi•19m ago
So clearly not all values are supported.

And I know that I can create a wrapper for unsupported types. My problem is exactly that – I don't know what types are supported. Is error supported, for example? Should I create a wrapper for it? And, as a handler author, should I support it directly or not?

0x696C6961•6m ago
I'm afraid you're going to have to bite the bullet and ... gasp ... read the documentation https://pkg.go.dev/log/slog
awesome_dude•1h ago
I have a gripe with slog - it uses magic for config

What I mean is, if you configure slog in (say) your main package, then, by magic, that config is used by any call to slog within your application.

There's no "Oh you are using this instance of slog that has been configured to have this behaviour" - it's "Oh slog got configured so that's the config you have been given"

I've never tried to see if I can split configs up, and I don't have a usecase, it just strikes me as magic is all

roncesvalles•1h ago
There's a default logger that's used when you call package-level functions (as opposed to methods on an instance of slog.Logger). The default logger is probably what you configured in your main package.

In my opinion this is perfectly idiomatic Go. Sometimes the package itself hosts one global instance. If you think that's "magic" then you must think all of Go is magic. It helps to think of a package in Go as equivalent to a single Java class. Splitting up a Go package's code into multiple files is purely cosmetic.

sethammons•56m ago
If you need log output in tests, you should use an instance of a logger.

More teams should be validating their logging and should be leveraging structured logging and getting valuable logging insights/events.

catlifeonmars•19m ago
I’m not sure I understand what you mean by “magic for config”. You create and configure a logger using slog.New(…). You can use the default logger instead, slog.Default(), which is just a global and has a default config. You can also set the default logger using slog.SetDefault(…).

It’s extremely unmagical in my opinion.

gdbsjjdn•1m ago
The "magic" is just global state? I agree that I try to avoid global state in my code but it's hardly Spring Boot levels of auto wiring bullshit.
arcaen•52m ago
The thing that gets me about slog is that the output key for the slog JSON handler is msg, but that's not compatible with Googles own GCP Stackdriver logging. Since that key is a constant I now need to use an attribute replacer to change it from msg to message (or whatever it is stackdriver wants). Good work Google.

Qwen3-Next

https://qwen.ai/blog?id=4074cca80393150c248e508aa62983f9cb7d27cd&from=research.latest-advancement...
243•tosh•5h ago•75 comments

Astrophysics Source Code Library

http://ascl.net/
15•SiempreViernes•1h ago•0 comments

Becoming the person who does the thing

https://www.fredrivett.com/2025/09/10/becoming-the-person-who-does-the-thing/
21•fredrivett•1h ago•3 comments

Examples from The LaTeX Companion book (3rd edition)

https://ctan.org/pkg/tlc3-examples
28•teleforce•3h ago•5 comments

Why our website looks like an operating system

https://posthog.com/blog/why-os
431•bnc319•12h ago•305 comments

Show HN: I made a generative online drum machine with ClojureScript

https://dopeloop.ai/beat-maker/
32•chr15m•3h ago•10 comments

Float Exposed

https://float.exposed/
257•SomaticPirate•11h ago•65 comments

Classic GTK1 GUI Library

https://gitlab.com/robinrowe/gtk1
44•MaximilianEmel•3d ago•11 comments

Debian 13, Postgres, and the US time zones

https://rachelbythebay.com/w/2025/09/11/debtz/
150•move-on-by•9h ago•63 comments

Top model scores may be skewed by Git history leaks in SWE-bench

https://github.com/SWE-bench/SWE-bench/issues/465
413•mustaphah•17h ago•127 comments

Using Emacs Org-Mode With Databases: A getting-started guide

https://gitlab.com/ryanprior/emacs-org-data-starter
57•adityaathalye•3d ago•6 comments

Introduction to Nyquist and Lisp Programming

https://manual.audacityteam.org/man/introduction_to_nyquist_and_lisp_programming.html
13•swatson741•3d ago•0 comments

Claude’s memory architecture is the opposite of ChatGPT’s

https://www.shloked.com/writing/claude-memory
373•shloked•17h ago•197 comments

Logging in Go with Slog: A Practitioner's Guide

https://www.dash0.com/guides/logging-in-go-with-slog
28•ayoisaiah•3d ago•16 comments

Doorbell prankster that tormented residents of apartments turns out to be a slug

https://www.theguardian.com/world/2025/sep/08/doorbell-prankster-that-tormented-residents-of-germ...
206•robin_reala•3d ago•103 comments

AirPods live translation blocked for EU users with EU Apple accounts

https://www.macrumors.com/2025/09/11/airpods-live-translation-eu-restricted/
371•thm•1d ago•428 comments

XFN – XHTML Friends Network (2003)

https://gmpg.org/xfn/
42•thinkingemote•4d ago•10 comments

Crossing the Atlantic Ocean. Alone. By Stand-Up-Paddleboard

https://zeroemissions.eu/en/ocean-crossing-eng
4•gnabgib•3d ago•2 comments

Building my childhood dream PC

https://fabiensanglard.net/2168/
158•joexbayer•4d ago•55 comments

Behind the scenes of Bun Install

https://bun.com/blog/behind-the-scenes-of-bun-install
394•Bogdanp•23h ago•131 comments

Samsung taking market share from Apple in U.S. as foldable phones gain momentum

https://www.cnbc.com/2025/08/16/samsungs-us-market-share-apple-rivalry-foldable-phones.html
241•mgh2•1d ago•272 comments

Toddlerbot: Open-Source Humanoid Robot

https://toddlerbot.github.io/
80•base698•12h ago•17 comments

Bulletproof host Stark Industries evades EU sanctions

https://krebsonsecurity.com/2025/09/bulletproof-host-stark-industries-evades-eu-sanctions/
192•todsacerdoti•18h ago•72 comments

Rails on SQLite: new ways to cause outages

https://andre.arko.net/2025/09/11/rails-on-sqlite-exciting-new-ways-to-cause-outages/
162•ingve•17h ago•49 comments

Show HN: C++ Compiler Support Page

https://cppstat.dev
51•cemdervis•4d ago•15 comments

Gene-edited pancreatic cells transplanted into a patient with type 1 diabetes

https://www.wired.com/story/no-more-injections-crispr-offers-new-hope-for-treating-diabetes/
228•manveerc•22h ago•58 comments

From burner phones to decks of cards: NYC teens adjusting to the smartphone ban

https://gothamist.com/news/from-burner-phones-to-decks-of-cards-nyc-teens-are-adjusting-to-the-sm...
250•geox•22h ago•188 comments

Full Moon: Seestar S50 vs. Samsung S25

https://www.4rknova.com//blog/2025/09/08/moon-photos
36•ibobev•3d ago•28 comments

Danish supermarket chain is setting up "Emergency Stores"

https://swiss.social/@swaldorff/115186445638788782
294•sohkamyung•13h ago•290 comments

The challenge of maintaining curl

https://lwn.net/Articles/1034966/
156•signa11•10h ago•43 comments