Documentation ¶
Overview ¶
Module level logging for Go
This package provides an alternative to the standard library log package.
The actual logging functions never return errors. If you are logging something, you really don't want to be worried about the logging having trouble.
Modules have names that are defined by dotted strings.
e.g. "first.second.third"
There is a root module that has the name "". Each module (except the root module) has a parent, identified by the part of the name without the last dotted value.
e.g. the parent of "first.second.third" is "first.second" the parent of "first.second" is "first" the parent of "first" is "" (the root module)
Each module can specify its own severity level. Logging calls that are of a lower severity than the module's effective severity level are not written out.
Loggers are created using the GetLogger function.
e.g. logger := loggo.GetLogger("foo.bar")
By default there is one writer registered, which will write to Stderr, and the root module, which will only emit warnings and above. If you want to continue using the default logger, but have it emit all logging levels you need to do the following.
writer, _, err := loggo.RemoveWriter("default") // err is non-nil if and only if the name isn't found. loggo.RegisterWriter("default", writer, loggo.TRACE)
Index ¶
- func ConfigureLoggers(specification string) error
- func LoggerInfo() string
- func ParseConfigurationString(specification string) (map[string]Level, error)
- func RegisterWriter(name string, writer Writer, minLevel Level) error
- func RemoveWriter(name string) (Writer, Level, error)
- func ResetLoggers()
- func ResetWriters()
- func WillWrite(level Level) bool
- type DefaultFormatter
- type Formatter
- type Level
- type Logger
- func (logger Logger) Criticalf(message string, args ...interface{})
- func (logger Logger) Debugf(message string, args ...interface{})
- func (logger Logger) EffectiveLogLevel() Level
- func (logger Logger) Errorf(message string, args ...interface{})
- func (logger Logger) Infof(message string, args ...interface{})
- func (logger Logger) IsDebugEnabled() bool
- func (logger Logger) IsErrorEnabled() bool
- func (logger Logger) IsInfoEnabled() bool
- func (logger Logger) IsLevelEnabled(level Level) bool
- func (logger Logger) IsTraceEnabled() bool
- func (logger Logger) IsWarningEnabled() bool
- func (logger Logger) LogLevel() Level
- func (logger Logger) Logf(level Level, message string, args ...interface{})
- func (logger Logger) Name() string
- func (logger Logger) SetLogLevel(level Level)
- func (logger Logger) Tracef(message string, args ...interface{})
- func (logger Logger) Warningf(message string, args ...interface{})
- type TestLogValues
- type TestWriter
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigureLoggers ¶
ConfigureLoggers configures loggers according to the given string specification, which specifies a set of modules and their associated logging levels. Loggers are colon- or semicolon-separated; each module is specified as <modulename>=<level>. White space outside of module names and levels is ignored. The root module is specified with the name "<root>".
An example specification:
`<root>=ERROR; foo.bar=WARNING`
func LoggerInfo ¶
func LoggerInfo() string
LoggerInfo returns information about the configured loggers and their logging levels. The information is returned in the format expected by ConfigureModules. Loggers with UNSPECIFIED level will not be included.
func ParseConfigurationString ¶
ParseConfigurationString parses a logger configuration string into a map of logger names and their associated log level. This method is provided to allow other programs to pre-validate a configuration string rather than just calling ConfigureLoggers.
Loggers are colon- or semicolon-separated; each module is specified as <modulename>=<level>. White space outside of module names and levels is ignored. The root module is specified with the name "<root>".
An example specification:
`<root>=ERROR; foo.bar=WARNING`
func RegisterWriter ¶
RegisterWriter adds the writer to the list of writers that get notified when logging. When registering, the caller specifies the minimum logging level that will be written, and a name for the writer. If there is already a registered writer with that name, an error is returned.
func RemoveWriter ¶
RemoveWriter removes the Writer identified by 'name' and returns it. If the Writer is not found, an error is returned.
func ResetLoggers ¶
func ResetLoggers()
ResetLogging iterates through the known modules and sets the levels of all to UNSPECIFIED, except for <root> which is set to WARNING.
func ResetWriters ¶
func ResetWriters()
ResetWriters puts the list of writers back into the initial state.
Types ¶
type DefaultFormatter ¶
type DefaultFormatter struct{}
DefaultFormatter provides a simple concatenation of all the components.
func (*DefaultFormatter) Format ¶
func (*DefaultFormatter) Format(level Level, module, filename string, line int, timestamp time.Time, message string) string
Format returns the parameters separated by spaces except for filename and line which are separated by a colon. The timestamp is shown to second resolution in UTC.
type Formatter ¶
type Formatter interface {
Format(level Level, module, filename string, line int, timestamp time.Time, message string) string
}
Formatter defines the single method Format, which takes the logging information, and converts it to a string.
type Level ¶
type Level uint32
Level holds a severity level.
The severity levels. Higher values are more considered more important.
func ParseLevel ¶
ParseLevel converts a string representation of a logging level to a Level. It returns the level and whether it was valid or not.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents a logging module. It has an associated logging level which can be changed; messages of lesser severity will be dropped. Loggers have a hierarchical relationship - see the package documentation.
The zero Logger value is usable - any messages logged to it will be sent to the root Logger.
func GetLogger ¶
GetLogger returns a Logger for the given module name, creating it and its parents if necessary.
func (Logger) EffectiveLogLevel ¶
EffectiveLogLevel returns the effective log level of the receiver - that is, messages with a lesser severity level will be discarded.
If the log level of the receiver is unspecified, it will be taken from the effective log level of its parent.
func (Logger) IsDebugEnabled ¶
IsDebugEnabled returns whether debugging is enabled at debug level.
func (Logger) IsErrorEnabled ¶
IsErrorEnabled returns whether debugging is enabled at error level.
func (Logger) IsInfoEnabled ¶
IsInfoEnabled returns whether debugging is enabled at info level.
func (Logger) IsLevelEnabled ¶
IsLevelEnabled returns whether debugging is enabled for the given log level.
func (Logger) IsTraceEnabled ¶
IsTraceEnabled returns whether debugging is enabled at trace level.
func (Logger) IsWarningEnabled ¶
IsWarningEnabled returns whether debugging is enabled at warning level.
func (Logger) Logf ¶
Logf logs a printf-formatted message at the given level. A message will be discarded if level is less than the the effective log level of the logger. Note that the writers may also filter out messages that are less than their registered minimum severity level.
func (Logger) SetLogLevel ¶
SetLogLevel sets the severity level of the given logger. The root logger cannot be set to UNSPECIFIED level. See EffectiveLogLevel for how this affects the actual messages logged.
type TestLogValues ¶
type TestLogValues struct { Level Level Module string Filename string Line int Timestamp time.Time Message string }
TestLogValues represents a single logging call.
type TestWriter ¶
type TestWriter struct {
Log []TestLogValues
}
TestWriter is a useful Writer for testing purposes. Each component of the logging message is stored in the Log array.
type Writer ¶
type Writer interface { // Write writes a message to the Writer with the given // level and module name. The filename and line hold // the file name and line number of the code that is // generating the log message; the time stamp holds // the time the log message was generated, and // message holds the log message itself. Write(level Level, name, filename string, line int, timestamp time.Time, message string) }
Writer is implemented by any recipient of log messages.
func NewSimpleWriter ¶
NewSimpleWriter returns a new writer that writes log messages to the given io.Writer formatting the messages with the given formatter.
func ReplaceDefaultWriter ¶
ReplaceDefaultWriter is a convenience method that does the equivalent of RemoveWriter and then RegisterWriter with the name "default". The previous default writer, if any is returned.