colog: comail.io/go/colog Index | Files

package colog

import "comail.io/go/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.

Index

Package Files

colog.go interfaces.go json_formatter.go std_extractor.go std_formatter.go tty.go tty_linux.go

func AddHeader Uses

func AddHeader(header string, level Level)

AddHeader adds a custom header to the input headers to be search for to determine the level for the standard logger

func AddHook Uses

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 Uses

func ClearFixedValues()

ClearFixedValues removes all previously set field-value in the standard logger

func FixedValue Uses

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 Flags Uses

func Flags() int

Flags returns the output flags for the standard log formatter if any

func ParseFields Uses

func ParseFields(active bool)

ParseFields activates or deactivates field parsing in the message for the standard logger

func Register Uses

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 Uses

func SetDefaultLevel(l Level)

SetDefaultLevel sets the level that will be used when no level is detected for the standard logger

func SetExtractor Uses

func SetExtractor(ex Extractor)

SetExtractor sets the extractor to use by the standard logger

func SetFlags Uses

func SetFlags(flags int)

SetFlags sets the output flags for the standard log formatter if any

func SetFormatter Uses

func SetFormatter(f Formatter)

SetFormatter sets the formatter to use by the standard logger

func SetHeaders Uses

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 Uses

func SetHost(host string)

SetHost sets the logger hostname assigned to the entries of the standard logger

func SetMinLevel Uses

func SetMinLevel(l Level)

SetMinLevel sets the minimum level that will be actually logged by the standard logger

func SetOutput Uses

func SetOutput(w io.Writer)

SetOutput is analog to log.SetOutput sets the output destination for the standard logger

func SetPrefix Uses

func SetPrefix(prefix string)

SetPrefix sets the logger output prefix of the standard logger

type CoLog Uses

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

CoLog encapsulates our log writer

func NewCoLog Uses

func NewCoLog(out io.Writer, prefix string, flags int) *CoLog

NewCoLog returns CoLog instance ready to be used in logger.SetOutput()

func (*CoLog) AddHeader Uses

func (cl *CoLog) AddHeader(header string, level Level)

AddHeader adds a custom header to the input headers to be search for to determine the level

func (*CoLog) AddHook Uses

func (cl *CoLog) AddHook(hook Hook)

AddHook adds a hook to be fired on every event with matching level being logged. See the hook interface

func (*CoLog) ClearFixedValues Uses

func (cl *CoLog) ClearFixedValues()

ClearFixedValues removes all previously set fields from the logger

func (*CoLog) FixedValue Uses

func (cl *CoLog) FixedValue(key string, value interface{})

FixedValue sets a key-value pair that will get automatically added to every log entry in this logger

func (*CoLog) Flags Uses

func (cl *CoLog) Flags() int

Flags returns the output flags for the formatter if any

func (*CoLog) NewLogger Uses

func (cl *CoLog) NewLogger() *log.Logger

NewLogger returns a colog-enabled logger

func (*CoLog) ParseFields Uses

func (cl *CoLog) ParseFields(active bool)

ParseFields activates or deactivates field parsing in the message

func (*CoLog) SetDefaultLevel Uses

func (cl *CoLog) SetDefaultLevel(l Level)

SetDefaultLevel sets the level that will be used when no level is detected

func (*CoLog) SetExtractor Uses

func (cl *CoLog) SetExtractor(ex Extractor)

SetExtractor sets the formatter to use

func (*CoLog) SetFlags Uses

func (cl *CoLog) SetFlags(flags int)

SetFlags sets the output flags for the formatter if any

func (*CoLog) SetFormatter Uses

func (cl *CoLog) SetFormatter(f Formatter)

SetFormatter sets the formatter to use

func (*CoLog) SetHeaders Uses

func (cl *CoLog) SetHeaders(headers HeaderMap)

SetHeaders sets custom headers as the input headers to be search for to determine the level

func (*CoLog) SetHost Uses

func (cl *CoLog) SetHost(host string)

SetHost sets the logger hostname assigned to the entries

func (*CoLog) SetMinLevel Uses

func (cl *CoLog) SetMinLevel(l Level)

SetMinLevel sets the minimum level that will be actually logged

func (*CoLog) SetOutput Uses

func (cl *CoLog) SetOutput(w io.Writer)

SetOutput is analog to log.SetOutput sets the output destination.

func (*CoLog) SetPrefix Uses

func (cl *CoLog) SetPrefix(prefix string)

SetPrefix sets the logger output prefix

func (*CoLog) Write Uses

func (cl *CoLog) Write(p []byte) (n int, err error)

Write implements io.Writer interface to that the standard logger uses.

type ColorFormatter Uses

type ColorFormatter interface {
    Formatter
    ColorSupported(yes bool)
}

ColorFormatter interface can be implemented by formatters to get notifications on whether the output supports color

type ColorSupporter Uses

type ColorSupporter interface {
    ColorSupported() bool
}

ColorSupporter interface can be implemented by "smart" outputs that want to handle color display themselves

type Entry Uses

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 Uses

type Extractor interface {
    Extract(*Entry) error
}

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 Fields Uses

type Fields map[string]interface{}

Fields is the key-value map for extracted data

type Formatter Uses

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 HeaderMap Uses

type HeaderMap map[string]Level

HeaderMap links input header strings with levels

type Hook Uses

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 Uses

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 Uses

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 Uses

func (jf *JSONFormatter) Flags() int

Flags returns the output flags for the formatter.

func (*JSONFormatter) Format Uses

func (jf *JSONFormatter) Format(e *Entry) ([]byte, error)

Format takes and entry and returns the formatted output in bytes

func (*JSONFormatter) SetFlags Uses

func (jf *JSONFormatter) SetFlags(flags int)

SetFlags sets the output flags for the formatter.

type Level Uses

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 Uses

func ParseLevel(level string) (Level, error)

ParseLevel parses a string into a type Level

func (Level) String Uses

func (level Level) String() string

String implements the Stringer interface for levels

type LevelMap Uses

type LevelMap map[Level][]byte

LevelMap links levels with output header bytes

type StdExtractor Uses

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 Uses

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 Uses

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 Uses

func (sf *StdFormatter) ColorSupported(supp bool)

ColorSupported enables or disables the colors, this will be called on every

func (*StdFormatter) Flags Uses

func (sf *StdFormatter) Flags() int

Flags returns the output flags for the formatter.

func (*StdFormatter) Format Uses

func (sf *StdFormatter) Format(e *Entry) ([]byte, error)

Format takes and entry and returns the formatted output in bytes

func (*StdFormatter) SetFlags Uses

func (sf *StdFormatter) SetFlags(flags int)

SetFlags sets the output flags for the formatter.

Package colog imports 15 packages (graph) and is imported by 4 packages. Updated 2017-09-05. Refresh now. Tools for package owners.