kv

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2019 License: MIT Imports: 11 Imported by: 40

README

kv GoDoc License Build Status Coverage Status GoReportCard

Package kv provides support for working with collections of key/value pairs.

Lists

The List type represents a sequence of alternating key/value pairs. Lists can render themselves as text in logfmt format, so if you like logging messages with key value pairs but are not ready to move away from the standard library log package you can use something like:

log.Println("this is a log message", kv.With(
    "key1", "value 1",
    "key2", 2,
))

// Output:
// this is a log message key1="value 1" key2=2

The output from the previous example can be easily read by humans, and easily parsed by machines. This makes it an excellent format for structured logging.

Errors

The Error type implements the builtin error interface and renders its error message as a free text message followed by key/value pairs:

cannot open file: permission denied file="/etc/passwd" user=alice

Errors are constructed with message text and key/value pairs.

// create an error
err := kv.NewError("permission denied").With("user", user)
log.Println(err)

// Output:
// permission denied user=alice

Errors can wrap other errors:

err = kv.Wrap(err, "cannot open file").With("file", filename)
log.Println(err)

// Output:
// cannot open file: permission denied file="/etc/passwd" user=alice

Context

Key/value pairs can be stored in the context:

ctx := context.Background()

// associate some key/value pairs with the context
ctx = kv.From(ctx).With("url", "/api/widgets", "method", "get")

// ... later on ...

log.Println("access denied", kv.From(ctx).With("file", filename))

// Output:
// access denied file="/etc/passwd" url="/api/widgets" method=get

Parse

One of the key points of structured logging is that logs are machine readable. The Parse function provides an easy way to read messages.

// read one line
line := []byte(`cannot open file: access denied file="/etc/passwd" user=alice`)

 text, list := kv.Parse(line)
 log.Println("text:", string(text))
 log.Println("list:", list)

// Output:
// text: cannot open file: access denied
// list: file="/etc/passwd" user=alice

Logging

The kvlog subdirectory contains a package that works well with the log package in the Go standard library. Its usage is as simple as:

func main() {
    kvlog.Attach() // attach to the standard logger
    log.Println("program started", kv.With("args", os.Args))
}

See the GoDoc for more details.

Documentation

Overview

Package kv provides support for working with collections of key/value pairs.

See https://github.com/jjeffery/kv for an overview of how to use this package, and why you might want to use it.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// LogOutput is the function called to log a message.
	// The default value calls the Output function for the
	// standard logger in the Go standard liberary.
	LogOutput = log.Output
)

Functions

func Log added in v0.8.1

func Log(args ...interface{})

Log is used to log a message. By default the message is logged using the standard logger in the Go "log" package.

Example
package main

import (
	"context"

	"github.com/jjeffery/kv"
)

func main() {
	kv.Log("message 1")
	kv.Log("message 2", kv.With("a", 1))

	kv := kv.With("p", "q")

	kv.Log("message 3")
	kv.Log("message 4", kv.With("a", 1, "b", 2))

	ctx := kv.From(context.Background()).With("c1", 100, "c2", 200)

	kv.Log(ctx, "message 5")
	kv.Log("message 6:", ctx, "can be in any order")
	kv.Log(ctx, "message 7", kv.With("a", 1, "b", 2))

}
Output:

message 1
message 2 a=1
message 3 p=q
message 4 p=q a=1 b=2
message 5 p=q c1=100 c2=200
message 6: can be in any order p=q c1=100 c2=200
message 7 p=q a=1 b=2 c1=100 c2=200

Types

type Context added in v0.8.0

type Context interface {
	context.Context

	// With returns a new context based on the existing context,
	// but with with the key/value pairs attached.
	With(keyvals ...interface{}) context.Context

	// NewError returns a new error with the message text and
	// the key/value pairs from the context attached.
	NewError(text string) Error

	// Wrap returns an error that wraps the existing error with
	// the optional message text, and the key/value pairs from
	// the context attached.
	Wrap(err error, text ...string) Error
}

Context implements the context.Context interface, and can create a new context with key/value pairs attached to it.

Example
package main

import (
	"context"
	"fmt"

	"github.com/jjeffery/kv"
)

func main() {
	// start with a context
	ctx := context.Background()

	// attach some key/value pairs
	ctx = kv.From(ctx).With("method", "get", "url", "/api/widgets")

	// ... later on log a message ...

	fmt.Println("permission denied", kv.From(ctx).With("user", "alice"))

}
Output:

permission denied user=alice method=get url="/api/widgets"

func From added in v0.8.0

func From(ctx context.Context) Context

From creates a new context based on ctx. See the Context example.

type Error added in v0.8.0

type Error interface {
	error

	// With returns a new error based on this error
	// with the key/value pairs attached.
	With(keyvals ...interface{}) Error
}

Error implements the builtin error interface, and provides an additional method for attaching key/value pairs.

Example
package main

import (
	"fmt"

	"github.com/jjeffery/kv"
)

func main() {
	// setup the user
	user := "alice"

	// ... later on ...

	// create an error and log it
	err := kv.NewError("permission denied").With("user", user)
	fmt.Println(err)

	// alternatively, wrap an existing error
	err = kv.Wrap(err, "cannot open file").With("file", "/etc/passwd")
	fmt.Println(err)

}
Output:

permission denied user=alice
cannot open file: permission denied file="/etc/passwd" user=alice

func NewError added in v0.8.0

func NewError(text string) Error

NewError returns an error that formats as the given text.

func Wrap added in v0.8.0

func Wrap(err error, text ...string) Error

Wrap returns an error that wraps err, optionally annotating with the message text.

type List

type List []interface{}

List is a slice of alternating keys and values.

func Parse added in v0.8.0

func Parse(input []byte) (text []byte, list List)

Parse parses the input and reports the message text, and the list of key/value pairs.

The text slice, if non-nil, points to the same backing array as input.

func With added in v0.8.0

func With(keyvals ...interface{}) List

With returns a list populated with keyvals as the key/value pairs.

func (List) From added in v0.8.0

func (l List) From(ctx context.Context) Context

From returns a new context with key/value pairs copied both from the list and the context.

func (List) Keyvals

func (l List) Keyvals() []interface{}

Keyvals returns the list cast as []interface{}.

func (List) Log added in v0.8.1

func (l List) Log(args ...interface{})

Log is used to log a message. By default the message is logged using the standard logger in the Go "log" package.

func (List) MarshalText

func (l List) MarshalText() (text []byte, err error)

MarshalText implements the TextMarshaler interface.

func (List) NewError added in v0.8.0

func (l List) NewError(text string) Error

NewError returns an error with the given message and a list of key/value pairs copied from the list.

func (List) String

func (l List) String() string

String returns a string representation of the key/value pairs in logfmt format: "key1=value1 key2=value2 ...".

func (*List) UnmarshalText added in v0.8.0

func (l *List) UnmarshalText(text []byte) error

UnmarshalText implements the TextUnmarshaler interface.

func (List) With added in v0.8.0

func (l List) With(keyvals ...interface{}) List

With returns a new list with keyvals appended. The original list (l) is not modified.

func (List) Wrap added in v0.8.0

func (l List) Wrap(err error, text ...string) Error

Wrap wraps the error with the key/value pairs copied from the list, and the optional text.

Directories

Path Synopsis
internal
logfmt
Package logfmt provides utilities for writing key/value pairs in logfmt format.
Package logfmt provides utilities for writing key/value pairs in logfmt format.
parse
Package parse provides a parser for messages with key/value pairs.
Package parse provides a parser for messages with key/value pairs.
pool
Package pool provides a buffer pool.
Package pool provides a buffer pool.
terminal
Package terminal provides support functions for dealing with terminals.
Package terminal provides support functions for dealing with terminals.
Package kvlog handles messages printed by the `log` package in the Go standard library.
Package kvlog handles messages printed by the `log` package in the Go standard library.

Jump to

Keyboard shortcuts

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