logfilter: github.com/d2g/logfilter Index | Files | Directories

package logfilter

import "github.com/d2g/logfilter"

Package logfilter implements a filter on the standard log package.

Like most alternative logging packages using this package support the semi standard logging levels like:

Trace
Debug
Info
Warning
Error
Fatal

Unlike other packages log filter doesn't require the package to specifically import an additional package over the standard logging package. It does however require them to follow a convention.

The default convention is:

log.Println("<Level>:<Message>")

i.e.

log.Println("Trace:Example Message") is a trace level message.

When logging at trace level it's usual to be very verbose which results in a large amount of output. Filtering this information down can result in it being easier to debug issues and system requirements while producing logging information(i.e. Disk IO).

Logfilter allows you to filter this output based on the package/file location and the log level type.

For example in your live application you may only want to output warning messages:

import(
	"log"
	"github.com/d2g/logfilter"
	"github.com/d2g/logfilter/dummy"
)

func main() {
	// Change the default filter to warning and above.
	logfilter.Default(logfilter.Warning)

	// I want to debug an issue in a particular package so want logging from that package.
	logfilter.Include("github.com/d2g/logfilter/dummy")

	// However at this stage I want only Info and above.
	logfilter.Include("github.com/d2g/logfilter/dummy").When(logfilter.Info)

	// Now only log level Warning and above will be written
	// Except for github.com/d2g/dummy which will have Info and above.
	log.Println("Debug: Not Displayed")
	dummy.Debug()
	dummy.Info()

	//Output:
	//dummy.go:17: Info: This is a Info message
}

If you've previously used logutils or a square based convention then look at the example included in example_logutils_test.go

Index

Package Files

capture.go doc.go filter.go init.go levels.go

func Default Uses

func Default(lvl Level)

Default sets the logging level the is output by all packages.

func Exclude Uses

func Exclude(packagenames ...string) filters

Exclude adds filter object(s) to the standard filter and returns pointers to the newly created object to allow you to set the required level. As default it sets the level to off (i.e. highest).

func FilterFunc Uses

func FilterFunc() func(*LogLine) bool

FilterFunc returns the standard filter function

func Flags Uses

func Flags() int

Flags returns the output flags of the std logger.

func Include Uses

func Include(packagenames ...string) filters

Include adds filter object(s) to the standard filter and returns pointers to the newly created object to allow you to set the required level. As default it sets the level to undefined (i.e. lowest).

func LevelToString Uses

func LevelToString(l Level) string

LevelToString converts a Level (i.e. Error) to the corresponding string version (i.e. "Error").

func Output Uses

func Output() io.Writer

Output returns the output io.writer of the std logger.

func Parsers Uses

func Parsers() []Parser

Parsers returns the current parsers in use by the std logger.

func Prefix Uses

func Prefix() string

Prefix returns the prefix of the std logger.

func SetFilterFunc Uses

func SetFilterFunc(f func(*LogLine) bool)

SetFilterFunc Set the Filter Function on the default logger.

func SetFlags Uses

func SetFlags(f int)

SetFlags sets the output flags on the default logger. The flags match those used by the standard log package.

func SetFormatter Uses

func SetFormatter(f Format)

SetFormatter sets the output formatter of the std logger.

func SetOutput Uses

func SetOutput(o io.Writer)

SetOutput sets the output io.writer of the std logger.

func SetParsers Uses

func SetParsers(p []Parser)

SetParsers sets the parsers used by the std logger.

func SetPrefix Uses

func SetPrefix(p string)

SetPrefix sets the prefix of the std logger.

func SqrFormat Uses

func SqrFormat(prefix string, l *LogLine, f int) []byte

SqrFormat generates the square output format [Level]: Message

func StdFilter Uses

func StdFilter(l *LogLine) bool

StdFilter is the default implementation used by logger for filtering. Returns true if the line is written out.

func StdFilterReset Uses

func StdFilterReset()

StdFilterReset resets the filters being applied, which is useful for testing.

func StdFormat Uses

func StdFormat(prefix string, l *LogLine, f int) []byte

StdFormat generates the standard output format Level: Message

type Format Uses

type Format func(string, *LogLine, int) []byte

Format is the func type required for formatting the output of log messages.It allows messages logged in one packages as `Level: Message` to be output as `[Level] Message`.

func Formatter Uses

func Formatter() Format

Formatter returns the formatter function of the std logger.

type Level Uses

type Level int

Level represents a logging level.

const (
    Undefined Level = iota
    Trace
    Debug
    Info
    Warning
    Error
    Fatal
    Off
)

Standard(ish) Logging Levels.

func SqrParser Uses

func SqrParser(m string) (Level, string)

SqrParser square convention parser [Level] Message

func StdParser Uses

func StdParser(m string) (Level, string)

StdParser is the standard convention parser Level: Message

func StringToLevel Uses

func StringToLevel(sl string) Level

StringToLevel converts a string log level (i.e. "Error") to the corresponding Level (i.e. Error).

type LogLine Uses

type LogLine struct {
    Timestamp time.Time
    File      string
    Line      int
    Message   string

    Level Level
}

LogLine struct representing the parsed log message.

func StringToLogLine Uses

func StringToLogLine(m string) LogLine

StringToLogLine converts the default log sting sent to the LogLine type.

type Logger Uses

type Logger struct {
    // contains filtered or unexported fields
}

Logger used to capture logging output prior to filtering/output.

func New Uses

func New(o io.Writer, p string, f int) *Logger

New creates a new Logger

func (*Logger) FilterFunc Uses

func (l *Logger) FilterFunc() func(*LogLine) bool

FilterFunc returns the Current Filter Function

func (*Logger) Flags Uses

func (l *Logger) Flags() int

Flags returns the output flags of the logger

func (*Logger) Formatter Uses

func (l *Logger) Formatter() Format

Formatter returns the formatter function of the logger.

func (*Logger) Output Uses

func (l *Logger) Output() io.Writer

Output returns the output io.writer of the logger.

func (*Logger) Parsers Uses

func (l *Logger) Parsers() []Parser

Parsers returns the current parsers in use by the logger.

func (*Logger) Prefix Uses

func (l *Logger) Prefix() string

Prefix returns the prefix of the logger.

func (*Logger) SetFilterFunc Uses

func (l *Logger) SetFilterFunc(f func(*LogLine) bool)

SetFilterFunc set the filter function on the logger.

func (*Logger) SetFlags Uses

func (l *Logger) SetFlags(f int)

SetFlags sets the default flag on the logger.

func (*Logger) SetFormatter Uses

func (l *Logger) SetFormatter(f Format)

SetFormatter sets the output formatter of the logger.

func (*Logger) SetOutput Uses

func (l *Logger) SetOutput(o io.Writer)

SetOutput sets the output io.writer of the logger.

func (*Logger) SetParsers Uses

func (l *Logger) SetParsers(p []Parser)

SetParsers sets the parsers used by the logger.

func (*Logger) SetPrefix Uses

func (l *Logger) SetPrefix(p string)

SetPrefix sets the prefix of the logger.

func (*Logger) Write Uses

func (l *Logger) Write(p []byte) (int, error)

Write is the implement the io.Writer to capture the message being written to log.

type Parser Uses

type Parser func(string) (Level, string)

The Parser func type allows you to add additional logging conventions to interpret different conventions.

Directories

PathSynopsis
dummyDummy package for making some noise to demonstrate github.com/d2g/logfilter

Package logfilter imports 6 packages (graph). Updated 2017-11-26. Refresh now. Tools for package owners.