formatter

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 5 Imported by: 0

README

logrus-formatter

Forked from antonfisher/nested-logrus-formatter

Human-readable log formatter, converts logrus fields to a nested structure:

Screenshot

Configuration:

type Formatter struct {
	/**
	FieldsOrder
	Default:
	- time
	- level
	- called_file (if report caller was enabled)
	- called_function (if report caller was enabled)
	- other fields (sorted alphabetically)
	- message
	*/
	FieldsOrder []string

	// TimestampFormat - default: "2006-01-02 15:04:05.000"
	TimestampFormat string

	// HideKeys - show [fieldValue] instead of [fieldKey:fieldValue]
	HideKeys bool

	// NoColors - disable colors
	NoColors bool

	// NoFieldsColors - apply colors only to the level, default is level + fields
	NoFieldsColors bool

	// NoFieldsSpace - no space between fields
	NoFieldsSpace bool

	// ShowFullLevel - show a full level [WARNING] instead of [WARN]
	ShowFullLevel bool

	// NoUppercaseLevel - no upper case for level value
	NoUppercaseLevel bool

	// TrimMessages - trim white spaces on messages
	TrimMessages bool
}

Usage

import (
	nested "github.com/SafeStudio/logrus-formatter"
	"github.com/sirupsen/logrus"
)

log := logrus.New()
log.SetFormatter(&nested.Formatter{
	HideKeys:    true,
	FieldsOrder: []string{"component", "category"},
})

log.Info("just info message")
// Output: 2006-01-02 15:04:05.000 [INFO] just info message

log.WithField("component", "rest").Warn("warn message")
// Output: 2006-01-02 15:04:05.000 [WARN] [rest] warn message

See more examples in the formatter_test.go file.

Development

# run tests:
make test

# run demo:
make demo

Documentation

Index

Constants

View Source
const (
	FieldTime           = "time"
	FieldLevel          = "level"
	FieldCalledFile     = "called_file"
	FieldCalledFunction = "called_function"
	FieldMessage        = "message"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	/**
	FieldsOrder
	Default:
	- time
	- level
	- called_file (if report caller was enabled)
	- called_function (if report caller was enabled)
	- other fields (sorted alphabetically)
	- message
	*/
	FieldsOrder []string

	// TimestampFormat - default: "2006-01-02 15:04:05.000"
	TimestampFormat string

	// HideKeys - show [fieldValue] instead of [fieldKey:fieldValue]
	HideKeys bool

	// NoColors - disable colors
	NoColors bool

	// NoFieldsColors - apply colors only to the level, default is level + fields
	NoFieldsColors bool

	// NoFieldsSpace - no space between fields
	NoFieldsSpace bool

	// ShowFullLevel - show a full level [WARNING] instead of [WARN]
	ShowFullLevel bool

	// NoUppercaseLevel - no upper case for level value
	NoUppercaseLevel bool

	// TrimMessages - trim white spaces on messages
	TrimMessages bool
}

Formatter - logrus formatter, implements logrus.Formatter

func (*Formatter) Format

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

Format an log entry

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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