formatters

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2018 License: MIT Imports: 4 Imported by: 0

README

go-logrus-formatters Build Status

This project is a set of logrus formatters.

Installation

Preferred way of installation is through dep:

dep ensure -add github.com/fabienm/go-logrus-formatter

GELF formatter

The GELF formatter supports 1.1 payload specification.

Notable features:

  • Logrus levels are converted to syslog levels
  • Logrus entries times are converted to UNIX timestamps.
  • Logrus entry fields are prefixed with _, excepted version, host, short_message, full_message, timestamp and level, allowing override.
Syslog level mapping
Logrus Syslog
Panic EMERG (0)
Fatal CRIT (2)
Error ERR (3)
Warn WARNING (4)
Info INFO (6)
Debug DEBUG (7)
Usage
package main

import (
	"os"

	"github.com/fabienm/go-logrus-formatters"
	log "github.com/sirupsen/logrus"
)

func init() {
	hostname, _ := os.Hostname()
	// Log as GELF instead of the default ASCII formatter.
	log.SetFormatter(formatters.NewGelf(hostname))
}

func main() {
	log.WithFields(log.Fields{
		"animal": "walrus",
		"size":   10,
	}).Info("A group of walrus emerges from the ocean")
	log.WithFields(log.Fields{
		"full_message":  "Backtrace here\n\nmore stuff",
		"user_id":      9001,
		"some_info":    "foo",
		"some_env_var": "bar",
	}).Fatal("A short message that helps you identify what is going on")
}

Output:

{"_animal":"walrus","_level_name":"INFORMATIONAL","_size":10,"host":"LAPTOP860528.local","level":6,"short_message":"A group of walrus emerges from the ocean","timestamp":1522937330.7570872,"version":"1.1"}
{"_some_env_var":"bar","_some_info":"foo","_user_id":9001,"_level_name":"CRITICAL","full_message":"Backtrace here\n\nmore stuff","host":"LAPTOP860528.local","level":2,"short_message":"A short message that helps you identify what is going on","timestamp":1522937330.7573297,"version":"1.1"}

See also

Documentation

Index

Constants

View Source
const (
	// GelfVersion is the supported gelf version
	GelfVersion = "1.1"
)

Variables

View Source
var (

	// DefaultLevel is the default syslog level to use if the logrus level does not map to a syslog level
	DefaultLevel = syslog.LOG_INFO
)

Functions

func NewGelf

func NewGelf(hostname string) gelfFormatter

NewGelf returns a new logrus / gelf-compliant formatter

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL