Documentation ¶
Overview ¶
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 ¶
- func Default(lvl Level)
- func Exclude(packagenames ...string) filters
- func FilterFunc() func(*LogLine) bool
- func Flags() int
- func Include(packagenames ...string) filters
- func LevelToString(l Level) string
- func Output() io.Writer
- func Prefix() string
- func SetFilterFunc(f func(*LogLine) bool)
- func SetFlags(f int)
- func SetFormatter(f Format)
- func SetOutput(o io.Writer)
- func SetParsers(p []Parser)
- func SetPrefix(p string)
- func SqrFormat(prefix string, l *LogLine, f int) []byte
- func StdFilter(l *LogLine) bool
- func StdFilterReset()
- func StdFormat(prefix string, l *LogLine, f int) []byte
- type Format
- type Level
- type LogLine
- type Logger
- func (l *Logger) FilterFunc() func(*LogLine) bool
- func (l *Logger) Flags() int
- func (l *Logger) Formatter() Format
- func (l *Logger) Output() io.Writer
- func (l *Logger) Parsers() []Parser
- func (l *Logger) Prefix() string
- func (l *Logger) SetFilterFunc(f func(*LogLine) bool)
- func (l *Logger) SetFlags(f int)
- func (l *Logger) SetFormatter(f Format)
- func (l *Logger) SetOutput(o io.Writer)
- func (l *Logger) SetParsers(p []Parser)
- func (l *Logger) SetPrefix(p string)
- func (l *Logger) Write(p []byte) (int, error)
- type Parser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
func Default(lvl Level)
Default sets the logging level the is output by all packages.
func Exclude ¶
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 ¶
FilterFunc returns the standard filter function
func Include ¶
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 ¶
LevelToString converts a Level (i.e. Error) to the corresponding string version (i.e. "Error").
func SetFilterFunc ¶
SetFilterFunc Set the Filter Function on the default logger.
func SetFlags ¶
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 ¶
func SetFormatter(f Format)
SetFormatter sets the output formatter of the std logger.
func StdFilter ¶
StdFilter is the default implementation used by logger for filtering. Returns true if the line is written out.
func StdFilterReset ¶
func StdFilterReset()
StdFilterReset resets the filters being applied, which is useful for testing.
Types ¶
type Format ¶
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`.
type Level ¶
type Level int
Level represents a logging level.
Standard(ish) Logging Levels.
func StringToLevel ¶
StringToLevel converts a string log level (i.e. "Error") to the corresponding Level (i.e. Error).
type LogLine ¶
LogLine struct representing the parsed log message.
func StringToLogLine ¶
StringToLogLine converts the default log sting sent to the LogLine type.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger used to capture logging output prior to filtering/output.
func (*Logger) FilterFunc ¶
FilterFunc returns the Current Filter Function
func (*Logger) SetFilterFunc ¶
SetFilterFunc set the filter function on the logger.
func (*Logger) SetFormatter ¶
SetFormatter sets the output formatter of the logger.
func (*Logger) SetParsers ¶
SetParsers sets the parsers used by the logger.