Documentation ¶
Overview ¶
Package colog implements prefix based logging by setting itself as output of the standard library and parsing the log messages. Level prefixes are called headers in CoLog terms to not confuse with log.Prefix() which is independent. Basic usage only requires registering:
func main() { colog.Register() log.Print("info: that's all it takes!") }
CoLog requires the standard logger to submit messages without prefix or flags. So it resets them while registering and assigns them to itself, unfortunately CoLog cannot be aware of any output previously set.
Index ¶
- func AddHeader(header string, level Level)
- func AddHook(hook Hook)
- func ClearFixedValues()
- func FixedValue(key string, value interface{})
- func Flags() int
- func ParseFields(active bool)
- func Register()
- func SetDefaultLevel(l Level)
- func SetExtractor(ex Extractor)
- func SetFlags(flags int)
- func SetFormatter(f Formatter)
- func SetHeaders(headers HeaderMap)
- func SetHost(host string)
- func SetMinLevel(l Level)
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- type CoLog
- func (cl *CoLog) AddHeader(header string, level Level)
- func (cl *CoLog) AddHook(hook Hook)
- func (cl *CoLog) ClearFixedValues()
- func (cl *CoLog) FixedValue(key string, value interface{})
- func (cl *CoLog) Flags() int
- func (cl *CoLog) NewLogger() *log.Logger
- func (cl *CoLog) ParseFields(active bool)
- func (cl *CoLog) SetDefaultLevel(l Level)
- func (cl *CoLog) SetExtractor(ex Extractor)
- func (cl *CoLog) SetFlags(flags int)
- func (cl *CoLog) SetFormatter(f Formatter)
- func (cl *CoLog) SetHeaders(headers HeaderMap)
- func (cl *CoLog) SetHost(host string)
- func (cl *CoLog) SetMinLevel(l Level)
- func (cl *CoLog) SetOutput(w io.Writer)
- func (cl *CoLog) SetPrefix(prefix string)
- func (cl *CoLog) Write(p []byte) (n int, err error)
- type ColorFormatter
- type ColorSupporter
- type Entry
- type Extractor
- type Fields
- type Formatter
- type HeaderMap
- type Hook
- type JSONEntry
- type JSONFormatter
- type Level
- type LevelMap
- type StdExtractor
- type StdFormatter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHeader ¶
AddHeader adds a custom header to the input headers to be search for to determine the level for the standard logger
func AddHook ¶
func AddHook(hook Hook)
AddHook adds a hook to be fired on every event with matching level being logged on the standard logger
func ClearFixedValues ¶
func ClearFixedValues()
ClearFixedValues removes all previously set field-value in the standard logger
func FixedValue ¶
func FixedValue(key string, value interface{})
FixedValue sets a field-value pair that will get automatically added to every log entry in the standard logger
func ParseFields ¶
func ParseFields(active bool)
ParseFields activates or deactivates field parsing in the message for the standard logger
func Register ¶
func Register()
Register sets CoLog as output for the default logger. It "hijacks" the standard logger flags and prefix previously set. It's not possible to know the output previously set, so the default os.Stderr is assumed.
func SetDefaultLevel ¶
func SetDefaultLevel(l Level)
SetDefaultLevel sets the level that will be used when no level is detected for the standard logger
func SetExtractor ¶
func SetExtractor(ex Extractor)
SetExtractor sets the extractor to use by the standard logger
func SetFlags ¶
func SetFlags(flags int)
SetFlags sets the output flags for the standard log formatter if any
func SetFormatter ¶
func SetFormatter(f Formatter)
SetFormatter sets the formatter to use by the standard logger
func SetHeaders ¶
func SetHeaders(headers HeaderMap)
SetHeaders sets custom headers as the input headers to be search for to determine the level for the standard logger
func SetHost ¶
func SetHost(host string)
SetHost sets the logger hostname assigned to the entries of the standard logger
func SetMinLevel ¶
func SetMinLevel(l Level)
SetMinLevel sets the minimum level that will be actually logged by the standard logger
Types ¶
type CoLog ¶
type CoLog struct {
// contains filtered or unexported fields
}
CoLog encapsulates our log writer
func (*CoLog) AddHeader ¶
AddHeader adds a custom header to the input headers to be search for to determine the level
func (*CoLog) AddHook ¶
AddHook adds a hook to be fired on every event with matching level being logged. See the hook interface
func (*CoLog) ClearFixedValues ¶
func (cl *CoLog) ClearFixedValues()
ClearFixedValues removes all previously set fields from the logger
func (*CoLog) FixedValue ¶
FixedValue sets a key-value pair that will get automatically added to every log entry in this logger
func (*CoLog) ParseFields ¶
ParseFields activates or deactivates field parsing in the message
func (*CoLog) SetDefaultLevel ¶
SetDefaultLevel sets the level that will be used when no level is detected
func (*CoLog) SetExtractor ¶
SetExtractor sets the formatter to use
func (*CoLog) SetFormatter ¶
SetFormatter sets the formatter to use
func (*CoLog) SetHeaders ¶
SetHeaders sets custom headers as the input headers to be search for to determine the level
func (*CoLog) SetMinLevel ¶
SetMinLevel sets the minimum level that will be actually logged
type ColorFormatter ¶
ColorFormatter interface can be implemented by formatters to get notifications on whether the output supports color
type ColorSupporter ¶
type ColorSupporter interface {
ColorSupported() bool
}
ColorSupporter interface can be implemented by "smart" outputs that want to handle color display themselves
type Entry ¶
type Entry struct { Level Level // severity: trace, debug, info, warning, error, alert Time time.Time // time of the event Host string // host origin of the message Prefix string // Prefix set to the logger File string // file where the log was called Line int // line in the file where the log was called Message []byte // logged message Fields Fields // map of key-value data parsed from the message }
Entry represents a message being logged and all attached data
type Extractor ¶
Extractor interface must be implemented by data extractors the extractor reads the message and tries to extract key-value pairs from the message and sets the in the entry
type Formatter ¶
type Formatter interface { Format(*Entry) ([]byte, error) // The actual formatter called every time SetFlags(flags int) // Like the standard log.SetFlags(flags int) Flags() int // Like the standard log.Flags() int }
Formatter interface must be implemented by message formatters Format(*Entry) will be called and the resulting bytes sent to output
type Hook ¶
type Hook interface { Levels() []Level // returns the set of levels for which the hook should be triggered Fire(*Entry) error // triggers the hook, this function will be called for every eligible log entry }
Hook is the interface to be implemented by event hooks
type JSONEntry ¶
type JSONEntry struct { Level string `json:"level,omitempty"` Time string `json:"time,omitempty"` Host string `json:"host,omitempty"` Prefix string `json:"prefix,omitempty"` File string `json:"file,omitempty"` Line int `json:"line,omitempty"` Message string `json:"message,omitempty"` Fields Fields `json:"fields,omitempty"` }
JSONEntry is an entry with the final JSON field types We can not just implement the Marshaller interface since some of the process depends on runtime options
type JSONFormatter ¶
type JSONFormatter struct { TimeFormat string LevelAsNum bool Flag int // contains filtered or unexported fields }
JSONFormatter serializes entries to JSON TimeFormat can be any Go time format, if empty it will mimic the standard logger format LevelAsNum will use a numeric string "1", "2",... for as levels instead of "trace", "debug", ..
func (*JSONFormatter) Flags ¶
func (jf *JSONFormatter) Flags() int
Flags returns the output flags for the formatter.
func (*JSONFormatter) Format ¶
func (jf *JSONFormatter) Format(e *Entry) ([]byte, error)
Format takes and entry and returns the formatted output in bytes
func (*JSONFormatter) SetFlags ¶
func (jf *JSONFormatter) SetFlags(flags int)
SetFlags sets the output flags for the formatter.
type Level ¶
type Level uint8
Level represents severity level
const ( // LTrace represents trace severity level LTrace Level // LDebug represents debug severity level LDebug // LInfo represents info severity level LInfo // LWarning represents warning severity level LWarning // LError represents error severity level LError // LAlert represents alert severity level LAlert )
func ParseLevel ¶
ParseLevel parses a string into a type Level
type StdExtractor ¶
type StdExtractor struct {
// contains filtered or unexported fields
}
StdExtractor implements a regex based extractor for key-value pairs both unquoted foo=bar and quoted foo="some bar" are supported
func (*StdExtractor) Extract ¶
func (se *StdExtractor) Extract(e *Entry) error
Extract finds key-value pairs in the message and sets them as Fields in the entry removing the pairs from the message.
type StdFormatter ¶
type StdFormatter struct { Flag int HeaderPlain LevelMap HeaderColor LevelMap Colors bool // Force enable colors NoColors bool // Force disable colors (has preference) // contains filtered or unexported fields }
StdFormatter supports plain and color level headers and bold/padded fields
func (*StdFormatter) ColorSupported ¶
func (sf *StdFormatter) ColorSupported(supp bool)
ColorSupported enables or disables the colors, this will be called on every
func (*StdFormatter) Flags ¶
func (sf *StdFormatter) Flags() int
Flags returns the output flags for the formatter.
func (*StdFormatter) Format ¶
func (sf *StdFormatter) Format(e *Entry) ([]byte, error)
Format takes and entry and returns the formatted output in bytes
func (*StdFormatter) SetFlags ¶
func (sf *StdFormatter) SetFlags(flags int)
SetFlags sets the output flags for the formatter.