Any recommendation for structured logging library in Golang?

201 views
Skip to first unread message

ChrisLu

unread,
Nov 17, 2020, 11:21:48 PM11/17/20
to golang-nuts
I am considering moving from glog to structured logging. I tried logrus, go-kit, uber/zap, but could not find one good fit. In short, this is the desired format:

[info][timestamp] [filename:line_number] message k1=v1 k2=v2 ...

It shows the correct file name(need to pop out a few call stacks) and line number in a customizable format, not as key-value pair for each line which is just too verbose to have the extra "time=" "file=".

Please let me know the one you actually use.


Thanks!

Chris

Mike Spreitzer

unread,
Nov 18, 2020, 2:18:23 AM11/18/20
to ChrisLu, golang-nuts
> From: ChrisLu <chri...@gmail.com>
> To: golang-nuts <golan...@googlegroups.com>
> Date: 11/17/2020 11:22 PM
> Subject: [EXTERNAL] [go-nuts] Any recommendation for structured
> logging library in Golang?
> Sent by: golan...@googlegroups.com
How about one that has fine-grained debug leveling, like glog or klog,
too?

Thanks,
Mike


Gopal M

unread,
Nov 18, 2020, 2:20:59 AM11/18/20
to Mike Spreitzer, ChrisLu, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/OF42CB2A54.2706B41C-ON85258624.0027F1D3-85258624.00280B00%40notes.na.collabserv.com.


--
Regards,
M. Gopal

Chris Lu

unread,
Nov 18, 2020, 3:28:30 AM11/18/20
to Gopal M, Mike Spreitzer, golang-nuts
On Tue, Nov 17, 2020 at 11:20 PM Gopal M <gopalt...@gmail.com> wrote:


I have tried uber/zap, which has a verbose json:
{time="...", file="abc.go", line="32", message="xafasdfasdf", k1=v1, k2=v2}

The extra "time=" and "file=" on every single log line are what I want to avoid. 

Do you have some way to change the log format?

Tamás Gulácsi

unread,
Nov 18, 2020, 8:50:25 AM11/18/20
to golang-nuts
This format is quite specific - roll your own!
See for example https://github.com/shamaazi/antilog or a derivative (github.com/UNO-SOFT/ulog) - very simple code, easy to modify.

My experience is against logfmt: is is nice, human-readable, but it is hard to log structures with it - you'll use fmt.Sprintf("%#v", structure), or json.Marshal(structure).

seank...@gmail.com

unread,
Nov 18, 2020, 5:03:53 PM11/18/20
to golang-nuts
Since you've been using glog, k8s.io/klog/v2 (fork by kubernetes) should feel familiar and v2 supports the structured variants

Denis Cheremisov

unread,
Nov 19, 2020, 4:14:21 AM11/19/20
to golang-nuts
Zerolog does the trick, need a bit of setup though for what you want

среда, 18 ноября 2020 г. в 07:21:48 UTC+3, ChrisLu:

Space A.

unread,
Nov 19, 2020, 5:21:47 AM11/19/20
to golang-nuts
If you want to go for structured and layered logging in JSON, consider https://github.com/francoispqt/onelog


четверг, 19 ноября 2020 г. в 12:14:21 UTC+3, Denis Cheremisov:
Reply all
Reply to author
Forward
0 new messages