carlware / promtail-go

promtail client in go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

promtail golang client

The purpose of this small library is creating a tool for upload logs to grafana loki. The logs looks pretty amazing and awesome, we can have static labels or dynamic labels for filtering while we're debugging.

awesome_logs

promtail client for golang services

// create a promtail client
promtail, pErr := client.NewSimpleClient(host, username, password,
    client.WithStaticLabels(map[string]interface{}{
        "app": "awesome-service",
    }),
    client.WithStreamConverter(promtail.NewRawStreamConv(labels, "=")),
)
if pErr != nil {
    panic(pErr)
}

// now can be used as a Writer for any logger

// setup any logger and use as the output, can be combined with many outputs using io.MultiWriter
output := zerolog.ConsoleWriter{Out: promtail}
output.FormatMessage = func(i interface{}) string {
    _, ok := i.(string)
    if ok {
        return fmt.Sprintf("%-50s", i)
    } else {
        return ""
    }
}
output.FormatLevel = func(i interface{}) string {
    _, ok := i.(string)
    if ok {
        return fmt.Sprintf("level=%-7s", i)
    } else {
        return "level=info"
    }
}
log.Logger = log.Output(output)

log.Info().
    Str("my_label", "some_value").
    Msg("the message")

About

promtail client in go

License:MIT License


Languages

Language:Go 92.2%Language:Makefile 7.8%