Package logfmt
Overview ▹
Index ▹
Variables
ErrInvalidKey is returned by Marshal functions and Encoder methods if a key contains an invalid character.
var ErrInvalidKey = errors.New("invalid key")
ErrNilKey is returned by Marshal functions and Encoder methods if a key is a nil interface or pointer value.
var ErrNilKey = errors.New("nil key")
ErrUnsupportedKeyType is returned by Encoder methods if a key has an unsupported type.
var ErrUnsupportedKeyType = errors.New("unsupported key type")
ErrUnsupportedValueType is returned by Encoder methods if a value has an unsupported type.
var ErrUnsupportedValueType = errors.New("unsupported value type")
func MarshalKeyvals ¶
func MarshalKeyvals(keyvals ...interface{}) ([]byte, error)
MarshalKeyvals returns the logfmt encoding of keyvals, a variadic sequence of alternating keys and values.
type Decoder ¶
A Decoder reads and decodes logfmt records from an input stream.
type Decoder struct {
// contains filtered or unexported fields
}
▹ Example
func NewDecoder ¶
func NewDecoder(r io.Reader) *Decoder
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 (*Decoder) Err ¶
func (dec *Decoder) Err() error
Err returns the first non-EOF error that was encountered by the Scanner.
func (*Decoder) Key ¶
func (dec *Decoder) Key() []byte
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 ¶
func (dec *Decoder) ScanKeyval() bool
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 ¶
func (dec *Decoder) ScanRecord() bool
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 ¶
func (dec *Decoder) Value() []byte
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 ¶
An Encoder writes logfmt data to an output stream.
type Encoder struct {
// contains filtered or unexported fields
}
▹ Example
func NewEncoder ¶
func NewEncoder(w io.Writer) *Encoder
NewEncoder returns a new encoder that writes to w.
func (*Encoder) EncodeKeyval ¶
func (enc *Encoder) EncodeKeyval(key, value interface{}) error
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 ¶
func (enc *Encoder) EncodeKeyvals(keyvals ...interface{}) error
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 ¶
func (enc *Encoder) EndRecord() error
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.
type MarshalerError struct { Type reflect.Type Err error }
func (*MarshalerError) Error ¶
func (e *MarshalerError) Error() string
type SyntaxError ¶
A SyntaxError represents a syntax error in the logfmt input stream.
type SyntaxError struct { Msg string Line int Pos int }
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string