Help us shape the future of Go! Take the 2026 Go Developer Survey

wrapped

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 4 Imported by: 0

README

WrappedJSONFormatter

This logrus formatter allows you to wrap you JSON logs into a first level element named after the field kind.

An example with the standard JSONFormatter would be:

{"animal":"walrus","level":"info","msg":"A giant walrus appears!",
"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}

With WrappedJSONFormatter, you could pass an extra field like this:

log = log.WithField("kind", "wrapped_key_name")

And the output would become:

{"wrapped_key_name": {"animal":"walrus","level":"info","msg":"A giant walrus appears!",
"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}}

Example:

package main

import (
	log "github.com/sirupsen/logrus"
	wrapped "github.com/michele/logrus-wrapped-json"
)

func init() {
  log.SetFormatter(&wrapped.WrappedJSONFormatter{})
}

func main() {
    httpLog := log.WithField("kind", "response_log")
    dbLog := log.WithField("kind", "sql_log")

    ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldMap

type FieldMap map[fieldKey]string

FieldMap allows customization of the key names for default fields.

type WrappedJSONFormatter

type WrappedJSONFormatter struct {
	// TimestampFormat sets the format used for marshaling timestamps.
	TimestampFormat string

	// DisableTimestamp allows disabling automatic timestamps in output
	DisableTimestamp bool

	// FieldMap allows users to customize the names of keys for various fields.
	// As an example:
	// formatter := &JSONFormatter{
	//   	FieldMap: FieldMap{
	// 		 FieldKeyTime: "@timestamp",
	// 		 FieldKeyLevel: "@level",
	// 		 FieldKeyLevel: "@message",
	//    },
	// }
	FieldMap FieldMap
}

func (*WrappedJSONFormatter) Format

func (f *WrappedJSONFormatter) Format(entry *logrus.Entry) ([]byte, error)

Jump to

Keyboard shortcuts

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