Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

magnetde/loki

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository is a mirror of a private GitLab instance. All changes will be overwritten.

Loki Hook for logrus

This hook allows logging with logrus to Loki. Logs are sent in logfmt format.

go get github.com/magnetde/loki

Example

package main

import (
	log "github.com/sirupsen/logrus"
	"github.com/magnetde/loki"
)

func main() {
	// NewHook(url, ...options)
	hook := loki.NewHook("http://localhost:3200", loki.WithName("my-go-binary"), loki.WithMinLevel(log.InfoLevel))
	defer hook.Close()

	log.AddHook(hook)

	// ...
}

Example call in Grafana

{name="my-go-binary"} | logfmt | level =~ "warning|error|fatal|panic"

This displays all log entries of the application, with a severity of at least "warning". The call depends on the Formatter.

Options

  • WithName(string):
    This adds the additional label "name" to all log entries sent to loki. It is the only additional label, that is added to the log message if the default logger is used.
  • WithLabel(string, interface{}):
    Similar to WithName, this adds an additional label to all log entries sent to loki.
  • WithLabelsEnabled(...Label):
    Send additional attributes of the entry as labels. By default, only the name attribute is sent.
    Available labels:
    • labels added with WithName or WithLabel are always added to log entires
    • FieldsLabel: add all extra fields as labels (Entry.Data)
    • TimeLabel: add the time as a label (Entry.Time)
    • LevelLabel: add the log level as a label (Entry.Level)
    • CallerLabel: add the caller with format "[file]:[line]:[function]" (Entry.Caller)
    • MessageLabel: add the message as a label (Entry.Message)
  • WithFormatter(logrus.Formatter):
    By default, the TextFormatter with disabled colors and disabled timestamp is used. Additional, it formats the caller in format ("file:line:func()", "") (empty file value).
  • WithRemoveColors(bool):
    Remove ANSI colors from the serialized log entry. This only works with the default formatter.
  • WithMinLevel(logrus.Level):
    Minimum level for log entries. All entries that have a lower severity are ignored.
  • WithBatchInterval(time.Duration):
    Batch interval. If this interval has been reached since the last sending, all log entries collected so far will be sent (default: 10 seconds).
  • WithBatchSize(int):
    Maximum batch size. If the number of collected log entries is exceeded, all collected entries will be sent (default: 1000).
  • WithSynchronous(bool):
    By default (async mode), log entries are processed in a separate Go routine. If synchronous sending is used, batch interval and size are ignored.
  • WithSuppressErrors(bool):
    Errors at asynchronous mode are logged to the console. This disables the logging of errors. Ignored at synchronous mode.

About

Loki hook or logrus

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages