Documentation
¶
Overview ¶
Package kvlog handles messages printed by the `log` package in the Go standard library. It parses the message for key/value pairs and formats the message on terminals with line wrapping and color display.
Example ¶
package main import ( "log" "github.com/jjeffery/kv/kvlog" ) func main() { // optionally setup log prefix and flags log.SetPrefix("test program: ") log.SetFlags(log.LstdFlags | log.LUTC) // attach kvlog for printing to stderr kvlog.Attach() log.Println("program started") }
Output:
Index ¶
- Variables
- func IsTerminal(writer io.Writer) bool
- func SetOutput(writer io.Writer)
- func Suppress(levels ...string)
- type Handler
- type Message
- type Writer
- func (w *Writer) Attach(logger ...*log.Logger)
- func (w *Writer) Handle(h Handler)
- func (w *Writer) IsSuppressed(level string) bool
- func (w *Writer) Levels() map[string]string
- func (w *Writer) SetLevel(level string, effect string)
- func (w *Writer) SetLevels(levels map[string]string)
- func (w *Writer) SetOutput(out io.Writer)
- func (w *Writer) Suppress(levels ...string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Levels is the default configuration for interpreting levels at the // beginning of the message text. A level is mapped to an effect, which // can be a color, "hide" or "none". Levels = map[string]string{ "trace": "none", "debug": "none", "info": "cyan", "warning": "yellow", "error": "red", "alert": "red", "fatal": "red", } // Std is the 'standard' writer, which can be attached to the // 'standard' logger using the Attach() function. Std = NewWriter(os.Stderr) )
Functions ¶
func IsTerminal ¶
IsTerminal returns true if the writer is a terminal.
Types ¶
type Handler ¶
type Handler interface { // Handles reports whether the handler is interested in // handling a message with the given prefix and level. // If none of the handlers want to handle a message, it is // not necessary to allocate the memory for the Message // structure. Handles(prefix, level string) bool // Handle a message written by the logger. The same message // is passed to all handlers on the assumption that the handlers // will not modify its contents. Handle(msg *Message) }
Handler is the interface to implement in order to handle structured messages emitted by the logger.
type Message ¶
type Message struct { Timestamp time.Time // Time that the logger called the output's Write method Prefix string // Prefix from the logger File string // File name and line number from the logger Level string // Message level (eg "debug") Text string // Message text List []string // Key/value pairs }
Message is a structured representation of the text emitted by a standard library logger.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer acts as the output for one or more log.Loggers. It parses each message written by the logger, and passes the information to any handlers registered with the output. The message is then formatted and printed to the output writer. If the output writer is a terminal, it formats the message for improved readability.
func Attach ¶
func Attach() *Writer
Attach configures the 'standard' logger to log via this package. Log output will go to standard error. Use the SetOutput method to override.
func NewWriter ¶
NewWriter creates writer that logs messages to out. If the output writer is a terminal device, the output will be formatted for improved readability.
func (*Writer) Attach ¶
Attach sets this writer as the output destination for the specified logger. If the logger is not specified, then this writer attaches to the log package 'standard' logger.
This method calls SetOutput for the specified logger (or the standard logger) to set its output writer.
func (*Writer) Handle ¶
Handle registers a handler that will be called for every logging event. The function receives a message, which is a structured representation of the log message text.
This function is useful for registering handlers that send log information to external sources.
func (*Writer) IsSuppressed ¶
IsSuppressed reports true if level should be suppressed.
func (*Writer) SetLevels ¶
SetLevels sets a list of levels and their associated actions. It replaces any existing level/effect mapping. If an unknown effect is supplied, a message is logged.