import "github.com/comail/colog"
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.
colog.go interfaces.go json_formatter.go std_extractor.go std_formatter.go tty.go tty_linux.go
AddHeader adds a custom header to the input headers to be search for to determine the level for the standard logger
AddHook adds a hook to be fired on every event with matching level being logged on the standard logger
func ClearFixedValues()
ClearFixedValues removes all previously set field-value in the standard logger
FixedValue sets a field-value pair that will get automatically added to every log entry in the standard logger
Flags returns the output flags for the standard log formatter if any
ParseFields activates or deactivates field parsing in the message for the standard logger
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.
SetDefaultLevel sets the level that will be used when no level is detected for the standard logger
SetExtractor sets the extractor to use by the standard logger
SetFlags sets the output flags for the standard log formatter if any
SetFormatter sets the formatter to use by the standard logger
SetHeaders sets custom headers as the input headers to be search for to determine the level for the standard logger
SetHost sets the logger hostname assigned to the entries of the standard logger
SetMinLevel sets the minimum level that will be actually logged by the standard logger
SetOutput is analog to log.SetOutput sets the output destination for the standard logger
SetPrefix sets the logger output prefix of the standard logger
type CoLog struct {
// contains filtered or unexported fields
}
CoLog encapsulates our log writer
NewCoLog returns CoLog instance ready to be used in logger.SetOutput()
AddHeader adds a custom header to the input headers to be search for to determine the level
AddHook adds a hook to be fired on every event with matching level being logged. See the hook interface
ClearFixedValues removes all previously set fields from the logger
FixedValue sets a key-value pair that will get automatically added to every log entry in this logger
Flags returns the output flags for the formatter if any
NewLogger returns a colog-enabled logger
ParseFields activates or deactivates field parsing in the message
SetDefaultLevel sets the level that will be used when no level is detected
SetExtractor sets the formatter to use
SetFlags sets the output flags for the formatter if any
SetFormatter sets the formatter to use
SetHeaders sets custom headers as the input headers to be search for to determine the level
SetHost sets the logger hostname assigned to the entries
SetMinLevel sets the minimum level that will be actually logged
SetOutput is analog to log.SetOutput sets the output destination.
SetPrefix sets the logger output prefix
Write implements io.Writer interface to that the standard logger uses.
ColorFormatter interface can be implemented by formatters to get notifications on whether the output supports color
ColorSupporter interface can be implemented by "smart" outputs that want to handle color display themselves
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
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
Fields is the key-value map for extracted data
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
HeaderMap links input header strings with levels
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 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 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 (jf *JSONFormatter) Flags() int
Flags returns the output flags for the formatter.
func (jf *JSONFormatter) Format(e *Entry) ([]byte, error)
Format takes and entry and returns the formatted output in bytes
func (jf *JSONFormatter) SetFlags(flags int)
SetFlags sets the output flags for the formatter.
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 )
ParseLevel parses a string into a type Level
String implements the Stringer interface for levels
LevelMap links levels with output header bytes
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 (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 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 (sf *StdFormatter) ColorSupported(supp bool)
ColorSupported enables or disables the colors, this will be called on every
func (sf *StdFormatter) Flags() int
Flags returns the output flags for the formatter.
func (sf *StdFormatter) Format(e *Entry) ([]byte, error)
Format takes and entry and returns the formatted output in bytes
func (sf *StdFormatter) SetFlags(flags int)
SetFlags sets the output flags for the formatter.
Package colog imports 15 packages (graph) and is imported by 43 packages. Updated 2019-03-01. Refresh now. Tools for package owners.