Documentation ¶
Overview ¶
Package logging implements a logging infrastructure for Go. It supports different logging backends like syslog, file and memory. Multiple backends can be utilized with different log levels per backend and logger.
Index ¶
- Variables
- func AddBackend(b Backend)
- func MustStringFormatter(format string) *stringFormatter
- func NewStringFormatter(format string) (*stringFormatter, error)
- func Redact(s string) string
- func Reset()
- func SetFormatter(f Formatter)
- func SetLevel(level Level, module string)
- type Annotater
- type Annotation
- type Annotator
- type Backend
- type ChannelMemoryBackend
- type Formatter
- type Level
- type LevelLogger
- type Leveled
- type LeveledBackend
- type LogBackend
- type Logger
- func (l *Logger) Backend() LeveledBackend
- func (l *Logger) Critical(format string, args ...interface{})
- func (l *Logger) Debug(format string, args ...interface{})
- func (l *Logger) Error(format string, args ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Fatalln(args ...interface{})
- func (l *Logger) Flags() int
- func (l *Logger) Info(format string, args ...interface{})
- func (l *Logger) IsEnabledFor(level Level) bool
- func (l *Logger) Log(lvl Level, format string, args ...interface{})
- func (l *Logger) Notice(format string, args ...interface{})
- func (l *Logger) Output(calldepth int, s string) error
- func (l *Logger) Outputf(adjdepth int, fmt string, args ...interface{})
- func (l *Logger) Panic(args ...interface{})
- func (l *Logger) Panicf(format string, args ...interface{})
- func (l *Logger) Panicln(args ...interface{})
- func (l *Logger) Prefix() string
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Println(v ...interface{})
- func (l *Logger) Re(a Annotater) *Logger
- func (l *Logger) SetBackend(b LeveledBackend)
- func (l *Logger) SetFlags(flag int)
- func (l *Logger) SetPrefix(p string)
- func (l *Logger) Warning(format string, args ...interface{})
- type MemoryBackend
- type Record
- type Redactor
- type SyslogBackend
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidLogLevel = errors.New("logger: invalid log level")
Functions ¶
func MustStringFormatter ¶
func MustStringFormatter(format string) *stringFormatter
MustStringFormatter is equivalent to NewStringFormatter with a call to panic on error.
func NewStringFormatter ¶
func SetFormatter ¶
func SetFormatter(f Formatter)
SetFormatter sets the default formatter for all new backends. A backend will fetch this value once it is needed to format a record. Note that backends will cache the formatter after the first point. For now, make sure to set the formatter before logging.
func SetLevel ¶
SetLevel sets the logging level for the specified module. The module corresponds to the string specified in GetLogger. Glob characters suitable for use in `path.Match` may be used. Exact (non-glob) settings take precedence over pattern matches, and in the case of overlapping patterns, settings applied later override earlier settings
Types ¶
type Annotation ¶
type Annotation struct { Key string Value interface{} }
Annotation is a key/value entry; the Annotator backend can add annotations
type Annotator ¶
type Annotator struct { Extras map[string]Annotation DynamicExtras func() []Annotation // contains filtered or unexported fields }
An Annotator is a kind of Backend that adds some annotations to a message in flight
func NewAnnotator ¶
type Backend ¶
Backend is the interface which a log backend need to implement to be able to be used as a logging backend.
type ChannelMemoryBackend ¶
type ChannelMemoryBackend struct {
// contains filtered or unexported fields
}
ChannelMemoryBackend is very similar to the MemoryBackend, except that it internally utilizes a channel.
func NewChannelMemoryBackend ¶
func NewChannelMemoryBackend(size int) *ChannelMemoryBackend
NewChannelMemoryBackend creates a simple in-memory logging backend which utilizes a go channel for communication.
Start will automatically be called by this function.
func (*ChannelMemoryBackend) Flush ¶
func (b *ChannelMemoryBackend) Flush()
Flush waits until all records in the buffered channel have been processed.
func (*ChannelMemoryBackend) Head ¶
func (b *ChannelMemoryBackend) Head() *node
Head returns the oldest record node kept in memory. It can be used to iterate over records, one by one, up to the last record.
Note: new records can get added while iterating. Hence the number of records iterated over might be larger than the maximum size.
func (*ChannelMemoryBackend) Log ¶
func (b *ChannelMemoryBackend) Log(level Level, calldepth int, rec *Record) error
Log implements the Log method required by Backend.
func (*ChannelMemoryBackend) Start ¶
func (b *ChannelMemoryBackend) Start()
Start launches the internal goroutine which starts processing data from the input channel.
func (*ChannelMemoryBackend) Stop ¶
func (b *ChannelMemoryBackend) Stop()
Stop signals the internal goroutine to exit and waits until it have.
type Formatter ¶
Formatter is the required interface for a custom log record formatter.
var ( // DefaultFormatter is the default formatter used and is only the message. DefaultFormatter Formatter = MustStringFormatter("%{message}") // Glog format GlogFormatter Formatter = MustStringFormatter("%{level:.1s}%{time:0102 15:04:05.999999} %{pid} %{shortfile}] %{message}") )
type Level ¶
type Level int
Level defines all available log levels for log messages.
type LevelLogger ¶
type LevelLogger interface { // a logging.LevelLogger is also a standardlog.Logger Fatal(v ...interface{}) Fatalf(format string, v ...interface{}) Fatalln(v ...interface{}) Panic(v ...interface{}) Panicf(format string, v ...interface{}) Panicln(v ...interface{}) Print(v ...interface{}) Printf(format string, v ...interface{}) Println(v ...interface{}) // Go1 details of log.Logger that don't seem appropriate to // the abstract notion of logging Output(calldepth int, s string) error Outputf(adjcalldepth int, format string, args ...interface{}) Prefix() string Flags() int SetFlags(flag int) SetPrefix(prefix string) Debug(format string, args ...interface{}) Info(format string, args ...interface{}) Notice(format string, args ...interface{}) Warning(format string, args ...interface{}) Error(format string, args ...interface{}) Critical(format string, args ...interface{}) }
type LeveledBackend ¶
LeveledBackend is a log backend with additional knobs for setting levels on individual modules to different levels.
func AddModuleLevel ¶
func AddModuleLevel(backend Backend) LeveledBackend
AddModuleLevel wraps a log backend with knobs to have different log levels for different modules.
func MultiLogger ¶
func MultiLogger(backends ...Backend) LeveledBackend
MultiLogger creates a logger which contain multiple loggers.
func SetBackend ¶
func SetBackend(backends ...Backend) LeveledBackend
Set backend replaces the backend currently set with the given new logging backend.
type LogBackend ¶
LogBackend utilizes the standard log module.
func NewLogBackend ¶
func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend
NewLogBackend creates a new LogBackend.
type Logger ¶
type Logger struct { Module string // the Level at which we Output records (defaults to INFO) OutputLevel Level // contains filtered or unexported fields }
func GetLogger ¶
TODO call NewLogger and remove MustGetLogger? GetLogger creates and returns a Logger object based on the module name.
func MustGetLogger ¶
MustGetLogger is like GetLogger but panics if the logger can't be created. It simplifies safe initialization of a global logger for eg. a package.
func (*Logger) Backend ¶
func (l *Logger) Backend() LeveledBackend
func (*Logger) Fatal ¶
func (l *Logger) Fatal(args ...interface{})
Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1).
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(args ...interface{})
Fatalln is equivalent to l.Critical(fmt.Sprintln()) followed by a call to os.Exit(1).
func (*Logger) IsEnabledFor ¶
IsEnabledFor returns true if the logger is enabled for the given level.
func (*Logger) Panic ¶
func (l *Logger) Panic(args ...interface{})
Panic is equivalent to l.Critical(fmt.Sprint()) followed by a call to panic().
func (*Logger) Panicln ¶
func (l *Logger) Panicln(args ...interface{})
Panicln is equivalent to l.Critical(fmt.Sprintln()) followed by a call to panic().
func (*Logger) Re ¶
Re returns a new logger that annotates log messages with the appropriate context (as defined by the Annotater)
func (*Logger) SetBackend ¶
func (l *Logger) SetBackend(b LeveledBackend)
type MemoryBackend ¶
type MemoryBackend struct {
// contains filtered or unexported fields
}
MemoryBackend is a simple memory based logging backend that will not produce any output but merly keep records, up to the given size, in memory.
func InitForTesting ¶
func InitForTesting(level Level) *MemoryBackend
InitForTesting is a convenient method when using logging in a test. Once called, the time will be frozen to January 1, 1970 UTC.
func NewMemoryBackend ¶
func NewMemoryBackend(size int) *MemoryBackend
NewMemoryBackend creates a simple in-memory logging backend.
func (*MemoryBackend) Head ¶
func (b *MemoryBackend) Head() *node
Head returns the oldest record node kept in memory. It can be used to iterate over records, one by one, up to the last record.
Note: new records can get added while iterating. Hence the number of records iterated over might be larger than the maximum size.
type Record ¶
type Record struct { Id uint64 Time time.Time Module string Level Level Annotations []Annotation // contains filtered or unexported fields }
Record represents a log record and contains the timestamp when the record was created, an increasing id, filename and line and finally the actual formatted log line.
type Redactor ¶
type Redactor interface {
Redacted() interface{}
}
Redactor is an interface for types that may contain sensitive information (like passwords), which shouldn't be printed to the log. The idea was found in relog as part of the vitness project.
type SyslogBackend ¶
SyslogBackend is a simple logger to syslog backend. It automatically maps the internal log levels to appropriate syslog log levels.
func NewSyslogBackend ¶
func NewSyslogBackend(prefix string) (b *SyslogBackend, err error)
NewSyslogBackend connects to the syslog daemon using UNIX sockets with the given prefix. If prefix is not given, the prefix will be derived from the launched command.
func NewSyslogBackendPriority ¶
func NewSyslogBackendPriority(prefix string, priority syslog.Priority) (b *SyslogBackend, err error)
NewSyslogBackendPriority is the same as NewSyslogBackend, but with custom syslog priority, like syslog.LOG_LOCAL3|syslog.LOG_DEBUG etc.