package logfmt
import "github.com/go-logfmt/logfmt"
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 ¶
- Variables
- func MarshalKeyvals(keyvals ...interface{}) ([]byte, error)
- type Decoder
- func NewDecoder(r io.Reader) *Decoder
- func NewDecoderSize(r io.Reader, size int) *Decoder
- func (dec *Decoder) Err() error
- func (dec *Decoder) Key() []byte
- func (dec *Decoder) ScanKeyval() bool
- func (dec *Decoder) ScanRecord() bool
- func (dec *Decoder) Value() []byte
- type Encoder
- func NewEncoder(w io.Writer) *Encoder
- func (enc *Encoder) EncodeKeyval(key, value interface{}) error
- func (enc *Encoder) EncodeKeyvals(keyvals ...interface{}) error
- func (enc *Encoder) EndRecord() error
- func (enc *Encoder) Reset()
- type MarshalerError
- type SyntaxError
Examples ¶
Variables ¶
ErrInvalidKey is returned by Marshal functions and Encoder methods if, after dropping invalid runes, a key is empty.
ErrNilKey is returned by Marshal functions and Encoder methods if a key is a nil interface or pointer value.
ErrUnsupportedKeyType is returned by Encoder methods if a key has an unsupported 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.
Code:play
Output: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())
}
}
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 ¶
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the logfmt records requested.
func NewDecoderSize ¶
NewDecoderSize returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the logfmt records requested. The size argument specifies the size of the initial buffer that the Decoder will use to read records from r. If a log line is longer than the size argument, the Decoder will return a bufio.ErrTooLong error.
func (*Decoder) Err ¶
Err returns the first non-EOF error that was encountered by the Scanner.
func (*Decoder) Key ¶
Key returns the most recent key found by a call to ScanKeyval. The returned slice may point to internal buffers and is only valid until the next call to ScanRecord. It does no allocation.
func (*Decoder) ScanKeyval ¶
ScanKeyval advances the Decoder to the next key/value pair of the current record, which can then be retrieved with the Key and Value methods. It returns false when decoding stops, either by reaching the end of the current record or an error.
func (*Decoder) ScanRecord ¶
ScanRecord advances the Decoder to the next record, which can then be parsed with the ScanKeyval method. It returns false when decoding stops, either by reaching the end of the input or an error. After ScanRecord returns false, the Err method will return any error that occurred during decoding, except that if it was io.EOF, Err will return nil.
func (*Decoder) Value ¶
Value returns the most recent value found by a call to ScanKeyval. The returned slice may point to internal buffers and is only valid until the next call to ScanRecord. It does no allocation when the value has no escape sequences.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes logfmt data to an output stream.
Code:play
Output:Example¶
package main
import (
"errors"
"os"
"time"
"github.com/go-logfmt/logfmt"
)
func main() {
check := func(err error) {
if err != nil {
panic(err)
}
}
e := logfmt.NewEncoder(os.Stdout)
check(e.EncodeKeyval("id", 1))
check(e.EncodeKeyval("dur", time.Second+time.Millisecond))
check(e.EndRecord())
check(e.EncodeKeyval("id", 1))
check(e.EncodeKeyval("path", "/path/to/file"))
check(e.EncodeKeyval("err", errors.New("file not found")))
check(e.EndRecord())
}
id=1 dur=1.001s
id=1 path=/path/to/file err="file not found"
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func (*Encoder) EncodeKeyval ¶
EncodeKeyval writes the logfmt encoding of key and value to the stream. A single space is written before the second and subsequent keys in a record. Nothing is written if a non-nil error is returned.
func (*Encoder) EncodeKeyvals ¶
EncodeKeyvals writes the logfmt encoding of keyvals to the stream. Keyvals is a variadic sequence of alternating keys and values. Keys of unsupported type are skipped along with their corresponding value. Values of unsupported type or that cause a MarshalerError are replaced by their error but do not cause EncodeKeyvals to return an error. If a non-nil error is returned some key/value pairs may not have be written.
func (*Encoder) EndRecord ¶
EndRecord writes a newline character to the stream and resets the encoder to the beginning of a new record.
func (*Encoder) Reset ¶
func (enc *Encoder) Reset()
Reset resets the encoder to the beginning of a new record.
type MarshalerError ¶
MarshalerError represents an error encountered while marshaling a value.
func (*MarshalerError) Error ¶
func (e *MarshalerError) Error() string
type SyntaxError ¶
A SyntaxError represents a syntax error in the logfmt input stream.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
Source Files ¶
decode.go doc.go encode.go jsonstring.go
- Version
- v0.6.0 (latest)
- Published
- Jan 31, 2023
- Platform
- js/wasm
- Imports
- 13 packages
- Last checked
- now –
Tools for package owners.