Documentation ¶
Index ¶
- Variables
- type Buffer
- type Err
- type Event
- type Formatter
- type FormatterFunc
- type JsonFormatter
- type LazyMessage
- type Level
- type LevelFilter
- type LevelInfo
- type Logger
- func NewColoredTextLogger(level Level, writer io.Writer) *Logger
- func NewFormattedLogger(level Level, writer io.Writer, formatter Formatter) *Logger
- func NewLogger(level Level, subscriber Subscriber) (logger *Logger)
- func NewPlainTextLogger(level Level, writer io.Writer) *Logger
- func NewStderrLogger(level Level) *Logger
- func NewStdoutLogger(level Level) *Logger
- func NewTextLogger(level Level, file *os.File) *Logger
- type NullFormatter
- type NullSubscriber
- type Producer
- func (p *Producer) Debug(message string, a ...interface{})
- func (p *Producer) Error(message string, a ...interface{})
- func (p *Producer) Fatal(message string, a ...interface{})
- func (p *Producer) Info(message string, a ...interface{})
- func (p *Producer) Log(level Level, message string, a []interface{})
- func (p *Producer) Trace(message string, a ...interface{})
- func (p *Producer) Verbose(message string, a ...interface{})
- func (p *Producer) Warn(message string, a ...interface{})
- type Publisher
- type Subscriber
- type Tag
- type TagSet
- type Tags
- type TagsFunc
- type TimeFormatter
- type Writer
Constants ¶
This section is empty.
Variables ¶
var DefaultLogger = NewStdoutLogger(0)
DefaultLogger is an all-levels, TTY-sensing logger that writes to STDOUT.
var DefaultTimeFormatter = &TimeFormatter{ TimeFormat: "02 Jan 2006 15:04:05.000 MST", Location: time.UTC, }
DefaultTimeFormatter is the default TimeFormatter used by various packaged default formatters and Logger recipes.
var Levels = map[Level]LevelInfo{ Verbose: {"verbose", "VRBSE", ansi.Blue}, Trace: {"trace", "TRACE", ansi.Green}, Debug: {"debug", "DEBUG", ansi.Cyan}, Info: {"info", "INFO ", 0}, Warn: {"warn", "WARN ", ansi.Yellow}, Error: {"error", "ERROR", ansi.Red}, Fatal: {"fatal", "FATAL", ansi.Magenta}, Silent: {"silent", "SILNT", ansi.White}, }
Levels contains default properties of the set levels, which can be used by formatters.
var NullLogger = NewLogger(0, new(NullSubscriber)) // 0 level avoids making a LevelFilter
NullLogger is a logger that discards events. You can use it as a placeholder in apps that have logging disabled and want calls to Info, Warn, Error etc to no-op.
var NullProducer = &Producer{Subscriber: new(NullSubscriber)}
NullProducer is a producer that discards all events.
var NullWriter = NewWriterWithFormat(io.Discard, new(NullFormatter))
NullWriter is a writer that does not write events to anywhere.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct { Subscriber // contains filtered or unexported fields }
Buffer uses a goroutine to consume events from a buffered channel, ensuring publication doesn't block on writing (at least until the buffer is full). A zero Buffer will not work. Use NewBuffer instead.
func NewBuffer ¶
func NewBuffer(size int, subscriber Subscriber) (b *Buffer)
NewBuffer wraps the given Subscriber in a buffer with the given size.
type Err ¶
type Err string
Err represents most of the errors generated by the package.
const ( // InvalidLevelName occurs when LevelByName is called with an invalid name. InvalidLevelName Err = "invalid level name" )
type Formatter ¶
Formatter can format a given Event for writing to a log device. Formatters should normally include a trailing newline.
var DefaultColoredTextFormatter Formatter = FormatterFunc(func(event *Event) []byte { return append(DefaultTimeFormatter.Format(event), []byte(fmt.Sprintf( " %s %s\n", event.Level.Color().Bold().Wrap(event.Level.Abbreviation()), event.Message, ))...) })
DefaultColoredTextFormatter is the same as DefaultPlainTextFormatter, but adds color to the Level component to help messages of different severities stand out.
var DefaultPlainTextFormatter Formatter = FormatterFunc(func(event *Event) []byte { return append(DefaultTimeFormatter.Format(event), []byte(fmt.Sprintf( " %s %s\n", event.Level.Abbreviation(), event.Message, ))...) })
DefaultPlainTextFormatter formats events as the time (using DefaultTimeFormatter), the Level abbreviation, and the message. Tags are not included.
type FormatterFunc ¶
FormatterFunc creates a Formatter using the given function.
func (FormatterFunc) Format ¶
func (f FormatterFunc) Format(event *Event) (formatted []byte)
type JsonFormatter ¶
type JsonFormatter struct {
Tags TagSet
}
JsonFormatter is a JSON log formatter compatible with Logstash.
func NewJsonFormatter ¶
func NewJsonFormatter(tags TagSet) *JsonFormatter
NewJsonFormatter creates a new JsonFormatter with the given TagSet.
func (*JsonFormatter) Format ¶
func (j *JsonFormatter) Format(event *Event) []byte
type LazyMessage ¶
type LazyMessage struct { Message string Args []interface{} }
LazyMessage is used by Producer to delay formatting of messages, in case an event is to be discarded by a LevelFilter.
func (*LazyMessage) String ¶
func (m *LazyMessage) String() string
type Level ¶
type Level uint8
Level represents log message severity. Levels are preset as the seven constants Verbose, Trace, Debug, Info, Warn, Error, and Fatal.
func LevelByName ¶
LevelByName returns a Level if one case-insensitively matches the given name. If none match, InvalidLevelName is returned.
func (Level) Abbreviation ¶
Abbreviation returns a five-character uppercase string representing the receiving Level.
func (Level) Color ¶
func (l Level) Color() ansi.EscapeSequence
Color returns the Color of the receiving Level, based on the Levels map.
type LevelFilter ¶
type LevelFilter struct { Subscriber LevelMask Level }
LevelFilter wraps a Subscriber, swallowing events that do not match LevelMask.
func NewLevelFilter ¶
func NewLevelFilter(level Level, subscriber Subscriber) *LevelFilter
NewLevelFilter creates a new LevelFilter that will swallow events with severity lower than the given Level.
func (*LevelFilter) Log ¶
func (l *LevelFilter) Log(event *Event)
type LevelInfo ¶
type LevelInfo struct { Name string Abbreviation string Color ansi.EscapeSequence }
LevelInfo represents the default properties of a Level.
type Logger ¶
Logger is a full-featured logging utility.
func NewColoredTextLogger ¶
NewColoredTextLogger creates a new Logger with the given Level for the given io.Writer, using DefaultColoredTextFormatter as its formatter.
func NewFormattedLogger ¶
NewFormattedLogger creates a new Logger with the given Level for the given io.Writer, using the given Formatter as its formatter.
func NewLogger ¶
func NewLogger(level Level, subscriber Subscriber) (logger *Logger)
NewLogger creates a new Logger with the given Level, which publishes synchronously to the given Subscriber.
func NewPlainTextLogger ¶
NewPlainTextLogger creates a new Logger with the given Level for the given io.Writer, using DefaultPlainTextFormatter as its formatter.
func NewStderrLogger ¶
NewStderrLogger creates a new Logger with the given Level for STDERR, using either DefaultPlainTextFormatter or DefaultColoredTextFormatter as its formatter, depending on whether it senses a TTY.
func NewStdoutLogger ¶
NewStdoutLogger creates a new Logger with the given Level for STDOUT, using either DefaultPlainTextFormatter or DefaultColoredTextFormatter as its formatter, depending on whether it senses a TTY.
func NewTextLogger ¶
NewTextLogger creates a new Logger with the given Level for the given os.File, using either DefaultPlainTextFormatter or DefaultColoredTextFormatter as its formatter, depending on whether it senses a TTY.
type NullFormatter ¶
type NullFormatter struct{}
NullFormatter is an implementation of Formatter that formats every message as an empty byte slice.
func (*NullFormatter) Format ¶
func (n *NullFormatter) Format(*Event) []byte
type NullSubscriber ¶
type NullSubscriber struct{}
NullSubscriber is an implementation of Subscriber that discards messages. It is used chiefly by NullLogger.
func (*NullSubscriber) Log ¶
func (n *NullSubscriber) Log(*Event)
type Producer ¶
type Producer struct { Subscriber Clock func() time.Time // Function used to timestamp events created by the receiver }
Producer provides the per-level functions of Logger, but can also be wrapped around any Subscriber when composing custom loggers.
type Publisher ¶
type Publisher []Subscriber
Publisher relays each Event it receives to as many Subscriber implementations as are in its slice.
func NewPublisher ¶
func NewPublisher(subscribers ...Subscriber) Publisher
NewPublisher creates a new Publisher that relays events to the given Subscriber slice.
func (*Publisher) Add ¶
func (p *Publisher) Add(subscriber Subscriber)
Add appends an additional Subscriber to the receiving Publisher.
type Subscriber ¶
type Subscriber interface {
Log(event *Event)
}
Subscriber represents anything that can receive Log events.
type Tags ¶
type Tags []*Tag
Tags can be attached to log events to provide additional information, particularly for JSON and other structured log formatters.
func (Tags) Join ¶
Join creates a new Tags by appending the given tags to the receiver. Tags towards the end of the slice take precedence, meaning the joined tags will override those in the receiver with the same name.
func (Tags) Map ¶
Map returns a map of tag keys to values. Overridden tags are excluded from the returned map.
func (Tags) MarshalJSON ¶
MarshalJSON causes the receiver to be marshaled to JSON as an object with ordered keys.
func (Tags) QueryEncode ¶
QueryEncode formats tags as a percent-encoded URL query string. Tags are positioned in the string in the order they first appear, with later values taking precedence.
type TagsFunc ¶
type TagsFunc func() Tags
TagsFunc creates a TagSet implementation from the given function.
type TimeFormatter ¶
TimeFormatter is a Formatter implementation that formats the Time of an Event using the time package's formatter.
func NewTimeFormatter ¶
func NewTimeFormatter(format string, location *time.Location) *TimeFormatter
NewTimeFormatter creates a new TimeFormatter with the given format and time.Location.
func (*TimeFormatter) Format ¶
func (t *TimeFormatter) Format(event *Event) []byte
type Writer ¶
type Writer struct { Formatter Writer io.Writer // OnWriteFail is called when the Writer is unable to write an Event to the underlying // io.Writer. If OnWriteFail is nil, write failures will cause a Writer to panic. OnWriteFail func(error) // contains filtered or unexported fields }
Writer is a Subscriber implementation that writes events to an io.Writer using a Formatter to convert events into byte slices.
func NewWriter ¶
NewWriter makes a new Writer for the given io.Writer, using MessageOnlyFormatter as its formatter.
func NewWriterWithFormat ¶
NewWriterWithFormat makes a new Writer for the given io.Writer, using the given Formatter.