log

package
v0.0.0-...-15ea2ad Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2018 License: BSD-3-Clause Imports: 15 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultFormatter is the default formatter used and is only the message.
	DefaultFormatter = MustStringFormatter("{{ .Message }}")

	// FancyFormatter is the default formatter for fancy messages.
	FancyFormatter = MustStringFormatter(`[{{formatColorString "magentafg" (formatTime .Timestamp "2006-01-02 15:04:05")}}] {{formatColor .LevelColor .LevelTitle "\t>"}} {{ .Message }}`)
)
View Source
var Levels = map[Level]LevelInfo{
	FATAL: {Level: FATAL, Color: aurora.RedFg, Name: "Fatal"},
	ERROR: {Level: ERROR, Color: aurora.RedFg, Name: "Error"},
	WARN:  {Level: WARN, Color: aurora.BrownFg, Name: "Warn"},
	INFO:  {Level: INFO, Color: aurora.CyanFg, Name: "Info"},
	DEBUG: {Level: DEBUG, Color: aurora.MagentaFg, Name: "Debug"},
}
View Source
var Log = &Logger{
	Reporters: []Reporter{stdLog{}},
	Level:     INFO,
}

singletons ftw?

Functions

func AddReporter

func AddReporter(r Reporter)

AddReporter adds a new reporter to the logger

func Debug

func Debug(msg string)

Debug level message.

func Debugf

func Debugf(msg string, v ...interface{})

Debugf level formatted message.

func Error

func Error(msg string)

Error level message.

func Errorf

func Errorf(msg string, v ...interface{})

Errorf level formatted message.

func Fatal

func Fatal(msg string)

Fatal level message, followed by an exit.

func Fatalf

func Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func Info

func Info(msg string)

Info level message.

func Infof

func Infof(msg string, v ...interface{})

Infof level formatted message.

func SetLevel

func SetLevel(l Level)

SetLevel sets the log level. This is not thread-safe.

func SetLevelFromString

func SetLevelFromString(s string)

SetLevelFromString sets the log level from a string, panicing when invalid. This is not thread-safe.

func Warn

func Warn(msg string)

Warn level message.

func Warnf

func Warnf(msg string, v ...interface{})

Warnf level formatted message.

Types

type Entry

type Entry struct {
	Logger    *Logger
	Level     Level
	Message   string
	Formatted map[string]interface{}
	Fields    Fields
	Timestamp time.Time
	// contains filtered or unexported fields
}

Entry represents a single log entry.

func NewEntry

func NewEntry(log *Logger) *Entry

NewEntry returns a new entry for `log`.

func Trace

func Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func WithError

func WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func WithField

func WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func WithFields

func WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func (*Entry) Debug

func (e *Entry) Debug(msg string)

Debug level message.

func (*Entry) Debugf

func (e *Entry) Debugf(msg string, v ...interface{})

Debugf level formatted message.

func (*Entry) Error

func (e *Entry) Error(msg string)

Error level message.

func (*Entry) Errorf

func (e *Entry) Errorf(msg string, v ...interface{})

Errorf level formatted message.

func (*Entry) Fatal

func (e *Entry) Fatal(msg string)

Fatal level message, followed by an exit.

func (*Entry) Fatalf

func (e *Entry) Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func (*Entry) Info

func (e *Entry) Info(msg string)

Info level message.

func (*Entry) Infof

func (e *Entry) Infof(msg string, v ...interface{})

Infof level formatted message.

func (*Entry) Stop

func (e *Entry) Stop(err *error)

Stop should be used with Trace, to fire off the completion message. When an `err` is passed the "error" field is set, and the write level is error.

func (*Entry) Trace

func (e *Entry) Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion write, useful with defer.

func (*Entry) Warn

func (e *Entry) Warn(msg string)

Warn level message.

func (*Entry) Warnf

func (e *Entry) Warnf(msg string, v ...interface{})

Warnf level formatted message.

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

The given error may implement .Fielder, if it does the method will add all its `.Fields()` into the returned entry.

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

type Fielder

type Fielder interface {
	Fields() Fields
}

Fielder is an interface for providing fields to custom types.

type Fields

type Fields map[string]interface{}

Fields represents a map of entry level data used for structured logging.

func (Fields) Fields

func (f Fields) Fields() Fields

Fields implements Fielder.

func (Fields) Get

func (f Fields) Get(name string) interface{}

Get field value by name.

func (Fields) Names

func (f Fields) Names() (v []string)

Names returns field names sorted.

type Formatter

type Formatter interface {
	Format(*Entry, int) map[string]interface{}
	Finalize(map[string]interface{}) (string, error)
}

func MustStringFormatter

func MustStringFormatter(format string) Formatter

MustStringFormatter is equivalent to NewStringFormatter with a call to panic on error.

func NewStringFormatter

func NewStringFormatter(format string) (Formatter, error)

type Level

type Level int
const (
	FATAL Level = iota
	ERROR
	WARN
	INFO
	DEBUG
)

func GetLevelFromString

func GetLevelFromString(s string) Level

GetLevelFromString returns the log level from a string, panicing when invalid

type LevelInfo

type LevelInfo struct {
	Level Level
	Color aurora.Color
	Name  string
}

type Logger

type Logger struct {
	*sync.Mutex
	Level     Level
	Reporters []Reporter
}

func NewLogger

func NewLogger(level Level, reporters []Reporter) *Logger

NewLogger creates a new logger

func (*Logger) Debug

func (l *Logger) Debug(msg string)

Debug level message.

func (*Logger) Debugf

func (l *Logger) Debugf(msg string, v ...interface{})

Debugf level formatted message.

func (*Logger) Error

func (l *Logger) Error(msg string)

Error level message.

func (*Logger) Errorf

func (l *Logger) Errorf(msg string, v ...interface{})

Errorf level formatted message.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

Fatal level message, followed by an exit.

func (*Logger) Fatalf

func (l *Logger) Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info level message.

func (*Logger) Infof

func (l *Logger) Infof(msg string, v ...interface{})

Infof level formatted message.

func (*Logger) Trace

func (l *Logger) Trace(msg string) *Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

Warn level message.

func (*Logger) Warnf

func (l *Logger) Warnf(msg string, v ...interface{})

Warnf level formatted message.

func (*Logger) WithError

func (l *Logger) WithError(err error) *Entry

WithError returns a new entry with the "error" set to `err`.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Entry

WithField returns a new entry with the `key` and `value` set.

Note that the `key` should not have spaces in it - use camel case or underscores

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fielder) *Entry

WithFields returns a new entry with `fields` set.

func (*Logger) Write

func (l *Logger) Write(level Level, e *Entry, msg string, calldepth int) *Logger

type Reporter

type Reporter interface {
	Write(e *Entry, calldepth int) error
}

Directories

Path Synopsis
examples
cli
handlers
cli

Jump to

Keyboard shortcuts

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