Documentation
¶
Overview ¶
Package glog is a go logging library.
Primarily, it is a convenience wrapper around go-logging (specifically shenwei356's fork at https://github.com/shenwei356/go-logging).
glog provides convenience functions for configuring commonly-used logging backends (console and file) and for submitting log messages via a (package-scoped) global logger, akin to the print-style helper methods in the standard library log package. It also includes additional backends: a convenience file-based backend and an (unlimited-size) in-memory list backend. This list backend is intended for use in relatively short-lived scenarios, such as batch-processing operations where the log output from each batch is to be treated independently (e.g., conditionally stored or transmitted). In such scenarios, one would clear the backend at the beginning of each batch run and decide what to do with the results at the end. A summary (the number of logged messages of each log level) is available to aid in conditional use.
Example ¶
// Remove any existing backends
ClearBackends()
// Add a backend
SetBackend("default", // Backend name
NewWriterBackend(
os.Stderr, // Write to stderr
"", // Empty/unspecified module
Debug, // Debug-level and above records will be logged
"", // Use the default message format
))
// Log some messages
Debugf("Debug message")
Infof("Info message")
Noticef("Notice message")
Warningf("Warning message")
Errorf("Error message")
Criticalf("Critical message")
// Change "default" log level. Note that this simply replaces the previously-defined backend.
SetBackend("default", // Backend name
NewWriterBackend(
os.Stderr, // Write to stderr
"", // Empty/unspecified module
Warning, // Warning-level and above records will be logged
"", // Use the default message format
))
Index ¶
- func ClearBackends() error
- func Close()
- func Criticalf(format string, objects ...interface{})
- func Debugf(format string, objects ...interface{})
- func Errorf(format string, objects ...interface{})
- func Infof(format string, objects ...interface{})
- func Logf(level LogLevel, format string, objects ...interface{})
- func NewWriterBackend(writer io.Writer, module string, level LogLevel, format string) logging.Backend
- func Noticef(format string, objects ...interface{})
- func RemoveBackend(name string) error
- func SetBackend(name string, backend logging.Backend) error
- func Warningf(format string, objects ...interface{})
- type FileBackend
- type ListBackend
- type LogLevel
- type Record
- type RecordSummary
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
func Close()
Close shuts down the logging system, performing cleanup such as flushing and closing files.
func Criticalf ¶
func Criticalf(format string, objects ...interface{})
Criticalf logs at CRITICAL level with a format string and set of objects.
func Debugf ¶
func Debugf(format string, objects ...interface{})
Debugf logs at DEBUG level with a format string and set of objects.
func Errorf ¶
func Errorf(format string, objects ...interface{})
Errorf logs at ERROR level with a format string and set of objects.
func Infof ¶
func Infof(format string, objects ...interface{})
Infof logs at INFO level with a format string and set of objects.
func NewWriterBackend ¶
func NewWriterBackend(writer io.Writer, module string, level LogLevel, format string) logging.Backend
NewWriterBackend creates a new backend for a supplied io.Writer.
func Noticef ¶
func Noticef(format string, objects ...interface{})
Noticef logs at NOTICE level with a format string and set of objects.
func SetBackend ¶
SetBackend adds or replaces a named backend.
Types ¶
type FileBackend ¶
Example ¶
// As we will be using a file-based backend,
// ensure things (eventually) get cleaned up.
defer Close()
// Remove any existing backends
ClearBackends()
// Add a backend
SetBackend("default", // Backend name
NewFileBackend(
"my_app.log", // Filename
true, // Append if file exists otherwise create
"", // Empty/unspecified module
Debug, // Debug-level and above records will be logged
"", // Use the default message format
))
// Log some messages
Debugf("Debug message")
Infof("Info message")
Noticef("Notice message")
Warningf("Warning message")
Errorf("Error message")
Criticalf("Critical message")
func NewFileBackend ¶
func NewFileBackend(filename string, append bool, module string, level LogLevel, format string) *FileBackend
NewFileBackend creates a new file-based backend.
func (FileBackend) Close ¶
func (fb FileBackend) Close()
type ListBackend ¶
type ListBackend struct {
// contains filtered or unexported fields
}
ListBackend provides a simple list-based store of log records.
Example ¶
ClearBackends()
backendName := "session"
backend := NewListBackend("", Debug)
SetBackend(backendName, backend)
// Produce some log messages
// Retrieve all Warning-and-above messages since backend creation
sessionContent := backend.Get(Warning)
for _, record := range sessionContent {
fmt.Printf("Level: %s, message: %s", record.Level, record.Message)
}
// Clear backend
backend.Clear()
// Produce some more log messages
// Retrieve all Info-and-above messages since backend was last clearec
sessionContent = backend.Get(Info)
func NewListBackend ¶
func NewListBackend(module string, level LogLevel) *ListBackend
NewListBackend creates a new record-list based backend.
func (*ListBackend) Clear ¶
func (backend *ListBackend) Clear()
Clear removes all stored records from the backend.
func (*ListBackend) Get ¶
func (backend *ListBackend) Get(minimumLevel LogLevel) []Record
Get retrieves all stored log records at or above the specified minimim level.
func (*ListBackend) Log ¶
func (backend *ListBackend) Log(vendorLevel logging.Level, calldepth int, record *logging.Record) error
Log implements the Log function required by the Backend interface.
func (*ListBackend) Summary ¶
func (backend *ListBackend) Summary() []*RecordSummary
Summary retrieves a summary of the counts of all records at each level.
type LogLevel ¶
type LogLevel int
LogLevel represents the available logging levels.
func ListLogLevels ¶
func ListLogLevels() []LogLevel
ListLogLevels returns an ordered list of available log levels, useful for iteration.
func NewLogLevel ¶
NewLogLevel returns a LogLevel for the specified level