kvlog

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2021 License: MIT Imports: 14 Imported by: 0

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/karlmutch/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

Examples

Constants

This section is empty.

Variables

View Source
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

func IsTerminal(writer io.Writer) bool

IsTerminal returns true if the writer is a terminal.

func SetOutput

func SetOutput(writer io.Writer)

SetOutput sets the output destination for log messages.

func Suppress

func Suppress(levels ...string)

Suppress instructs the Std writer to suppress any message with the specified level.

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

func NewWriter(out io.Writer) *Writer

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

func (w *Writer) Attach(logger ...*log.Logger)

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

func (w *Writer) Handle(h Handler)

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

func (w *Writer) IsSuppressed(level string) bool

IsSuppressed reports true if level should be suppressed.

func (*Writer) Levels

func (w *Writer) Levels() map[string]string

Levels returns a list of levels and their associated actions.

func (*Writer) SetLevel

func (w *Writer) SetLevel(level string, effect string)

SetLevel sets an individual level and its associated display effect.

func (*Writer) SetLevels

func (w *Writer) SetLevels(levels map[string]string)

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.

func (*Writer) SetOutput

func (w *Writer) SetOutput(out io.Writer)

SetOutput sets the output destination for log messages.

func (*Writer) Suppress

func (w *Writer) Suppress(levels ...string)

Suppress instructs the writer to suppress any message with the specified level.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL