Documentation ¶
Index ¶
- Variables
- func WrapWriter(w io.Writer) io.Writer
- type BasicLogger
- type ExtendedLogger
- type FormatterFunc
- type Level
- type Log
- func (l *Log) AddFormatOverride(level Level, f FormatterFunc)
- func (l *Log) Chain(logger *Log)
- func (l *Log) ClearBackend(level Level) *Log
- func (l *Log) Debug(v ...interface{})
- func (l *Log) Debugf(format string, v ...interface{})
- func (l *Log) DestroyLogger(name string)
- func (l *Log) Error(v ...interface{})
- func (l *Log) Errorf(format string, v ...interface{})
- func (l *Log) Fatal(v ...interface{})
- func (l *Log) Fatalf(format string, v ...interface{})
- func (l *Log) Fatalln(v ...interface{})
- func (l *Log) FullPath() string
- func (l *Log) GetLogger(name string) (*Log, bool)
- func (l *Log) Info(v ...interface{})
- func (l *Log) Infof(format string, v ...interface{})
- func (l *Log) Information(v ...interface{})
- func (l *Log) Informationf(format string, v ...interface{})
- func (l *Log) Inherit(name string) *Log
- func (l *Log) Iterate(fn func(*Log))
- func (l *Log) Level() Level
- func (l *Log) Name() string
- func (l *Log) Notice(v ...interface{})
- func (l *Log) Noticef(format string, v ...interface{})
- func (l *Log) Panic(v ...interface{})
- func (l *Log) Panicf(format string, v ...interface{})
- func (l *Log) Panicln(v ...interface{})
- func (l *Log) Print(v ...interface{})
- func (l *Log) Printf(format string, v ...interface{})
- func (l *Log) Println(v ...interface{})
- func (l *Log) SetBackend(backend io.Writer) *Log
- func (l *Log) SetBackendForLevel(level Level, backend io.Writer) *Log
- func (l *Log) SetFormatter(formatter FormatterFunc) *Log
- func (l *Log) SetLevel(level Level) *Log
- func (l *Log) SetOption(option string, value interface{})
- func (l *Log) SetRawBackend(backend io.Writer) *Log
- func (l *Log) SetRawBackendForLevel(level Level, backend io.Writer) *Log
- func (l *Log) SetTimezone(tz *time.Location) *Log
- func (l *Log) Warn(v ...interface{})
- func (l *Log) Warnf(format string, v ...interface{})
- func (l *Log) Warning(v ...interface{})
- func (l *Log) Warningf(format string, v ...interface{})
- func (l *Log) Writer() io.Writer
- type Logger
- type Message
- type StdLogger
- type SyncWriter
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ( // Black escape sequence (normal font type). Black = []byte{0x33, '[', '3', '0', 'm'} // Red escape sequence (normal font type). Red = []byte{0x33, '[', '3', '1', 'm'} // Green escape sequence (normal font type). Green = []byte{0x33, '[', '3', '2', 'm'} // Yellow escape sequence (normal font type). Yellow = []byte{0x33, '[', '3', '3', 'm'} // Blue escape sequence (normal font type). Blue = []byte{0x33, '[', '3', '4', 'm'} // Magenta escape sequence (normal font type). Magenta = []byte{0x33, '[', '3', '5', 'm'} // Cyan escape sequence (normal font type). Cyan = []byte{0x33, '[', '3', '6', 'm'} // White escape sequence (normal font type). White = []byte{0x33, '[', '3', '7', 'm'} // Reset escape sequence. Reset = []byte{0x33, '[', '0', 'm'} )
var Discard = ioutil.Discard
var Exit exitFunc
Exit provides a method for overriding Fatal* functions' default behavior. By default, this is defined in init.go and initialized here for replacement by user code, if necessary.
var Levels = []string{
"INHERIT",
"FATAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
}
Levels defines the supported RFC 5424 logging levels in this package.
var Panic panicFunc
Panic provides a method for overriding Panic* functions' default behavior. By default, this is defined in init.go and initialized here for replacement by user code, if necessary.
var Stdout = WrapWriter(os.Stdout)
Stdout is the standard output writer, used globally by all writers by default to reduce risk of interleaving.
Functions ¶
Types ¶
type BasicLogger ¶
type ExtendedLogger ¶
type ExtendedLogger interface { Error(...interface{}) Errorf(string, ...interface{}) Warn(...interface{}) Warning(...interface{}) Warnf(string, ...interface{}) Warningf(string, ...interface{}) Notice(...interface{}) Noticef(string, ...interface{}) Info(...interface{}) Information(...interface{}) Infof(string, ...interface{}) Informationf(string, ...interface{}) Debug(...interface{}) Debugf(string, ...interface{}) BasicLogger }
type FormatterFunc ¶
FormatterFunc defines the type of a formatter function that can be used to override the use of Go templates. This function must accept the logger instance as its first argument followed by a single formatter (containing the message data) and return a string.
func NewDefaultFormatter ¶
func NewDefaultFormatter(format string) FormatterFunc
NewDefaultFormatter returns the defaultFormatter used by logging for inline variable string replacement.
This implementation is exceedingly basic, simple, and fairly performant. It is approximately 3 times faster than the Golang text template formatter and should be used except in rare circumstances where conditional manipulation of logging entries is required.
func NewFastFormatter ¶ added in v1.1.0
func NewFastFormatter(format string) FormatterFunc
func NewTemplateFormatter ¶
func NewTemplateFormatter(tpl string, funcs map[string]interface{}) FormatterFunc
NewTemplateFormatter returns a formatter backed by Golang's text templates for line-by-line templating support.
templateFormatters support context-sensitive template functions via the `funcs` argument here and can be used to conditionally manipulate logging output.
Be aware that the templateFormatter is approximately 3 times slower than the default formatter.
type Level ¶
type Level int
Level defines a type for use internally for identifying logging levels.
RFC 5424 levels (borrowed from syslog message facilities).
func LevelFromString ¶
LevelFromString returns a Level value (internally an integer) given a matching string. This serves as a simple map between string -> integer values of error levels.
Some levels have aliases. INFO and WARN can be given as "information" or "warning," respectively.
The case of input values is ignored.
type Log ¶
type Log struct { // This mutex covers the backends, log chain, options metadata, and child // loggers. sync.Mutex // Metadata options. Typically this map will be empty and isn't used // internally by the logger. However, it is provided for formatterOverrides // that may need additional metadata for configuration (such as enabling or // disabling colorized output, among other things). Options map[string]interface{} // contains filtered or unexported fields }
Log container.
func MustGetDefaultLogger ¶
func MustGetDefaultLogger() *Log
MustGetDefaultLogger returns the default logger and is a convenience function for `MustGetLogger("__main__")`. This name is chosen to avoid conflicts with other loggers used by client applications.
func MustGetDefaultLoggerWithLevel ¶
MustGetDefaultLoggerWithLevel returns the default logger with the specified logging level. This is a convenience function for calling `MustGetLoggerWithLevel("__main__", level)` and is primarily intended to provide an easy to remember call for configuring a primary (or default) logger for client application. "__main__" is chosen to avoid conflicts with other loggers.
func MustGetLogger ¶
MustGetLogger always returns a logger instance associated with the name `name`, creating it if it doesn't exist.
func MustGetLoggerWithLevel ¶
MustGetLoggerWithLevel always returnr a logger instance associated with the name `name` at the specified logging level, creating it if it doesn't exist. If the logger has already been created, no attempt will be made to reset the level. Useful if you wish to otherwise avoid clobbering a previously-configured logger instance.
func MustInheritLogger ¶
MustInheritLogger always returns a logger instance that is either derived from the named parent logger or is itself a new logger instance. All parent properties are replicated onto the child.
`parent`, in this case, may reference a dot-separated fully qualified path name to the parent logger. If the child logger specified by `name` does not exist it will be created. If the parent inheritance chain doesn't exist, it will also be created.
func NewLogger ¶
NewLogger creates and returns a new logger instance with the name `name`, associating it with the logger container in this module. This will always create a new logger.
If you simply wish to retrieve a logger instance if it exists (creating it if it does not), use MustGetLogger() instead.
func (*Log) AddFormatOverride ¶
func (l *Log) AddFormatOverride(level Level, f FormatterFunc)
AddFormatOverride injects an override function for the level specified.
func (*Log) Chain ¶
Chain another Log instance with the current logger. When a logger is configured to chain to another logger, any logging messages will be repeatedly dispatched down the chain until no more logger instances are found. In this way, it's possible to isolate multiple logger behaviors by error level.
For example, if you wish to create two loggers, one that logs only error messages (and nothing else) and another that logs everything (including error messages), such as might be necessary for applications that must write error messages to a file as well as to STDOUT, you can do the following:
errorsOnly := &bytes.Buffer{} // Error logger. errorLog := MustGetLogger("errors") errorLog.SetBackend(nil) // Clear the main backend. errorLog.SetBackendForLevel(Error, errorLog) // Everything else (remember, the default is to log to os.Stdout)... everythingLog := MustGetLogger("everything") // Chain the loggers starting with the most restrictive. errorLog.Chain(everything) // Now, when we call errorLog.*, it will always log to everythingLog. errorLog.Error("This is an error. It is written to both.") errorLog.Debug("This is a debug notice. It is written only to everythingLog.")
func (*Log) ClearBackend ¶
ClearBackend removes the backend for the specified level.
func (*Log) DestroyLogger ¶
DestroyLogger destroys the child logger identified by `name` alongside any of its children.
func (*Log) Fatal ¶
func (l *Log) Fatal(v ...interface{})
Fatal works similarly to the Go standard library log.Fatal. Like Fatalf, it's treated as as an error condition and exits the application. It additionally logs the message as Fatal.
func (*Log) Fatalf ¶
Fatalf works identically to the Go standard library log.Fatalf, returning an error status and exiting the application. It additionally logs the message as Fatal.
func (*Log) Fatalln ¶
func (l *Log) Fatalln(v ...interface{})
Fatalln works similarly to the Go standard library log.Fatalln. Like Fatalf and Fatal, it's treated as an error condition and exits the application. It additionally logs the message as Fatal.
func (*Log) GetLogger ¶
GetLogger returns the child logger associated with this parent by its name `name`. If no such logger is configured it will be created.
func (*Log) Information ¶
func (l *Log) Information(v ...interface{})
Information is an alias for Info.
func (*Log) Informationf ¶
Informationf is an alias for Infof.
func (*Log) Inherit ¶
Inherit constructs a new logger from the parent instance and returns it. Inherited loggers retain all of their parent properties until they are manually changed and are configured with the special level value Inherit. Inherited loggers are registered globally with the parent's name as a dot-separated prefix (e.g. parent.child) and may be accessed via MustGetLogger or by the parent logger via parent.GetLogger.
Inherit will *always* overwrite an existing logger by the same name.
If a logger inheritance hierarchy declaration (e.g. a dot-separated name) is passed to this method, the last component will be used as the inherited logger name.
func (*Log) Iterate ¶
Iterate over the current logger's children, applying the function fn(). Callback functions receive the current logger as their sole argument.
Useful for modifying an entire logging tree.
func (*Log) Level ¶
GetLevel returns the level for the current logger or, if the level is set to the value Inherit, will return the level associated with the parent logger (if available).
If no suitable level is found, this will return Warning.
func (*Log) Notice ¶
func (l *Log) Notice(v ...interface{})
Notice logs a notice containing pertinent information. This does not indicate an error condition.
func (*Log) Noticef ¶
Noticef logs a notice containing pertinent information with formatting. This does not indicate an error condition.
func (*Log) Panic ¶
func (l *Log) Panic(v ...interface{})
Panic works similarly to the Go standard library log.Panic. Like Panicf, it's treated as as an error condition and panics. It additionally logs the message as fatal.
func (*Log) Panicf ¶
Panicf works identically to the Go standard library log.Panicf, printing the error and returning a panic. As with Fatalf, this logs the message as fatal.
func (*Log) Panicln ¶
func (l *Log) Panicln(v ...interface{})
Panicln works similarly to the Go standard library log.Panicln. Like Panicf and Panic, it's treated as a fatal error and returns a panic.
func (*Log) Print ¶
func (l *Log) Print(v ...interface{})
Print works similarly to the Go standard library log.Print. Like Printf, it's treated as an informational message.
func (*Log) Printf ¶
Printf works similarly to the Go standard library log.Printf with the exception that it's treated as an informational message.
func (*Log) Println ¶
func (l *Log) Println(v ...interface{})
Println works similarly to the Go standard library log.Println. Like Printf and Print, it's treated as an informational message.
func (*Log) SetBackend ¶
SetBackend changes the default backend. This backend will be written to whenever any of the logging facility methods (Error, Critical, etc.) are called.
If you wish to override this backend for a specific logging level, use SetBackendForLevel.
Setting a backend with this function will wrap the given writer in a SyncWriter if it isn't already.
func (*Log) SetBackendForLevel ¶
SetBackendForLevel changes the backend to use for a specific level. Configuring this value will override the default backend for the specified level only.
For instance, creating the following:
buf := &bytes.Buffer{} logger := MustGetLogger("example") logger.SetBackendForLevel(Error, buf)
Will cause:
logger.Error("some error")
to save logging data to `buf`, while
logger.Critical("critical error!") logger.Debug("some debugging information")
will both write to the default backend (os.Stdout).
To clear the backend for the specified level, reverting it back to using the default backend, call ClearBackend(level).
Setting a backend with this function will wrap the given writer in a SyncWriter if it isn't already.
func (*Log) SetFormatter ¶
func (l *Log) SetFormatter(formatter FormatterFunc) *Log
func (*Log) SetLevel ¶
SetLevel changes the logger's level. Error messages that are at this level--or lower--will be logged; everything else will be ignored. Note that in this context "lower" means the literal integer value of the logger and not the relative importance of its data. For example, setting a logging level of Error will log anything at an Error or below (which includes Alert, Emergency, and Critical). Likewise, setting a logging level of Debug will log literally everything.
If you wish to control what the logger instance logs such as logging only Error-level messages, you'll need to manipulate the backends. This can be done by setting a backend for the Error level and clearing the default backend by setting it to nil.
func (*Log) SetOption ¶
SetOption is the recommended method for setting metadata values in the logger. It's possible to set options by manipulating the Log.Options map directly but there is no stability guarantee provided for its continued export status.
func (*Log) SetRawBackend ¶
SetRawBackend behaves identically to SetBackend with the exception that it does not wrap the backend argument in a SyncWriter.
func (*Log) SetRawBackendForLevel ¶
SetRawBackendForLevel behaves identically to SetBackendForLevel with the exception that it does not wrap the backend argument in a SyncWriter.
func (*Log) SetTimezone ¶
SetTimezone sets the logger's timezone to the specified location. The logger uses time.UTC by default. Change this to time.Local to display the server's local time.
type Logger ¶
type Logger interface { Chain(*Log) DestroyLogger(string) GetLogger(string) *Log Inherit(string) *Log Iterate(func(*Log)) SetBackend(io.Writer) *Log SetRawBackend(io.Writer) *Log SetBackendForLevel(Level, io.Writer) *Log SetRawBackendForLevel(Level, io.Writer) *Log ClearBackend(Level) *Log Name() string FullPath() string SetFormat(string) *Log SetLevel(Level) *Log Level() Level SetOption(string, interface{}) SetTimezone(*time.Location) *Log Error(...interface{}) Errorf(string, ...interface{}) Warn(...interface{}) Warnf(string, ...interface{}) Warning(...interface{}) Warningf(string, ...interface{}) Notice(...interface{}) Noticef(string, ...interface{}) Info(...interface{}) Infof(string, ...interface{}) Information(...interface{}) Informationf(string, ...interface{}) Debug(...interface{}) Debugf(string, ...interface{}) Print(...interface{}) Printf(string, ...interface{}) Println(...interface{}) Fatal(...interface{}) Fatalf(string, ...interface{}) Fatalln(...interface{}) Panic(...interface{}) Panicf(string, ...interface{}) Panicln(...interface{}) Writer() io.Writer }
Logger defines the complete API for the logger implementation contained herein.
type Message ¶
Message instance. This is used internally for the formatter template.
func (*Message) FormatTime ¶
FormatTime is the time formatting function. This is useful if client code wishes to use a Go template pipeline to change the format of the timestamp.
This is a convenience method and isn't required.
type StdLogger ¶
type StdLogger interface { Fatal(...interface{}) Fatalf(string, ...interface{}) Fatalln(...interface{}) Flags() int Output(int, string) error Panic(...interface{}) Panicf(string, ...interface{}) Panicln(...interface{}) Prefix() string Print(...interface{}) Printf(string, ...interface{}) Println(...interface{}) SetFlags(int) SetOutput(io.Writer) SetPrefix(string) Writer() io.Writer }
StdLogger defines an interface matching the logger from the Golang standard library logger.
func CoreLogger ¶
type SyncWriter ¶
SyncWriter provides an implementation of io.Writer that synchronizes logging messages such that interleaving output over a shared writer won't happen.
When using Logging's default settings, the internal reference to os.Stdout will be wrapped.
func (*SyncWriter) Wrapped ¶
func (w *SyncWriter) Wrapped() io.Writer
Wrapped returns the wrapped writer.