formatter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2019 License: MIT Imports: 9 Imported by: 0

README

Colored Nested Logrus Formatter

Build Status Go Report Card Coverage Status License

Human readable log formatter for the logrus library. Option to define custom colors for specific field matches.

Usage

You can either register it for every log or create your new log instance and use the SetFormatter function of the logrus Log.

Example code:

package main

import (
	formatter "github.com/DaRealFreak/colored-nested-formatter"
	"github.com/sirupsen/logrus"
	"time"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&formatter.Formatter{
		DisableColors:            false,
		ForceColors:              false,
		DisableTimestamp:         false,
		UseUppercaseLevel:        false,
		UseTimePassedAsTimestamp: false,
		TimestampFormat:          time.StampMilli,
		PadAllLogEntries:         true,
	})

	formatter.AddFieldMatchColorScheme("color", &formatter.FieldMatch{
		Value: "blue",
		Color: "232:33",
	})
	formatter.AddFieldMatchColorScheme("moreColorFields", &formatter.FieldMatch{
		Value: "green",
		Color: "232:34",
	})

	log.Info("normal info log entry")
	log.WithField("color", "unregistered").Infof("normal colored not nested info log entry")
	log.WithField("color", "blue").Info("blue colored nested info log entry")
	log.WithFields(logrus.Fields{"color": "blue", "moreColorFields": "green"}).Info(
		"blue and green colored nested info log entry",
	)
}

The above code will output this: Example Output

Configuration

You have multiple configuration options for the formatter for whatever use case you need it for:

type Formatter struct {
	// timestamp formatting, default is time.RFC3339
	TimestampFormat string
	// color schema for messages
	ColorSchema *ColorSchema
	// no colors
	DisableColors bool
	// no check for TTY terminal
	ForceColors bool
	// no timestamp
	DisableTimestamp bool
	// false -> time passed, true -> timestamp
	UseTimePassedAsTimestamp bool
	// false -> info, true -> INFO
	UseUppercaseLevel bool
	// reserves space for all log entries for all registered matches
	PadAllLogEntries bool
}

Development

Want to contribute? Great!
I'm always glad hearing about bugs or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Overview

Package formatter implements a formatter compatible with the logrus logger library

Index

Constants

This section is empty.

Variables

View Source
var (
	FieldMatchColorScheme map[string][]*FieldMatch
)

nolint: gochecknoglobals

Functions

func AddFieldMatchColorScheme

func AddFieldMatchColorScheme(key string, match *FieldMatch)

AddFieldMatchColorScheme registers field match color scheme

Types

type ColorSchema

type ColorSchema struct {
	Timestamp  string
	InfoLevel  string
	WarnLevel  string
	ErrorLevel string
	FatalLevel string
	PanicLevel string
	DebugLevel string
}

ColorSchema is the color schema for the default log parts/levels

type FieldMatch

type FieldMatch struct {
	Value interface{}
	Color string
}

FieldMatch contains the value and defined color of the field match

type Formatter

type Formatter struct {
	// timestamp formatting, default is time.RFC3339
	TimestampFormat string
	// color schema for messages
	ColorSchema *ColorSchema
	// no colors
	DisableColors bool
	// no check for TTY terminal
	ForceColors bool
	// no timestamp
	DisableTimestamp bool
	// false -> time passed, true -> timestamp
	UseTimePassedAsTimestamp bool
	// false -> info, true -> INFO
	UseUppercaseLevel bool
	// reserves space for all log entries for all registered matches
	PadAllLogEntries bool
}

Formatter contains all options for this formatter

func (*Formatter) Format

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

Format implements the interface method of the logrus Formatter

Jump to

Keyboard shortcuts

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