Giter VIP home page Giter VIP logo

kv's Introduction

kv GoDoc License Build Status Coverage Status GoReportCard Release

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.

kv's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

karlmutch

kv's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.