Logrus Loki Hook
A simple non blocking logrus Hook pushing logs to a Loki server
Loki labels
The hook add every logrus field as label and the following default:
-
app
: the app name -
level
: the log level, e.g. info
If log.ReportCaller
is set:
-
go_function
: the go function name -
go_file
: the go file name -
go_line
: the go fine line number
Usage
If you are running on Grafana Cloud, use:
export LOKI_ADDR=https://logs-us-west1.grafana.net
export LOKI_USERNAME=<username>
export LOKI_PASSWORD=<password>
Create a new logrus Hook from env and log as usual:
package main
import (
"time"
"github.com/sirupsen/logrus"
loki "gitlab.bertha.cloud/partitio/lab/logrus-loki-hook"
)
func main() {
h, err := loki.NewHookFromEnv(
"hook_example",
logrus.TraceLevel,
loki.WithFieldAsLabels(false),
loki.WithLabelsKV("version", "v0.3.0"),
)
if err != nil {
logrus.Fatal(err)
}
defer h.Close()
log := logrus.New()
log.SetLevel(logrus.TraceLevel)
log.ReportCaller = true
log.AddHook(h)
i := 0
for {
i++
l := log.WithField("value", i).WithField("test", true)
if i%2 == 0 {
l.Infof("it's the %d message", i)
} else {
l.Errorf("%d is even", i)
}
time.Sleep(time.Second)
}
}
Loki Command Line example query:
logcli query '{app="hook_example", version="0.3.0"}'
2020-11-09T18:40:39+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:39+01:00" level=error msg="1 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=1
2020-11-09T18:40:40+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:40+01:00" level=info msg="it's the 2 message" func=main.main file="/logrus-loki-hook/example/example.go:26" test=true value=2
2020-11-09T18:40:41+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:41+01:00" level=error msg="3 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=3
2020-11-09T18:40:42+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:42+01:00" level=info msg="it's the 4 message" func=main.main file="/logrus-loki-hook/example/example.go:26" test=true value=4
2020-11-09T18:40:43+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:43+01:00" level=error msg="5 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=5
2020-11-09T18:40:44+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:44+01:00" level=info msg="it's the 6 message" func=main.main file="/logrus-loki-hook/example/example.go:26" test=true value=6
2020-11-09T18:40:45+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:45+01:00" level=error msg="7 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=7
2020-11-09T18:40:46+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:46+01:00" level=info msg="it's the 8 message" func=main.main file="/logrus-loki-hook/example/example.go:26" test=true value=8
2020-11-09T18:40:47+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:47+01:00" level=error msg="9 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=9
2020-11-09T18:40:48+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:48+01:00" level=info msg="it's the 10 message" func=main.main file="/logrus-loki-hook/example/example.go:26" test=true value=10
2020-11-09T18:40:49+01:00 {app="hook_example", version="0.3.0"} time="2020-11-09T18:40:49+01:00" level=error msg="11 is even" func=main.main file="/logrus-loki-hook/example/example.go:28" test=true value=11