formatter

package module
v0.0.0-...-c4d8c1a Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 7 Imported by: 0

README

logrus-formatter-nested

Build Status Go Report Card GoDoc

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

Screenshot

Configuration:

type Formatter struct {
	// FieldsOrder - default: fields sorted alphabetically
	FieldsOrder []string

	// TimestampFormat - default: time.StampMilli = "Jan _2 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 whitespaces on messages
	TrimMessages bool

	// CallerFirst - print caller info first
	CallerFirst bool

	// CustomCallerFormatter - set custom formatter for caller info
	CustomCallerFormatter func(*runtime.Frame) string
}

Usage

import (
	nested "github.com/andoma-go/logrus-formatter-nested"
	"github.com/andoma-go/logrus"
)

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

log.Info("just info message")
// Output: Jan _2 15:04:05.000 [INFO] just info message

log.WithField("component", "rest").Warn("warn message")
// Output: Jan _2 15:04:05.000 [WARN] [rest] warn message

See more examples in the tests file.

Development

# run tests:
make test

# run demo:
make demo

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	// FieldsOrder - default: fields sorted alphabetically
	FieldsOrder []string

	// TimestampFormat - default: time.StampMilli = "Jan _2 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 whitespaces on messages
	TrimMessages bool

	// CallerFirst - print caller info first
	CallerFirst bool

	// CustomCallerFormatter - set custom formatter for caller info
	CustomCallerFormatter func(*runtime.Frame) string
}

Formatter - logrus formatter, implements logrus.Formatter

func (*Formatter) Format

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

Format an log entry

Example (Default)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
	})

	l.Debug("test1")
	l.Info("test2")
	l.Warn("test3")
	l.Error("test4")

}
Output:

- [DEBU] test1
- [INFO] test2
- [WARN] test3
- [ERRO] test4
Example (Field_order)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		FieldsOrder:     []string{"component", "category"},
		HideKeys:        false,
	})

	ll := l.WithField("component", "main")
	lll := ll.WithField("category", "rest")

	l.Info("test1")
	ll.Info("test2")
	lll.Info("test3")

}
Output:

- [INFO] test1
- [INFO] [component:main] test2
- [INFO] [component:main] [category:rest] test3
Example (Full_level)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		ShowFullLevel:   true,
	})

	l.Debug("test1")
	l.Info("test2")
	l.Warn("test3")
	l.Error("   test4")

}
Output:

- [DEBUG] test1
- [INFO] test2
- [WARNING] test3
- [ERROR]    test4
Example (Hide_keys)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		HideKeys:        true,
	})

	ll := l.WithField("category", "rest")

	l.Info("test1")
	ll.Info("test2")

}
Output:

- [INFO] test1
- [INFO] [rest] test2
Example (No_fields_space)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		FieldsOrder:     []string{"component", "category"},
		HideKeys:        false,
		NoFieldsSpace:   true,
	})

	ll := l.WithField("component", "main")
	lll := ll.WithField("category", "rest")

	l.Info("test1")
	ll.Info("test2")
	lll.Info("test3")

}
Output:

- [INFO] test1
- [INFO][component:main] test2
- [INFO][component:main][category:rest] test3
Example (No_uppercase_level)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:         true,
		TimestampFormat:  "-",
		FieldsOrder:      []string{"component", "category"},
		NoUppercaseLevel: true,
	})

	ll := l.WithField("component", "main")
	lll := ll.WithField("category", "rest")
	llll := ll.WithField("category", "other")

	l.Debug("test1")
	ll.Info("test2")
	lll.Warn("test3")
	llll.Error("test4")

}
Output:

- [debu] test1
- [info] [component:main] test2
- [warn] [component:main] [category:rest] test3
- [erro] [component:main] [category:other] test4
Example (Show_keys)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		HideKeys:        false,
	})

	ll := l.WithField("category", "rest")

	l.Info("test1")
	ll.Info("test2")

}
Output:

- [INFO] test1
- [INFO] [category:rest] test2
Example (Sort_order)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		NoColors:        true,
		TimestampFormat: "-",
		HideKeys:        false,
	})

	ll := l.WithField("component", "main")
	lll := ll.WithField("category", "rest")

	l.Info("test1")
	ll.Info("test2")
	lll.Info("test3")

}
Output:

- [INFO] test1
- [INFO] [component:main] test2
- [INFO] [category:rest] [component:main] test3
Example (Trim_message)
package main

import (
	"os"

	"github.com/andoma-go/logrus"
	nested "github.com/andoma-go/logrus-formatter-nested"
)

func main() {
	l := logrus.New()
	l.SetOutput(os.Stdout)
	l.SetLevel(logrus.DebugLevel)
	l.SetFormatter(&nested.Formatter{
		TrimMessages:    true,
		NoColors:        true,
		TimestampFormat: "-",
	})

	l.Debug(" test1 ")
	l.Info("test2 ")
	l.Warn(" test3")
	l.Error("   test4   ")

}
Output:

- [DEBU] test1
- [INFO] test2
- [WARN] test3
- [ERRO] test4

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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