log

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2019 License: MIT Imports: 15 Imported by: 9

Documentation

Index

Constants

View Source
const (
	FieldLevel      = "level"
	FieldTimestamp  = "timestamp"
	FieldMessage    = "message"
	FieldLoggerName = "loggerName"
)
View Source
const LoggerLogrus = "logrus"

LoggerLogrus indicates the name of the predefined logrus ILogger implementation

View Source
const LoggerNil = "nil"

LoggerNil indicates the name of the predefined nil ILogger implementation which does nothing when the log functions are invoked.

View Source
const (
	TemplateDefault = `` /* 132-byte string literal not displayed */
)

Variables

View Source
var (
	TerminalThemeDefault = &TerminalTheme{
		Template: `{{ LoggerName . }}{{ Level . }}{{ Timestamp " HH:mm:ss " . }} {{ .Message }}`,
		Schemes: TerminalColorScheme{
			"loggerName": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				InfoLevel:  []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				WarnLevel:  []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				FatalLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				PanicLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
			},
			"level": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Bold, fmtc.DarkGray},
				InfoLevel:  []fmtc.Color{fmtc.Bold, fmtc.Cyan},
				WarnLevel:  []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.Bold, fmtc.Red},
				FatalLevel: []fmtc.Color{fmtc.Bold, fmtc.Red},
				PanicLevel: []fmtc.Color{fmtc.Bold, fmtc.Red},
			},
			"timestamp": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.DarkGray},
				InfoLevel:  []fmtc.Color{fmtc.Cyan},
				WarnLevel:  []fmtc.Color{fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.Red},
				FatalLevel: []fmtc.Color{fmtc.Red},
				PanicLevel: []fmtc.Color{fmtc.Red},
			},
		},
	}

	TerminalThemeAlternative = &TerminalTheme{
		Template: `{{ LoggerName . }}{{ Scheme "level" (string " " .Level " ") . }}{{ Timestamp " HH:mm:ss " . }} {{ .Message }}`,
		Schemes: TerminalColorScheme{
			"loggerName": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				InfoLevel:  []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				WarnLevel:  []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				FatalLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
				PanicLevel: []fmtc.Color{fmtc.Bold, fmtc.Yellow},
			},
			"level": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Bold, fmtc.DarkGray},
				InfoLevel:  []fmtc.Color{fmtc.Bold, fmtc.White, fmtc.BgBlue},
				WarnLevel:  []fmtc.Color{fmtc.Black, fmtc.BgYellow},
				ErrorLevel: []fmtc.Color{fmtc.Bold, fmtc.White, fmtc.BgRed},
				FatalLevel: []fmtc.Color{fmtc.Bold, fmtc.White, fmtc.BgRed},
				PanicLevel: []fmtc.Color{fmtc.Bold, fmtc.White, fmtc.BgRed},
			},
			"timestamp": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.BgBlack, fmtc.DarkGray},
				InfoLevel:  []fmtc.Color{fmtc.BgBlack, fmtc.Cyan},
				WarnLevel:  []fmtc.Color{fmtc.BgBlack, fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.BgBlack, fmtc.Red},
				FatalLevel: []fmtc.Color{fmtc.BgBlack, fmtc.Red},
				PanicLevel: []fmtc.Color{fmtc.BgBlack, fmtc.Red},
			},
		},
	}

	TerminalThemeCliApp = &TerminalTheme{
		Template: `{{ Timestamp " HH:mm:ss " . }} {{ Message . "           " }}`,
		Schemes: TerminalColorScheme{
			"timestamp": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Gray},
				InfoLevel:  []fmtc.Color{fmtc.Cyan},
				WarnLevel:  []fmtc.Color{fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.Red},
				FatalLevel: []fmtc.Color{fmtc.Red},
				PanicLevel: []fmtc.Color{fmtc.Red},
			},
			"message": LevelColorScheme{
				DebugLevel: []fmtc.Color{fmtc.Gray},
				InfoLevel:  []fmtc.Color{fmtc.White},
				WarnLevel:  []fmtc.Color{fmtc.Yellow},
				ErrorLevel: []fmtc.Color{fmtc.LightRed},
				FatalLevel: []fmtc.Color{fmtc.LightRed},
				PanicLevel: []fmtc.Color{fmtc.LightRed},
			},
		},
	}
)

Functions

func Contains

func Contains(name string) bool

Contains indicates if a logger by the given name exists.

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the default logger.

func DebugObj added in v1.0.3

func DebugObj(obj interface{})

DebugObj logs a debug message of a json representation of the provided object. Does nothing if the object is nil.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at level Debug on the default logger.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the default logger.

func ErrorErr added in v1.0.3

func ErrorErr(err error, message ...interface{})

ErrorErr logs an error using the provided message and error if the error is not nil. Does nothing if the error is nil

func ErrorErrf added in v1.0.3

func ErrorErrf(err error, format string, message ...interface{})

ErrorErrf logs an error with a string format using the provided message and error if the error is not nil. Does nothing if the error is nil

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at level Error on the default logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the default logger.

func FatalErr added in v1.0.3

func FatalErr(err error, message ...interface{})

FatalErr logs a fatal error using the provided message and error if the error is not nil. Does nothing if the error is nil

func FatalErrf added in v1.0.3

func FatalErrf(err error, format string, message ...interface{})

FatalErrf logs an fatal error with a string format using the provided message and error if the error is not nil. Does nothing if the error is nil

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a message at level Fatal on the default logger.

func Info

func Info(args ...interface{})

Info logs a message at level Info on the default logger.

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at level Info on the default logger.

func List

func List() []string

List returns the list of loggers that have been registered.

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the default logger.

func PanicErr added in v1.0.3

func PanicErr(err error, message ...interface{})

PanicErr logs a panic error using the provided message and error if the error is not nil. Does nothing if the error is nil

func PanicErrf added in v1.0.3

func PanicErrf(err error, format string, message ...interface{})

PanicErrf logs an panic error with a string format using the provided message and error if the error is not nil. Does nothing if the error is nil

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a message at level Panic on the default logger.

func SetDefault

func SetDefault(logger ILogger)

SetDefault sets a logger instance as the default logger.

func SetDefaultBuilder

func SetDefaultBuilder(ctor LoggerBuilder)

SetDefaultBuilder assigns the default builder to be used when creating new loggers.

func SetFormatter

func SetFormatter(formatter IFormatter)

SetFormatter sets a custom formatter to display the logs

func SetLevel

func SetLevel(level Level)

SetLevel sets the default logger level.

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the default logger.

func WarnErr added in v1.0.3

func WarnErr(err error, message ...interface{})

WarnErr logs a warning using the provided message and error if the error is not nil. Does nothing if the error is nil

func WarnErrf added in v1.0.3

func WarnErrf(err error, format string, message ...interface{})

WarnErrf logs a warning with a string format using the provided message and error if the error is not nil. Does nothing if the error is nil

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a message at level Warn on the default logger.

Types

type BaseFormatter

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

BaseFormatter base structure for formatters

func (*BaseFormatter) SetTemplate

func (f *BaseFormatter) SetTemplate(tmpl string) error

type BaseTerminalFormatter

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

	// Force disabling colors.
	DisableColors bool

	// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
	EnvironmentOverrideColors bool
	// contains filtered or unexported fields
}

BaseTerminalFormatter base structure to create formatters for a terminal

func (*BaseTerminalFormatter) SetTheme

func (f *BaseTerminalFormatter) SetTheme(scheme *TerminalTheme)

type Entry

type Entry struct {
	// LoggerName indicates to what logger the log entry belongs to
	LoggerName string

	// Contains all the fields set by the user. TODO
	Data map[string]interface{}

	// Time at which the log entry was created
	Timestamp time.Time

	// Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic
	Level Level

	// Message passed to Debug, Info, Warn, Error, Fatal or Panic
	Message string
	// contains filtered or unexported fields
}

Entry represents a log entry.

func (*Entry) AddMetadata

func (e *Entry) AddMetadata(key string, val interface{})

type IFormatter

type IFormatter interface {
	Format(io.Writer, *Entry) error
	SetTemplate(string) error
}

The Formatter interface is used to implement a custom Formatter. It takes an `Entry`. It exposes all the fields, including the default ones:

* `entry.Data["msg"]`. The message passed from Info, Warn, Error .. * `entry.Data["time"]`. The timestamp. * `entry.Data["level"]. The level the entry was logged at.

Any additional fields added with `WithField` or `WithFields` are also in `entry.Data`. Format is expected to return an array of bytes which are then logged to `logger.Out`.

type ILogger

type ILogger interface {
	// Name returns the manager name
	Name() string

	// SetLevel sets the logging level
	SetLevel(level Level)
	// GetLevel gets the logging level
	GetLevel() Level

	// Debug logs a message at level Debug on the logger.
	Debug(args ...interface{})
	// Debugf logs a message at level Debug on the logger.
	Debugf(format string, args ...interface{})

	// Info logs a message at level Info on the logger.
	Info(args ...interface{})
	// Infof logs a message at level Info on the logger.
	Infof(format string, args ...interface{})

	// Warn logs a message at level Warn on the logger.
	Warn(args ...interface{})
	// Warnf logs a message at level Warn on the logger.
	Warnf(format string, args ...interface{})

	// Error logs a message at level Error on the logger.
	Error(args ...interface{})
	// Errorf logs a message at level Error on the logger.
	Errorf(format string, args ...interface{})

	// Fatal logs a message at level Fatal on the logger.
	Fatal(args ...interface{})
	// Fatalf logs a message at level Fatal on the logger.
	Fatalf(format string, args ...interface{})

	// Panic logs a message at level Panic on the logger.
	Panic(args ...interface{})
	// Panicf logs a message at level Panic on the logger.
	Panicf(format string, args ...interface{})

	// SetFormatter sets a custom formatter to display the logs
	SetFormatter(formatter IFormatter)
}

ILogger defines the contract for a logger interface to be used by the mgo and mongo packages. This interface matches most commonly used loggers which should make it simple to assign any logger implementation being used. By default it uses the sirupsen/logrus standard logger implementation.

func Default

func Default() ILogger

GetDefault returns the the instance currently set as the default logger

func Get

func Get(name string) ILogger

Get returns an instance of the requested logger by its name. Returns the Nil Logger implementation if a logger by the given name is not found.

{name} - The name of the logger instance to be retrieved.

func New

func New(name string, writer ...io.Writer) ILogger

New creates a new logger instance using the default builder assigned.

{name}   - The name of the logger to create.
{writer} - (Optional) The io.Writer the logger instance should use. If not provided,
           it is set to the default writer by the implementation, typically Stdout or Stderr

func NewLogrus

func NewLogrus(name string, writer ...io.Writer) ILogger

NewLogrus creates a new instance of the logrus implementation of ILogger

func NewMemory added in v1.0.4

func NewMemory(name string, writer ...io.Writer) ILogger

NewMemory returns a logger implementation that keeps the logs in memory until flushed. In this case, when providing an io.Writer, that would be the writer where all the stored logs would be flushed into. Uses os.Stdout if no writer is provided

func NewNil

func NewNil() ILogger

NewNil creates a new instance of the Nil logger

func Register

func Register(name string, logger ILogger) ILogger

Register registers an instance of ILogger to be returned as the singleton instance by the given name.

{name}   - The logger name.
{logger} - The logger instance.

type ILoggerAsync added in v1.0.4

type ILoggerAsync interface {
	ILogger
	Flush(clear ...bool)
	Reset()
}

type Level

type Level uint32

Level type

const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger, obtained with `logrus.New()`.

func GetLevel

func GetLevel() Level

GetLevel returns the default logger level.

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the Logrus log level constant.

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. PanicLevel becomes "panic".

type LevelColorScheme

type LevelColorScheme map[Level][]fmtc.Color

LevelColorScheme represents the terminal colors associated to the level parsing for each logging level.

type LoggerBuilder

type LoggerBuilder func(name string, writer ...io.Writer) ILogger

LoggerBuilder defines a logger constructor. The factory contains multiple logger constructors where new loggers with specified names can be created. Also these loggers can have their own io.Writer.

type TerminalColorScheme

type TerminalColorScheme map[string]LevelColorScheme

TerminalColorScheme defines terminal colors that are tied to a log level and a field.

type TerminalFormatter

type TerminalFormatter struct {
	BaseTerminalFormatter
}

TextFormatter formats logs into text

func NewTerminalFormatter

func NewTerminalFormatter() *TerminalFormatter

func (*TerminalFormatter) Format

func (f *TerminalFormatter) Format(writer io.Writer, entry *Entry) error

Format renders a single log entry

type TerminalTheme

type TerminalTheme struct {
	Template string
	Schemes  TerminalColorScheme
}

TerminalTheme contains the logging theme configuration for terminal logging

Jump to

Keyboard shortcuts

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