Skip to content
Sections
LanguageRUEN

v2.1API

createParser()

The parser factory: all options, defaults and the methods of the returned object.

ts
function createParser(options: ParserOptions): Parser

Creates a parser. Patterns and queries compile at creation, so configuration errors show up immediately, not on the millionth line.

Options#

OptionTypeDefaultDescription
formatstringnoneOne of the built-in formats
patternstringnoneA pattern instead of format; exactly one of the two is required
timezonestringlocal'UTC' or an offset such as '+03:00' for lines without a zone
yearnumbercurrentYear for the syslog format
onError'skip' | 'collect' | 'throw''skip'What to do with an unparsed line
maxErrorsnumber1000Cap for parser.errors
maxLineLengthnumber65536Maximum line length in characters
typedbooleantrueConvert logfmt and JSON values to numbers and booleans

Methods#

parser.parseLine(line)#

Parses one line. Returns a Record, or null if the line is not recognised and onError is not 'throw'.

js
const rec = parser.parseLine('level=warn msg="disk 91%"');
rec.level; // 'warn'

parser.parse(text)#

Parses a multi-line text and returns an array of records. Meant for small strings in tests; use a stream for files.

parser.stream(readable, opts?)#

An async iterator of records, see streaming.

parser.query(text)#

Compiles a query into a function (rec) => boolean.

Properties#

  • parser.errors: collected ParseError objects (only with onError: 'collect');
  • parser.stats: { lines, parsed, skipped, aborted }, updated as reading progresses.

One parser per source

A parser keeps the state of a multi-line record, so do not share it between two concurrent streams. Create one per source.

Updated: