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 ¶
- Variables
- func Log(args ...interface{})
- type Context
- type Error
- type List
- func (l List) From(ctx context.Context) Context
- func (l List) Keyvals() []interface{}
- func (l List) Log(args ...interface{})
- func (l List) MarshalText() (text []byte, err error)
- func (l List) NewError(text string) Error
- func (l List) String() string
- func (l *List) UnmarshalText(text []byte) error
- func (l List) With(keyvals ...interface{}) List
- func (l List) Wrap(err error, text ...string) Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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"
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
type List ¶
type List []interface{}
List is a slice of alternating keys and values.
func Parse ¶ added in v0.8.0
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
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 ¶
MarshalText implements the TextMarshaler interface.
func (List) NewError ¶ added in v0.8.0
NewError returns an error with the given message and a list of key/value pairs copied from the list.
func (List) 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
UnmarshalText implements the TextUnmarshaler interface.
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. |