TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Structured logging with slog

275 pointsby spaceyover 1 year ago

21 comments

mrweaselover 1 year ago
Admittedly I&#x27;m not a huge fan of having:<p><pre><code> slog.Info(&quot;hello, world&quot;, &quot;user&quot;, os.Getenv(&quot;USER&quot;)) </code></pre> It&#x27;s a little magical that &quot;user&quot; is a key. So what if you have multiple key-value pairs? Arguably it most likely going to be obvious which is the keys, but having every other value be a key and the rest values seems a little clumsy.<p>I really like Pythons approach where you can have user=&quot;value&quot; it makes things a bit more clear.
评论 #37227593 未加载
评论 #37226737 未加载
评论 #37226893 未加载
评论 #37226895 未加载
评论 #37232383 未加载
评论 #37228691 未加载
评论 #37226727 未加载
评论 #37227201 未加载
评论 #37226782 未加载
评论 #37230075 未加载
评论 #37229938 未加载
评论 #37228561 未加载
评论 #37228015 未加载
corytheboydover 1 year ago
Structured logging is such an easy to gain, massive improvement to observability. Assuming you can pay for the log processor to make sense of it all :)<p>I’ve been working on a side project to bring something like the DataDog log explorer to the local development environment. The prototype I made has already been extremely helpful in debugging issues in a very complex async ball of Rails code. Does something like that sound useful to other folks? Does it already exist and I just can’t find it?
评论 #37229984 未加载
评论 #37232145 未加载
评论 #37226644 未加载
ar_lanover 1 year ago
I&#x27;m so happy this is a stdlib feature. This is good enough for me to not need to bring in external loggers (e.g. zerolog) which is nice, and I strongly think that structured logging should be the default logging format.<p>Logs are data.
dgb23over 1 year ago
The rationale:<p>&gt; With many structured logging packages to choose from, large programs will often end up including more than one through their dependencies. The main program might have to configure each of these logging packages so that the log output is consistent: it all goes to the same place, in the same format. By including structured logging in the standard library, we can provide a common framework that all the other structured logging packages can share.<p>This is IMO the right way of doing it. Provide an interface with simple defaults, usable out of the box. Those who need more can use a library that builds towards the interface.<p>So when evaluating any library, you can ask &quot;How well does this integrate with interfaces in the standard library?&quot;. Discovering that some functionality is just a &quot;Fooer&quot; that pieces well together with existing stuff is calming. Not only do you already know how to &quot;Foo&quot;, you also get a hidden stability benefit: There&#x27;s an implied API surface contract here.<p>This is in stark contrast to the &quot;builds on top of&quot; approach, where you end up with competing, idiosyncratic interfaces. This is often necessary, but there is always an implied risk in terms of maintenance and compatibility.
评论 #37226458 未加载
评论 #37227275 未加载
baz00over 1 year ago
Now all we need is the 1,000,000 other components in the multiple ecosystems to log in the same format and I won&#x27;t have a perpetual headache.<p>Good job Go though for being opinionated but rational.
评论 #37235702 未加载
geodelover 1 year ago
With this another most requested feature is covered by Go. This leaves error handling, enum type which are often asked by users but are not actively being worked on for now.
评论 #37226690 未加载
评论 #37226148 未加载
nwsmover 1 year ago
It&#x27;s nice to have this in the standard library, but it doesn&#x27;t solve any existing pain points around structured log metadata and contexts. We use zap [0] and store a zap logger on the request context which allows different parts of the request pipeline to log with things like tenantId, traceId, and correlationId automatically appended. But getting a logger off the context is annoying, leads to inconsistent logging practices, and creates a logger dependency throughout most of our Go code.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;uber-go&#x2F;zap">https:&#x2F;&#x2F;github.com&#x2F;uber-go&#x2F;zap</a>
评论 #37227309 未加载
评论 #37232824 未加载
jasonhanselover 1 year ago
I must admit: I&#x27;m not a huge fan of structured logging, beyond simple use cases like tagging messages by the thread that produced them. If you want something machine-readable, use a dedicated metrics system, analytics database, or document store. If you want something human-readable, structured logging will only make things worse.
评论 #37230618 未加载
评论 #37230629 未加载
zknillover 1 year ago
The new structured logging library is a great addition, its nice to have structured logging in the standard lib.<p>It&#x27;s easy to get started with log&#x2F;slog and one of the built in handlers, but as soon as you want to change something the library design pushes you towards implementing an entire handler.<p>For example, if I want the built in JSON format, but with a different formatting of the Time field, that&#x27;s not easy to do. It&#x27;s not obvious how to change the built in handler.<p>I wrote slogmw[1] to solve this problem. It&#x27;s a set of middleware and examples that make it easy to make small changes to the built in handlers without having to write a whole new handler from scratch.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;zknill&#x2F;slogmw">https:&#x2F;&#x2F;github.com&#x2F;zknill&#x2F;slogmw</a>
评论 #37232887 未加载
评论 #37227470 未加载
politicianover 1 year ago
I wish there was a better approach for the problem of avoiding function calls when the log level at runtime is higher than the call site.<p>So,<p><pre><code> slog.Info(&quot;failed to frob&quot;, &quot;thing&quot;, GetThing(1)) </code></pre> Still calls GetThing(1) when the log level is greater than Info. The only solution right now for this is to test the log level before making the logging call. It would be amazing if language designers could make the arguments late bound instead or used aspect-oriented programming approaches to protect each logging call site.
评论 #37230053 未加载
评论 #37228636 未加载
评论 #37231353 未加载
Seb-Cover 1 year ago
I guess it&#x27;s nice to have a standard, but I wish the Golang developers stopped introducing stuff like &quot;args ...any&quot; all over the place in the standard library.<p>It&#x27;s not the level of type-safety that I expect from a strongly typed language.
mihaitodorover 1 year ago
Benthos just adopted it: <a href="https:&#x2F;&#x2F;github.com&#x2F;benthosdev&#x2F;benthos&#x2F;commit&#x2F;ee0000450413ad37685ecbfe0d1ef40d178d29fb">https:&#x2F;&#x2F;github.com&#x2F;benthosdev&#x2F;benthos&#x2F;commit&#x2F;ee0000450413ad3...</a>
评论 #37227549 未加载
kushaover 1 year ago
Oof. We just converted all of our logging to zap[0] to get structured JSON logging for downstream parsing. Wonder how the perf stacks up.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;uber-go&#x2F;zap">https:&#x2F;&#x2F;github.com&#x2F;uber-go&#x2F;zap</a>
评论 #37227639 未加载
tastysandwichover 1 year ago
I&#x27;m really glad they&#x27;ve introduced this, I just wish it also had the traditional formatting methods, eg Infof, Debugf, Errorf, etc, for backwards compatibility.<p>I&#x27;ve got a few packages that accept a basic logger interface, eg:<p><pre><code> type debugLogger interface { Debugf(format string, args ...any) } type MyThing struct { logger debugLogger } func New(logger debugLogger) *MyThing { return &amp;MyThing{logger} } </code></pre> I&#x27;d love to switch to slog but I&#x27;ll have to v2 these packages now.
评论 #37229522 未加载
__loamover 1 year ago
Slog is an amazing name
insanitybitover 1 year ago
Structured logging is a very sane default. Even if you end up with `{&quot;msg&quot;: &quot;blah blah blah&quot;}` at least you have room to grow in the future.
评论 #37227710 未加载
candiddevmikeover 1 year ago
Looking for advice: logging for servers&#x2F;services tends to be different than logging for CLI-based applications. How do folks differentiate them or use slog for them in a generic way? Or does it make sense to have separate logging packages for CLI vs services? CLI tends to be more verbose and procedural vs servers&#x2F;service based logging which is more errors only unless debug.
评论 #37227238 未加载
Femoloover 1 year ago
Log everything as Json!<p>We need to start making Json viewer were we look into log files without tooling the default.<p>It&#x27;s so much easier to use Json automatically and ship them to systems out of the box.<p>Linux logging should do that too. Not just container in k8s
评论 #37229724 未加载
mi_lkover 1 year ago
Q: what do people use for structured logging in Java?
评论 #37229852 未加载
评论 #37228134 未加载
bobbyiover 1 year ago
Walking past an eatery with outdoor seating, I overheard one diner say the phrase &quot;process raw logs&quot; and I thought, &quot;wow, I guess that is one of those tricky problems that basically everyone ends up dealing with&quot;.<p>And then I heard &quot;... with a chainsaw. It&#x27;s a chainsaw mill&quot; and realized I may have misunderstood the context.
评论 #37228022 未加载
User23over 1 year ago
Just log to sqlite. It’s literally better than all the alternatives, but for some reason nobody does.
评论 #37231346 未加载