logger

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: MIT Imports: 5 Imported by: 0

README

Simple structured logger for Go

A simple logger package that provides levels, a number of output formats, and named sub-logs. Output formats include key/value, JSON and AMQP/RabbitMQ

Installation

Install using go get github.com/felix/logger.

Documentation is available at http://godoc.org/github.com/felix/logger

Usage

Create a key/value logger
log := logger.New(logger.Name("app"), logger.Level(logger.DEBUG))
log.Error("unable to do anything")
... [info] app: unable to do anything
log.Warn("invalid something", "id", 344, "error", "generally broken")
Add structure
... [warn] app: invalid something id=344 error="generally broken"
Create a sub-logger
sublog := log.GetNamed("database")
sublog.Info("connection initialised")
... [info] app.database: connection initialised
Create a new Logger with pre-defined values

For major sub-systems there is no need to repeat values for each log call:

reqID := "555"
msgLog := sublog.WithField("request", reqID)
msgLog.Error("failed to process message")
... [info] app.database: failed to process message request=555

Comparison

BenchmarkCoreLogger-12           5000000               288 ns/op
BenchmarkLocal-12                2000000               654 ns/op
BenchmarkLogrus-12               1000000              1738 ns/op
BenchmarkFieldsLocal-12          1000000              1024 ns/op
BenchmarkFieldsLogrus-12         1000000              2061 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...interface{})

Debug logs a debug message.

func Error

func Error(msg string, args ...interface{})

Error logs an error message.

func Info

func Info(msg string, args ...interface{})

Info logs an information message.

func IsDebug added in v0.3.0

func IsDebug() bool

IsDebug determines the debug status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func IsInfo added in v0.3.0

func IsInfo() bool

IsInfo determines the info status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func IsWarn added in v0.3.0

func IsWarn() bool

IsWarn determines the info status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func SetField added in v0.3.0

func SetField(k string, v interface{})

SetField enables changing the default fields for a logger instance.

func SetLevel added in v0.3.0

func SetLevel(lvl message.Level)

SetLevel enables changing the minimum level for a logger instance.

func SetLevelString added in v0.3.0

func SetLevelString(lvl string)

SetLevelString enables changing the minimum level for a logger instance.

func SetName added in v0.3.0

func SetName(n string)

SetName enables changing the name for a logger instance.

func Warn

func Warn(msg string, args ...interface{})

Warn logs an information message.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a simple levelled logger.

func GetNamed added in v0.3.0

func GetNamed(n string) *Logger

GetNamed creates a new instance of a logger with a new name.

func New

func New(opts ...Option) (*Logger, error)

New creates a new logger instance

func (*Logger) Configure added in v0.3.0

func (l *Logger) Configure(opts ...Option) error

Configure applies settings to the logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...interface{})

Debug logs a debug message.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...interface{})

Error logs an error message.

func (*Logger) GetNamed added in v0.3.0

func (l *Logger) GetNamed(n string) *Logger

GetNamed creates a new instance of a logger with a new name.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...interface{})

Info logs an information message.

func (*Logger) IsDebug

func (l *Logger) IsDebug() bool

IsDebug determines the debug status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func (*Logger) IsInfo

func (l *Logger) IsInfo() bool

IsInfo determines the info status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func (*Logger) IsWarn

func (l *Logger) IsWarn() bool

IsWarn determines the info status for a logger instance. Use this to conditionally execute blocks of code depending on the log verbosity.

func (*Logger) Log

func (l *Logger) Log(lvl message.Level, msg string, args ...interface{})

Log for a logger instance

func (*Logger) SetField added in v0.3.0

func (l *Logger) SetField(k string, v interface{})

SetField enables changing the default fields for a logger instance.

func (*Logger) SetLevel added in v0.3.0

func (l *Logger) SetLevel(lvl message.Level)

SetLevel enables changing the minimum level for a logger instance.

func (*Logger) SetLevelString added in v0.3.0

func (l *Logger) SetLevelString(lvl string)

SetLevelString enables changing the minimum level for a logger instance.

func (*Logger) SetName added in v0.3.0

func (l *Logger) SetName(n string)

SetName enables changing the name for a logger instance.

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...interface{})

Warn logs an information message.

type Option added in v0.3.0

type Option func(*Logger) error

An Option configures a logger

func Field added in v0.3.0

func Field(k string, v interface{}) Option

Field configures an initial field of the logger.

func Fields added in v0.3.0

func Fields(f map[string]interface{}) Option

Fields configures an initial set of fields of the logger.

func Level

func Level(lvl message.Level) Option

Level configures the minimum level to log.

func LevelString added in v0.3.0

func LevelString(lvl string) Option

LevelString configures the minimum level to log.

func Name added in v0.3.0

func Name(n string) Option

Name configures the name of the logger.

func Writer added in v0.3.0

func Writer(f message.Writer) Option

Writer add an output formatter for the logger.

Directories

Path Synopsis
writers
kv

Jump to

Keyboard shortcuts

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