Documentation
¶
Overview ¶
Package logging provides a multi-backend leveled logging facility.
Backends ¶
package logging defines the following builtin backends:
- NoopBackend, which discards all messages
- FileBackend, which redirects logs to an io.Writer object. It has constructors for files, stdout and stderr
- SyslogBackend, only available in unix-like systems, writes log entryies to a syslog daemon.
A backend can safely be used by multiple loggers.
It is the caller's responsibility to call Close on backends when they are not used anymore to free their resources.
Index ¶
- type Backend
- type FileBackend
- type Formatter
- type Level
- type Logger
- func (l *Logger) AddBackend(b Backend)
- func (l *Logger) Alert(text string)
- func (l *Logger) Alertf(text string, args ...interface{})
- func (l *Logger) AsStdLog(level Level) *log.Logger
- func (l *Logger) Critical(text string)
- func (l *Logger) Criticalf(text string, args ...interface{})
- func (l *Logger) Debug(text string)
- func (l *Logger) Debugf(text string, args ...interface{})
- func (l *Logger) Error(text string)
- func (l *Logger) Errorf(text string, args ...interface{})
- func (l *Logger) Fatal(text string)
- func (l *Logger) Fatalf(text string, args ...interface{})
- func (l *Logger) Info(text string)
- func (l *Logger) Infof(text string, args ...interface{})
- func (l *Logger) Log(level Level, m string)
- func (l *Logger) Notice(text string)
- func (l *Logger) Noticef(text string, args ...interface{})
- func (l *Logger) SetBackend(b ...Backend)
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) Trace(text string)
- func (l *Logger) Tracef(text string, args ...interface{})
- func (l *Logger) Warning(text string)
- func (l *Logger) Warningf(text string, args ...interface{})
- type NoopBackend
- type Record
- type SyslogBackend
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface { Write(*Record) error SetFormatter(Formatter) SetLevel(Level) Level() Level Reopen() error Close() error }
Backend is the interface that specifies the methods that a backend must implement.
type FileBackend ¶
type FileBackend struct {
// contains filtered or unexported fields
}
FileBackend is a backend that writes to a file.
func NewFileBackend ¶
func NewFileBackend(filename string) (*FileBackend, error)
NewFileBackend creates a new backend to write the logs in a given file.
func NewIoBackend ¶
func NewIoBackend(buf io.Writer) *FileBackend
NewIoBackend creates a new backend to write the logs in a given io.Writer.
func NewStderrBackend ¶
func NewStderrBackend() *FileBackend
NewStderrBackend creates a new backend to write the logs on the error output.
func NewStdoutBackend ¶
func NewStdoutBackend() *FileBackend
NewStdoutBackend creates a new backend to write the logs on the standard output.
func (*FileBackend) Close ¶ added in v0.3.0
func (b *FileBackend) Close() error
Close closes the underlying file used by the backend.
func (*FileBackend) Level ¶
func (b *FileBackend) Level() Level
Level returns the log level set for this backend.
func (*FileBackend) Reopen ¶
func (b *FileBackend) Reopen() error
Reopen closes and reopens the file it writes to. It should be used after log rotation.
func (*FileBackend) SetFormatter ¶
func (b *FileBackend) SetFormatter(f Formatter)
SetFormatter defines the formatter for this backend.
func (*FileBackend) SetLevel ¶
func (b *FileBackend) SetLevel(l Level)
SetLevel changes the log level of the backend.
func (FileBackend) Write ¶
func (b FileBackend) Write(r *Record) error
type Formatter ¶
Formatter is the types of the functions that can be used to format a log entry. They take a pointer to a record and return a formatted string.
type Level ¶
type Level byte
Level is the type of log levels.
func LevelByName ¶
LevelByName creates a log level given its name. It returns an error if the name does not match any level.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a facility that writes logs to one or more backands (files, stdout/stderr, syslog, etc.) which can be configured independently
Loggers are concurrent-safe.
func GetLogger ¶
GetLogger returns a logger given its name. if the logger does not exist, it initializes one with the defaults (it logs to stdout with level INFO).
func (*Logger) AddBackend ¶
AddBackend add a new Backend to the logger. All set backends are kept.
func (*Logger) Alertf ¶ added in v0.4.0
Alertf formats the message with given args and logs the result with the. Alert level.
func (*Logger) AsStdLog ¶
AsStdLog encapsulate the logger in an instance of lof.Logger from the standard library and returns it.
It is there for interoperability reasons.
func (*Logger) Criticalf ¶
Criticalf formats the message with given args and logs the result with the. Critical level.
func (*Logger) Debugf ¶
Debugf formats the message with given args and logs the result with the Debug level.
func (*Logger) Errorf ¶
Errorf formats the message with given args and logs the result with the Error level.
func (*Logger) Fatalf ¶
Fatalf formats the message with given args and logs the result with the Fatal level.
func (*Logger) Infof ¶
Infof formats the message with given args and logs the result with the Info level.
func (*Logger) Log ¶
Log sends a record containing the message `m` to the registered backends whose level is at least `level`.
func (*Logger) Noticef ¶ added in v0.4.0
Noticef formats the message with given args and logs the result with the Notice level.
func (*Logger) SetBackend ¶
SetBackend sets the backend list to the logger. Any existing backend will be lost.
func (*Logger) SetLevel ¶
SetLevel changes the log level of all regisered backends to the given level.
func (*Logger) Tracef ¶ added in v0.4.0
Tracef formats the message with given args and logs the result with the Trace level.
type NoopBackend ¶
type NoopBackend struct{}
NoopBackend does nothing and discards all log entries without writing them anywhere.
func NewNoopBackend ¶
func NewNoopBackend() (*NoopBackend, error)
NewNoopBackend creates a noop backend.
func (*NoopBackend) SetFormatter ¶
func (*NoopBackend) SetFormatter(_ Formatter)
SetFormatter is a noop.
type Record ¶
Record contains the data to be logged. It is passed to a formatter to generate the logged message.
type SyslogBackend ¶
type SyslogBackend struct {
// contains filtered or unexported fields
}
SyslogBackend writes the logs to a syslog system.
func NewSyslogBackend ¶
func NewSyslogBackend(facilityName, tag string) (*SyslogBackend, error)
NewSyslogBackend initializes a new connection to a syslog server with the given facility. tag can contain an identifier for the log stream. It defaults to os.Arg[0].
func (*SyslogBackend) Close ¶ added in v0.3.0
func (sb *SyslogBackend) Close() error
Close closes the connection to the syslog daemon.
func (*SyslogBackend) Level ¶
func (sb *SyslogBackend) Level() Level
Level returns the log level set for this backend.
func (*SyslogBackend) SetFormatter ¶
func (sb *SyslogBackend) SetFormatter(f Formatter)
SetFormatter defines the formatter for this backend.
func (*SyslogBackend) SetLevel ¶
func (sb *SyslogBackend) SetLevel(level Level)
SetLevel changes the log level of the backend.
func (*SyslogBackend) Write ¶
func (sb *SyslogBackend) Write(r *Record) error
Write sends an entry to the syslog server.