Documentation
¶
Overview ¶
Package log can filter log by field and support multiple level
Index ¶
- Variables
- type Config
- type Entry
- func (entry *Entry) AddField(key string, value string)
- func (entry *Entry) AddFields(fields Fields)
- func (entry *Entry) Debug(args ...interface{})
- func (entry *Entry) Debugf(format string, args ...interface{})
- func (entry *Entry) DeleteField(key string)
- func (entry *Entry) Error(args ...interface{})
- func (entry *Entry) Errorf(format string, args ...interface{})
- func (entry *Entry) Fatal(args ...interface{})
- func (entry *Entry) Fatalf(format string, args ...interface{})
- func (entry *Entry) Info(args ...interface{})
- func (entry *Entry) Infof(format string, args ...interface{})
- func (entry *Entry) Panic(args ...interface{})
- func (entry *Entry) Panicf(format string, args ...interface{})
- func (entry *Entry) SetEntryLevel(s string) error
- func (entry *Entry) SetPkgAlias(alias string)
- func (entry *Entry) Trace(args ...interface{})
- func (entry *Entry) Tracef(format string, args ...interface{})
- func (entry *Entry) Warn(args ...interface{})
- func (entry *Entry) Warnf(format string, args ...interface{})
- type Fields
- type Filter
- type Formatter
- type Level
- type Logger
- func (log *Logger) AddFilter(filter Filter, level Level)
- func (log *Logger) ApplyConfig(c *Config) error
- func (log *Logger) DisableSourceLine()
- func (log *Logger) EnableSourceLine()
- func (log *Logger) NewEntry() *Entry
- func (log *Logger) NewEntryWithPkg(pkgName string) *Entry
- func (log *Logger) PrintEntries()
- func (log *Logger) RegisterPkg() *Entry
- func (log *Logger) RegisteredPkgs() map[string]*Entry
- func (log *Logger) SetLevel(s string) error
- type PkgFilter
- type TextFormatter
Constants ¶
This section is empty.
Variables ¶
var AllLevels = []Level{ FatalLevel, PanicLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel, TraceLevel, }
AllLevels includes all the logging level
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Level string `yaml:"level" json:"level"`
Color bool `yaml:"color" json:"color"`
Source bool `yaml:"source" json:"source"`
ShowElapsedTime bool `yaml:"showElapsedTime" json:"showElapsedTime"`
TimeFormat string `yaml:"timeFormat" json:"timeFormat"`
XXX map[string]interface{} `yaml:",inline"`
}
type Entry ¶
type Entry struct {
Logger *Logger
Pkg string // TODO: we should make use of this and stop storing the it in the map field?
EntryLevel Level
Fields Fields
Time time.Time
Level Level
Message string
}
Entry is the real logger
func (*Entry) DeleteField ¶
DeleteField remove a tag from entry, this was added for benchmark to remove the automatically added pkg tag when using RegisterPkg
func (*Entry) SetEntryLevel ¶
func (*Entry) SetPkgAlias ¶
SetPkgAlias allows use shorter name for pkg when logging
type Level ¶
type Level uint8
Level is log level
const ( // FatalLevel log error and call `os.Exit(1)` FatalLevel Level = iota // PanicLevel log error and call `panic` PanicLevel // ErrorLevel log error ErrorLevel // WarnLevel log warning WarnLevel // InfoLevel log info InfoLevel // DebugLevel log debug message, user should enable DebugLevel logging when report bug DebugLevel // TraceLevel is very verbose, user should enable it only on packages they are currently investing instead of globally TraceLevel )
func ParseLevel ¶
ParseLevel match the level string with Level, it will use strings.HasPrefix in non strict mode
func (Level) ShortUpperString ¶
ShortUpperString returns the first 4 characters of a level in upper case
type Logger ¶
type Logger struct {
Out io.Writer
Formatter Formatter
Level Level
Filters map[Level]map[string]Filter
Entries map[string]*Entry
// contains filtered or unexported fields
}
Logger is used to set output, formatter and filters, the real log operation is in Entry
func (*Logger) AddFilter ¶
AddFilter add a filter to logger, the filter should be simple string check on fields, i.e. PkgFilter check pkg field
func (*Logger) ApplyConfig ¶
func (*Logger) DisableSourceLine ¶
func (log *Logger) DisableSourceLine()
DisableSourceLine does not show `source` field
func (*Logger) EnableSourceLine ¶
func (log *Logger) EnableSourceLine()
EnableSourceLine add `source` field when logging, it use runtime.Caller(), the overhead has not been measured
func (*Logger) NewEntry ¶
NewEntry returns an Entry with empty Fields Deprecated: use RegisterPkg instead
func (*Logger) NewEntryWithPkg ¶
NewEntryWithPkg returns an Entry with pkg Field set to pkgName, should be used with PkgFilter Deprecated: use RegisterPkg instead
func (*Logger) PrintEntries ¶
func (log *Logger) PrintEntries()
func (*Logger) RegisterPkg ¶
RegisterPkg creates a new entry with pkg field set to the caller's package and register this entry to logger
func (*Logger) RegisteredPkgs ¶
type PkgFilter ¶
type PkgFilter struct {
// contains filtered or unexported fields
}
PkgFilter only allows entry without `pkg` field or `pkg` value in the allow set to pass TODO: we should support level TODO: a more efficient way might be trie tree and use `/` to divide package into segments instead of using character
func NewPkgFilter ¶
NewPkgFilter returns a filter that allow log that contains `pkg` filed in the allow set
func (*PkgFilter) Accept ¶
Accept checks if the entry.Pkg (NOT entry.Fields["pkg"]) is in the white list
func (*PkgFilter) FilterDescription ¶
func (*PkgFilter) FilterName ¶
FilterName implements Filter interface
type TextFormatter ¶
type TextFormatter struct {
EnableColor bool
EnableTimeStamp bool
EnableElapsedTime bool
TimeStampFormat string
}
func NewTextFormatter ¶
func NewTextFormatter() *TextFormatter
func (*TextFormatter) SetColor ¶
func (f *TextFormatter) SetColor(b bool)
func (*TextFormatter) SetElapsedTime ¶
func (f *TextFormatter) SetElapsedTime(b bool)
func (*TextFormatter) SetTimeFormat ¶
func (f *TextFormatter) SetTimeFormat(tf string)