glog

package module
v0.0.0-...-94476c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2019 License: MIT Imports: 7 Imported by: 1

README

glog

GitHub Release Go Doc Go Report Card

Build Status GolangCI

Package glog is a go logging library.

Primarily, it is a convenience wrapper around go-logging (specifically the fork shenwei356/go-logging that adds colour support for Windows).

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.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearBackends

func ClearBackends() error

ClearBackends removes all backends.

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 Logf

func Logf(level LogLevel, format string, objects ...interface{})

Logf logs at the specified 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 RemoveBackend

func RemoveBackend(name string) error

RemoveBackend removes a named backend.

func SetBackend

func SetBackend(name string, backend logging.Backend) error

SetBackend adds or replaces a named backend.

func Warningf

func Warningf(format string, objects ...interface{})

Warningf logs at WARNING level with a format string and set of objects.

Types

type FileBackend

type FileBackend struct {
	logging.Backend
	// contains filtered or unexported fields
}
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.

const (
	Debug LogLevel = iota + 1
	Info
	Notice
	Warning
	Error
	Critical
)

Available log levels.

func ListLogLevels

func ListLogLevels() []LogLevel

ListLogLevels returns an ordered list of available log levels, useful for iteration.

func NewLogLevel

func NewLogLevel(name string) (LogLevel, error)

NewLogLevel returns a LogLevel for the specified level

func (LogLevel) String

func (level LogLevel) String() string

type Record

type Record struct {
	Time    time.Time
	Level   LogLevel
	Message string
}

Record encapsulates a single log entry.

type RecordSummary

type RecordSummary struct {
	Level LogLevel
	Count int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL