parser

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package parser implements a logfmt parser.

logfmt is described here: https://www.brandur.org/logfmt, however I am not aware of an actual specification. Thus, the logfmt dialect understood by this package is as follows:

A log consists of 0 or more records, which are delimited by newline characters.

Each record consists of 0 or more key-value pairs, which are whitespace-delimited.

Each key-value pair consists of a mandatory key, and optionally a value (absent values are implied to be false).

Keys and values may be quoted strings, in which case they may contain any character (except unescaped quotes or newlines). Both single (') and double (") quotes are acceptable for quoted strings. Within quoted strings, the following escape characters are recognized:

\a, \b, \\, \t, \n, \f, \r, \v, \', \", \xhh

\xhh is interpreted to mean the hex value hh, cast to a Go rune. Exactly two hex digits are required.

Unquoted values contain any non-whitespace, non-newline character except for '=', keys have the same constraint, but also cannot contain ;, ' or " whether quoted or unquoted. This is for security, because key names are used directly as SQL column names.

Values which are non-quoted will be parsed into one of the following types, in descending order of precedence:

* integer * float * bool * string

Lines which contain only whitespace are skipped, and do not imply an empty record.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSyntaxError

func IsSyntaxError(e error) bool

IsSyntaxError returns True if the given error is a SyntaxError.

Types

type Parser

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

Parser is used to store state while processing input data.

func NewParser

func NewParser(stream io.Reader) (*Parser, error)

NewParser instantiates a new logfmt parser on the specified stream.

func NewParserFromString

func NewParserFromString(text string) (*Parser, error)

NewParserFromString instantiates a new logfmt parser on the specified text, given as a string.

func (*Parser) NextRecord

func (p *Parser) NextRecord() (map[string]interface{}, error)

NextRecord parses the next full record in the stream, returning it. If there are no further records, it will return an io.EOF. If other errors are encountered, they will be propagated as well.

In the event that a syntax error is found, then the parser will be advanced to the beginning of the next line (or EOF). Thus parsing may be able to continue if the error is ignored, which might be desirable for certain use cases.

type SyntaxError

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

SyntaxError indicates a syntax error has been detected. To check if an error is a syntax error, use the IsSyntaxError() method.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL