consolepretty

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 9 Imported by: 4

Documentation

Overview

Package consolepretty is a concrete implementation of the logger.Sink and logger.Context used for outputting good looking human-readable logs to the console.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = New(DefaultConfig)

Default is a logger Sink that outputs human-readable logs to the console using its default settings.

View Source
var DefaultColorConfig = ColorConfig{
	Date:                color.New(color.FgHiBlack),
	Scope:               color.New(color.FgCyan, color.Bold),
	CallerFile:          color.New(color.FgHiBlack),
	CallerDelimiter:     color.New(color.FgHiBlack),
	CallerLine:          color.New(color.FgHiBlack),
	PreMessageDelimiter: color.New(color.FgWhite),
	MessageDebug:        color.New(color.FgHiBlack, color.Italic),
	MessageInfo:         color.New(color.FgHiWhite),
	MessageWarn:         color.New(color.FgHiYellow),
	MessageError:        color.New(color.FgRed),
	MessagePanic:        color.New(color.FgHiRed, color.Bold),
	LevelDebug:          color.New(color.FgHiBlack, color.Italic),
	LevelInfo:           color.New(color.FgGreen),
	LevelWarn:           color.New(color.FgYellow),
	LevelError:          color.New(color.FgRed, color.Bold),
	LevelPanic:          color.New(color.FgHiWhite, color.BgRed, color.Bold),
	FieldKey:            color.New(color.FgHiBlack, color.Italic),
	FieldDelimiter:      color.New(color.FgHiBlack, color.Italic),
	FieldValue:          color.New(color.FgWhite),
	FieldValueZero:      color.New(color.FgHiBlack, color.Italic),
	ErrorKey:            color.New(color.FgRed, color.Italic, color.Bold),
	ErrorDelimiter:      color.New(color.FgRed, color.Italic),
	ErrorValue:          color.New(color.FgHiRed),
	ErrorType:           color.New(color.FgRed, color.Italic),
}

DefaultColorConfig is the config used in New to populate some values if left unset. Changing this global value also changes the fallback values used in New.

View Source
var DefaultConfig = Config{
	Ellipsis:           "…",
	DateFormat:         "Jan-02 15:04Z0700",
	CallerMaxLength:    23,
	CallerMinLength:    23,
	ScopeMinLengthAuto: true,
}

DefaultConfig is the config used in New to populate some values if left unset. Changing this global value also changes the fallback values used in New.

Functions

func New

func New(conf Config) logger.Sink

New creates a new pretty-console logging Sink and uses fallback values from DefaultConfig and DefaultColorConfig for certain configs. Namely:

Config.Writer = DefaultConfig.Writer
Config.DateFormat = DefaultConfig.DateFormat

Config.Coloring = DefaultColorConfig
Example
package main

import (
	"github.com/iver-wharf/wharf-core/pkg/logger"
	"github.com/iver-wharf/wharf-core/pkg/logger/consolepretty"
)

func main() {
	defer logger.ClearOutputs()
	logger.AddOutput(logger.LevelDebug, consolepretty.New(consolepretty.Config{
		Prefix:            "foo:",
		DisableDate:       true,
		DisableCallerLine: true,
	}))

	logger.New().Debug().Message("Sample message.")

}
Output:

foo:[DEBUG|consolepretty/pretty_example_test.go] Sample message.

Types

type ColorConfig

type ColorConfig struct {
	// Date sets the color attributes for the timestamp of the logs.
	Date *color.Color
	// Scope sets the color attributes for the scope value of the logs.
	Scope *color.Color
	// CallerFile sets the color attributes for the caller file path of the logs.
	CallerFile *color.Color
	// CallerDelimiter sets the color attributes for the delimiter between the
	// caller file path and the caller line number of the logs.
	CallerDelimiter *color.Color
	// CallerLine sets the color attributes for the caller line number of the logs.
	CallerLine *color.Color
	// PreMessageDelimiter sets the color attributes for the delimiters between
	// the date timestamp, logging level, scope, and caller of the logs.
	PreMessageDelimiter *color.Color
	// MessageDebug sets the color attributes for the message on debug logs.
	MessageDebug *color.Color
	// MessageInfo sets the color attributes for the message on info logs.
	MessageInfo *color.Color
	// MessageWarn sets the color attributes for the message on warning logs.
	MessageWarn *color.Color
	// MessageError sets the color attributes for the message on error logs.
	MessageError *color.Color
	// MessagePanic sets the color attributes for the message on panic logs.
	MessagePanic *color.Color
	// LevelDebug sets the color attributes for the log level on debug logs.
	LevelDebug *color.Color
	// LevelInfo sets the color attributes for the log level on info logs.
	LevelInfo *color.Color
	// LevelWarn sets the color attributes for the log level on warning logs.
	LevelWarn *color.Color
	// LevelError sets the color attributes for the log level on error logs.
	LevelError *color.Color
	// LevelPanic sets the color attributes for the log level on panic logs.
	LevelPanic *color.Color
	// FieldKey sets the color attributes for the string key of each field added
	// via the Event.With* methods for the logs.
	FieldKey *color.Color
	// FieldDelimiter sets the color attributes for the delimiter between the
	// string key and the formatted value of each field added via the
	// Event.With* methods for the logs.
	FieldDelimiter *color.Color
	// FieldValue sets the color attributes for the formatted value of each
	// field added via the Event.With* methods for the logs for any non-zero
	// values.
	//
	// A zero-value here is more narrow than Go's definition. Here a zero-value
	// only refers to nil and empty strings.
	FieldValue *color.Color
	// FieldValueZero sets the color attributes for the formatted value of each
	// field added via the Event.With* methods for the logs for any zero
	// values.
	//
	// A zero-value here is more narrow than Go's definition. Here a zero-value
	// only refers to nil and empty strings.
	FieldValueZero *color.Color
	// ErrorKey sets the color attributes for the string key of the error added
	// via Event.WithError method for the logs.
	ErrorKey *color.Color
	// ErrorDelimiter sets the color attributes for the delimiter between the
	// string key and the formatted error string of the error added via
	// Event.WithError method for the logs.
	ErrorDelimiter *color.Color
	// ErrorValue sets the color attributes for the error string of the error
	// added via Event.WithError method for the logs.
	ErrorValue *color.Color
	// ErrorType sets the color attributes for the error type of the error
	// added via Event.WithError method for the logs.
	ErrorType *color.Color
}

ColorConfig lets you gradually configure the coloring of the logger.

type Config

type Config struct {
	// Writer is the io.Writer target that the pretty-console logger will write
	// to. Defaults to using a github.com/mattn/go-colorable wrapper around
	// os.Stdout.
	Writer io.Writer

	// Coloring defines how certain parts of the logs are colored.
	Coloring *ColorConfig

	// DateFormat is the format to display the timestamp of when a logged
	// message was logged. This does not alter how Event.WithTime is rendered.
	DateFormat string

	// Prefix sets an optional string added to the beginning of the log message.
	//
	// When set to "" (empty string):
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// When set to "foo:":
	// 	foo:Jan 02 15:04Z [INFO |example.go:20] Sample message.
	Prefix string

	// DisableDate removes the date field from the log when set to true.
	//
	// When set to false:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// When set to true:
	// 	[INFO |example.go:20] Sample message.
	DisableDate bool

	// DisableCaller removes the caller file name and line fields from the log
	// when set to true.
	//
	// When set to false:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// With set to true:
	// 	Jan 02 15:04Z [INFO ] Sample message.
	DisableCaller bool

	// DisableCallerLine removes just the caller line field from the log
	// when set to true, but leaves the caller file name as-is.
	//
	// When set to false:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// With set to true:
	// 	Jan 02 15:04Z [INFO |example.go] Sample message.
	DisableCallerLine bool

	// Ellipsis defines the string used when trimming the values, as an effect
	// of the caller or scope max length configs.
	//
	// Setting this to a value longer than the max length is considered
	// undefined behavior, and should be avoided.
	Ellipsis string

	// CallerMaxLength will trim the caller file and line down to this length
	// if set to a value of 1 or higher.
	//
	// When set to 0:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// With set to 10:
	// 	Jan 02 15:04Z [INFO |…ple.go:20] Sample message.
	CallerMaxLength int

	// CallerMinLength will pad the caller file and line with spaces so that it
	// reaches the target character width.
	//
	// When set to 0:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// 	Jan 02 15:04Z [INFO |test.go:20] Sample message.
	// With set to 13:
	// 	Jan 02 15:04Z [INFO |example.go:20] Sample message.
	// 	Jan 02 15:04Z [INFO |test.go:20   ] Sample message.
	CallerMinLength int

	// ScopeMaxLength will trim the scope down to this length if set to a value
	// of 1 or higher.
	//
	// When set to 0:
	// 	Jan 02 15:04Z [INFO |GORM-debug] Sample message.
	// With set to 5:
	// 	Jan 02 15:04Z [INFO |GORM…] Sample message.
	ScopeMaxLength int

	// ScopeMinLength will pad the scope with spaces so that it reaches the
	// target character width.
	//
	// When set to 0:
	// 	Jan 02 15:04Z [INFO |GORM] Sample message.
	// 	Jan 02 15:04Z [INFO |GORM-debug] Sample message.
	// With set to 12:
	// 	Jan 02 15:04Z [INFO |GORM        ] Sample message.
	// 	Jan 02 15:04Z [INFO |GORM-debug  ] Sample message.
	ScopeMinLength int

	// ScopeMinLengthAuto will automatically pad the scope with spaces to
	// accommodate for the longest scope created by logger.NewScoped.
	//
	// When set to false:
	// 	Jan 02 15:04Z [INFO |GORM] Sample message.
	// 	Jan 02 15:04Z [INFO |GORM-debug] Sample message.
	// With set to true:
	// 	Jan 02 15:04Z [INFO |GORM      ] Sample message.
	// 	Jan 02 15:04Z [INFO |GORM-debug] Sample message.
	ScopeMinLengthAuto bool
}

Config lets you gradually configure the output of the logger by disabling certain features or changing the format of certain field types.

Jump to

Keyboard shortcuts

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