log: acln.ro/log Index | Files

package log

import "acln.ro/log"

Index

Package Files

log.go

Constants

const (
    LevelKey     = "level"
    TimestampKey = "ts"
    ComponentKey = "component"
    TaskKey      = "task"
    RegionKey    = "region"
    ErrorKey     = "error"
)

Reserved built-in keys

type Event Uses

type Event string

Event represents an event. It occupies the "event" key in a KV.

func (Event) KV Uses

func (ev Event) KV() KV

KV returns a KV with the event name under the key "event".

type JSONSink Uses

type JSONSink struct {
    Output io.Writer
    // contains filtered or unexported fields
}

JSONSink emits JSON objects to an output stream. JSONSink values must not be copied.

func (*JSONSink) Drain Uses

func (js *JSONSink) Drain(kv KV) error

Drain encodes the specified key-value pairs to JSON, then writes them out to the underyling io.Writer.

When using JSONSink, callers must ensure that all values in the KV map can be JSON-encoded, otherwise the resulting object may be malformed, or encoding might fail.

Drain makes a single Write call to the underlying io.Writer.

type KV Uses

type KV map[string]interface{}

KV is a collection of key-value pairs.

func (KV) KV Uses

func (kv KV) KV() KV

KV returns kv.

func (KV) SortedKeys Uses

func (kv KV) SortedKeys() []string

SortedKeys returns all keys in the map, sorted in the order prescribed by this package. Built-in keys go first, in the order "level", "ts", "component", "task", "region", "error", followed by user-defined keys, sorted lexicographically.

func (KV) String Uses

func (kv KV) String() string

String returns a textual representation of the key-value pairs.

type KVer Uses

type KVer interface {
    KV() KV
}

KVer is any type which can represent itself as a key-value pair.

type Level Uses

type Level uint32

Level represents a log level.

const (
    Quiet Level = 1 + iota
    Error
    Info
    Debug
)

Supported log levels.

func (Level) MarshalJSON Uses

func (lv Level) MarshalJSON() ([]byte, error)

MarshalJSON marshals lv as a JSON string.

func (*Level) Set Uses

func (lv *Level) Set(s string) error

Set implements the flag.Value.Set method.

func (Level) String Uses

func (lv Level) String() string

String returns a textual representation of the log level. The strings are "quiet", "error", "info", and "debug". If lv is not a supported log level, String returns the empty string.

type Logger Uses

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

Logger is a structured, leveled logger.

func New Uses

func New(sink Sink, lv Level) *Logger

New creates a new Logger which forwards logs at or below the specified level to the specified Sink.

func (*Logger) Debug Uses

func (l *Logger) Debug(kvers ...KVer) error

Debug emits a log message at the Debug level.

func (*Logger) Error Uses

func (l *Logger) Error(err error, kvers ...KVer) error

Error emits a log message at the Error level.

func (*Logger) ForComponent Uses

func (l *Logger) ForComponent(component string) *Logger

ForComponent returns a Logger for the specified component. If a component key exists in one of the parent loggers, the specified component name is appended to it in the returned logger, per acln.ro/hiername.

func (*Logger) ForRegion Uses

func (l *Logger) ForRegion(region string) *Logger

ForRegion returns a Logger for the specified region. ForRegion is intended to be used in conjunction with a *runtime/trace.Region. By convention, the region names should match. If a region key exists in one of the parent loggers, the specified region name is appended to it in the returned logger, per acln.ro/hiername.

func (*Logger) ForTask Uses

func (l *Logger) ForTask(task string) *Logger

ForTask returns a Logger for the specified task. ForTask is intended to be used in conjunction with a *runtime/trace.Task. By convention, the task names should match. If a task key exists in one of the parent loggers, the specified task name is appended to it in the returned logger, per acln.ro/hiername.

func (*Logger) Info Uses

func (l *Logger) Info(kvers ...KVer) error

Info emits a log message at the Info level.

func (*Logger) SetLevel Uses

func (l *Logger) SetLevel(lv Level)

SetLevel sets the log level to lv.

func (*Logger) WithKV Uses

func (l *Logger) WithKV(kver ...KVer) *Logger

WithKV returns a new Logger which logs messages with the specified key-value pairs. The keys "level", "ts", "component", "task", "region", "error", and "msg" are reserved.

type Op Uses

type Op string

Op represents an operation. It occupies the "op" key in a KV.

func (Op) KV Uses

func (op Op) KV() KV

KV returns a KV with the operation name under the key "op".

type Sink Uses

type Sink interface {
    Drain(KV) error
}

A Sink encodes key-value pairs and produces a log message. Implementations of Sink must be safe for concurrent use.

Implementations of Sink which produce output where the order of key-value pairs is significant should use KV.SortedKeys to determine the order prescribed by this package.

Implementations of Sink must not modify KV maps.

type Tee Uses

type Tee []Sink

Tee is a Sink which sends KVs to all sinks it contains.

func (Tee) Drain Uses

func (t Tee) Drain(kv KV) error

Drain sends kv to all sinks contained in t. If Sink.Drain returns an error for any Sink, Drain records the first such error and returns it.

type TextSink Uses

type TextSink struct {
    Output io.Writer
    // contains filtered or unexported fields
}

TextSink emits textual log messages to an output stream. TextSink values must not be copied.

func (*TextSink) Drain Uses

func (ts *TextSink) Drain(kv KV) error

Drain encodes the specified key-value pairs to text, then writes them to the underlying io.Writer, followed by a newline.

Values are formatted using fmt.Sprint. If the textual representation of values contains whitespace or unprintable characters (in accordance with unicode.IsSpace and unicode.IsPrint), the values are quoted.

Drain makes a single Write call to the underlying io.Writer.

Package log imports 11 packages (graph) and is imported by 1 packages. Updated 2020-01-03. Refresh now. Tools for package owners.