v2.1API
createParser()
The parser factory: all options, defaults and the methods of the returned object.
function createParser(options: ParserOptions): ParserCreates a parser. Patterns and queries compile at creation, so configuration errors show up immediately, not on the millionth line.
Options#
| Option | Type | Default | Description |
|---|---|---|---|
format | string | none | One of the built-in formats |
pattern | string | none | A pattern instead of format; exactly one of the two is required |
timezone | string | local | 'UTC' or an offset such as '+03:00' for lines without a zone |
year | number | current | Year for the syslog format |
onError | 'skip' | 'collect' | 'throw' | 'skip' | What to do with an unparsed line |
maxErrors | number | 1000 | Cap for parser.errors |
maxLineLength | number | 65536 | Maximum line length in characters |
typed | boolean | true | Convert 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'.
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: collectedParseErrorobjects (only withonError: '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.