Skip to content

Formatting examples

Michael Kenney edited this page Mar 9, 2019 · 5 revisions

Text format

By default, bdlm/log uses a basic text format:

time="2018-08-12T20:36:34.577-06:00" level="debug" msg="Oh, look, a bird..." data.animal="bird" data.count=1 caller="main.go:42 main.main" host="myhost"
time="2018-08-12T20:36:34.578-06:00" level="info" msg="A group of walrus emerges from the ocean" data.animal="walrus" data.count=20 caller="main.go:46 main.main" host="myhost"
time="2018-08-12T20:36:34.578-06:00" level="warn" msg="The group's number increased tremendously!" data.animal="walrus" data.count=100 caller="main.go:50 main.main" host="myhost"
time="2018-08-12T20:36:34.578-06:00" level="error" msg="Tremendously sized cow enters the ocean." data.animal="cow" data.count="wait, what?" caller="main.go:54 main.main" host="myhost"
time="2018-08-12T20:36:34.579-06:00" level="panic" msg="The walrus are attacking!" data.animal="walrus" data.run=true caller="main.go:58 main.main" host="myhost"
time="2018-08-12T20:36:34.579-06:00" level="fatal" msg="That could have gone better..." data.dead=true data.winner="walrus" caller="main.go:33 main.main.func1" host="myhost"

For development environments, color-coded output formated for humans is automatically enabled when a tty terminal is detected:

This can be disabled with by setting the DisableTTY property to true:

log.SetFormatter(&log.TextFormatter{
    DisableTTY: true,
})

There is also a ForceTTY flag that can be used to force tty output. This is useful when tailing container logs that won't register as a tty terminal for example.

log.SetFormatter(&log.TextFormatter{
    ForceTTY: true,
})

JSON format

JSON formatting is also available with log.SetFormatter(&log.JSONFormatter{}) for easy parsing by logstash or similar:

{"caller":"main.go:42 main.main","data":{"animal":"bird","count":1},"host":"myhost","level":"debug","msg":"Oh, look, a bird...","time":"2018-08-12T20:38:03.997-06:00"}
{"caller":"main.go:46 main.main","data":{"animal":"walrus","count":20},"host":"myhost","level":"info","msg":"A group of walrus emerges from the ocean","time":"2018-08-12T20:38:03.998-06:00"}
{"caller":"main.go:50 main.main","data":{"animal":"walrus","count":100},"host":"myhost","level":"warn","msg":"The group's number increased tremendously!","time":"2018-08-12T20:38:03.998-06:00"}
{"caller":"main.go:54 main.main","data":{"animal":"cow","count":"wait, what?"},"host":"myhost","level":"error","msg":"Tremendously sized cow enters the ocean.","time":"2018-08-12T20:38:03.998-06:00"}
{"caller":"main.go:58 main.main","data":{"animal":"walrus","run":true},"host":"myhost","level":"panic","msg":"The walrus are attacking!","time":"2018-08-12T20:38:03.999-06:00"}
{"caller":"main.go:33 main.main.func1","data":{"dead":true,"winner":"walrus"},"host":"myhost","level":"fatal","msg":"That could have gone better...","time":"2018-08-12T20:38:03.999-06:00"}

Similar to the text formatter, The JSON formatter also makes adjustments by default when a tty terminal is detected:

This can be disabled with by setting the DisableTTY property to true:

log.SetFormatter(&log.JSONFormatter{
    DisableTTY: true,
})

There is also a ForceTTY flag that can be used to force tty output. This is useful when tailing container logs that won't register as a tty terminal for example.

log.SetFormatter(&log.JSONFormatter{
    ForceTTY: true,
})