logfmt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2016 License: MIT Imports: 13 Imported by: 0

README

GoDoc Go Report Card TravisCI Coverage Status

logfmt

Package logfmt implements utilities to marshal and unmarshal data in the logfmt format. It provides an API similar to encoding/json and encoding/xml.

The logfmt format was first documented by Brandur Leach in this article. The format has not been formally standardized. The most authoritative public specification to date has been the documentation of a Go Language package written by Blake Mizerany and Keith Rarick.

Goals

This project attempts to conform as closely as possible to the prior art, while also removing ambiguity where necessary to provide well behaved encoder and decoder implementations.

Non-goals

This project does not attempt to formally standardize the logfmt format. In the event that logfmt is standardized this project would take conforming to the standard as a goal.

Versioning

Package logfmt publishes releases via semver compatible Git tags prefixed with a single 'v'.

Documentation

Overview

Package logfmt implements utilities to marshal and unmarshal data in the logfmt format. The logfmt format records key/value pairs in a way that balances readability for humans and simplicity of computer parsing. It is most commonly used as a more human friendly alternative to JSON for structured logging.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidKey = errors.New("invalid key")

ErrInvalidKey is returned by Marshal functions and Encoder methods if a key contains an invalid character.

View Source
var ErrNilKey = errors.New("nil key")

ErrNilKey is returned by Marshal functions and Encoder methods if a key is a nil interface or pointer value.

View Source
var ErrUnsupportedKeyType = errors.New("unsupported key type")

ErrUnsupportedKeyType is returned by Encoder methods if a key has an unsupported type.

View Source
var ErrUnsupportedValueType = errors.New("unsupported value type")

ErrUnsupportedValueType is returned by Encoder methods if a value has an unsupported type.

Functions

func MarshalKeyvals

func MarshalKeyvals(keyvals ...interface{}) ([]byte, error)

MarshalKeyvals returns the logfmt encoding of keyvals, a variadic sequence of alternating keys and values.

Types

type Decoder

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

A Decoder reads and decodes logfmt records from an input stream.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/go-logfmt/logfmt"
)

func main() {
	in := `
id=1 dur=1.001s
id=1 path=/path/to/file err="file not found"
`

	d := logfmt.NewDecoder(strings.NewReader(in))
	for d.ScanRecord() {
		for d.ScanKeyval() {
			fmt.Printf("k: %s v: %s\n", d.Key(), d.Value())
		}
		fmt.Println()
	}
	if d.Err() != nil {
		panic(d.Err())
	}

}
Output:
k: id v: 1
k: dur v: 1.001s

k: id v: 1
k: path v: /path/to/file
k: err v: file not found

func NewDecoder

func NewDecoder(r io.Reader) *