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 ¶
var ErrInvalidKey = errors.New("invalid key")
ErrInvalidKey is returned by Marshal functions and Encoder methods if a key contains an invalid character.
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.
var ErrUnsupportedKeyType = errors.New("unsupported key type")
ErrUnsupportedKeyType is returned by Encoder methods if a key has an unsupported type.
var ErrUnsupportedValueType = errors.New("unsupported value type")
ErrUnsupportedValueType is returned by Encoder methods if a value has an unsupported type.
Functions ¶
func MarshalKeyvals ¶
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