logger

package module
Version: v0.0.0-...-63d0eda Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: MIT Imports: 13 Imported by: 11

README

Logger

A simple prefixer logger that can wrap several backends.

Interface

type Logger interface {
	WithPrefix(prefix string) Logger
	WithPrefixf(format string, args ...interface{}) Logger
	WithField(key string, value interface{}) Logger
	WithError(error error) Logger
	WithFields(fields map[string]interface{}) Logger
	//
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	//
	Print(args ...interface{})
	Printf(format string, args ...interface{})
	Println(args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatalln(args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	Panicln(args ...interface{})
}

Backends

  • Logrus with logger.WrapLogrus(l *logrus.Logger) function

Helpers

  • type M map[string]interface{} used as a shorthand for a map of interfaces used by WithFields method
  • func WithLogger(ctx context.Context, l Logger) context.Context to embeds the logger inside a context
  • func LogWith(ctx context.Context) Logger to extract a logger from a context

License

MIT

Contributing

All PRs are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithLogger

func WithLogger(ctx context.Context, l Logger) context.Context

WithLogger returns a new context that embeds the given logger.

Types

type ColorScheme

type ColorScheme struct {
	InfoLevelStyle  string
	WarnLevelStyle  string
	ErrorLevelStyle string
	FatalLevelStyle string
	PanicLevelStyle string
	DebugLevelStyle string
	PrefixStyle     string
	TimestampStyle  string
}

type ContextKey

type ContextKey string

A ContextKey is used to add data into a context.Context.

const LoggerKey ContextKey = "_logger"

LoggerKey is the storing key used for storing and retrieve the logger from a context.

type Logger

type Logger interface {
	WithPrefix(prefix string) Logger
	WithPrefixf(format string, args ...interface{}) Logger
	WithField(key string, value interface{}) Logger
	WithError(error error) Logger
	WithFields(fields map[string]interface{}) Logger
	//
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	//
	Print(args ...interface{})
	Printf(format string, args ...interface{})
	Println(args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatalln(args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	Panicln(args ...interface{})
}

A Logger is the interface used in this package for logging, so that any backend can be plugged in.

func LogWith

func LogWith(ctx context.Context) Logger

LogWith returns the logger extracted from the context. It panics if no logger inside the context.

func NewNullLogger

func NewNullLogger() Logger

NewNullLogger returns null Logger.

func WrapLogrus

func WrapLogrus(l *logrus.Logger) Logger

WrapLogrus returns Logger based on Logrus backend.

type LogrusTextFormatter

type LogrusTextFormatter struct {
	// Set to true to bypass checking for a TTY before outputting colors.
	ForceColors bool

	// Force disabling colors. For a TTY colors are enabled by default.
	DisableColors bool

	// Force formatted layout, even for non-TTY output.
	ForceFormatting bool

	// Disable timestamp logging. useful when output is redirected to logging
	// system that already adds timestamps.
	DisableTimestamp bool

	// Disable the conversion of the log levels to uppercase
	DisableUppercase bool

	// Enable logging the full timestamp when a TTY is attached instead of just
	// the time passed since beginning of execution.
	FullTimestamp bool

	// Timestamp format to use for display when a full timestamp is printed.
	TimestampFormat string

	// The fields are sorted by default for a consistent output. For applications
	// that log extremely frequently and don't use the JSON formatter this may not
	// be desired.
	DisableSorting bool

	// Wrap empty fields in quotes if true.
	QuoteEmptyFields bool

	// Can be set to the override the default quoting character "
	// with something else. For example: ', or `.
	QuoteCharacter string

	// Pad msg field with spaces on the right for display.
	// The value for this parameter will be the size of padding.
	// Its default value is zero, which means no padding will be applied for msg.
	SpacePadding int

	// Regexp to find prefix to be colored.
	// If not defined, no coloration is applied.
	// e.g. Use `^(\[.*?\])\s` to colorize prefix for message like "[prefix#1][prefix#2] The message"
	PrefixRE *regexp.Regexp

	sync.Once
	// contains filtered or unexported fields
}

A LogrusTextFormatter is pretty printer for Logrus. It supports github.com/mdouchement/logger prefix. Borrowed from https://github.com/x-cray/logrus-prefixed-formatter.

func (*LogrusTextFormatter) Format

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

Format implements logrus.Formatter.

func (*LogrusTextFormatter) SetColorScheme

func (f *LogrusTextFormatter) SetColorScheme(colorScheme *ColorScheme)

SetColorScheme setup the color scheme.

type M

type M map[string]interface{}

M defines a generic map of type `map[string]interface{}`.

Jump to

Keyboard shortcuts

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