-
Notifications
You must be signed in to change notification settings - Fork 0
Python API
sat edited this page Jun 26, 2026
·
1 revision
The public surface of log2seq. This page is the reference; for how the pieces fit together see Building a Parser.
| Name | Kind | Summary |
|---|---|---|
LogParser |
class | the parser; binds header rules to a statement parser |
init_parser |
function | build the default parser, or override one stage |
KEY_TIMESTAMP |
str | result key 'timestamp'
|
KEY_STATEMENT |
str | result key 'message'
|
KEY_WORDS |
str | result key 'words'
|
KEY_SYMBOLS |
str | result key 'symbols'
|
ParserDefinitionError |
exception | raised while building a parser |
LogParseFailure |
exception | raised while parsing a line |
__version__ |
str | e.g. '0.4.0'
|
-
header_parsers— aHeaderParseror a list of them (tried in order, first match wins). -
statement_parser— aStatementParser. -
ignore_failure— ifTrue,process_linereturnsNoneinstead of raising on an unparsable line.
Methods:
-
process_line(line, verbose=False) -> dict | None— run both stages. -
process_header(line, verbose=False) -> dict— header stage only. -
process_statement(message) -> (words, symbols)— statement stage only.
With no arguments, identical to preset.default(). Pass one argument to keep the
default for the other stage — e.g. init_parser(header_parsers=[my_hp]) reuses
the default statement parser.
process_line returns a plain dict:
- Always present:
'timestamp','message','words','symbols'(theKEY_*constants above).len(symbols) == len(words) + 1. - Header fields are named by the rule: an item named
hostadds a'host'key; there is no fixedhostconstant. Timestamp-component items are folded into'timestamp'. - An optional item that did not match is omitted — the key is absent, not
None. Test withkey in result.
-
ParserDefinitionError— a parser was built incorrectly (e.g. a rule with noStatement, or a malformed item pattern). Raised at construction time. -
LogParseFailure— a line matched no header rule (or a stage failed) andignore_failureisFalse. Carries the offending line in its message.
import log2seq
try:
d = log2seq.init_parser().process_line("not a log line")
except log2seq.LogParseFailure as e:
...-
log2seq.header—HeaderParserand theItemclasses (Date,Time,MonthAbbreviation,Digit,Hostname,String,UserItem,ItemGroup,Statement,UnixTime,TimeZone, …). Defines__all__, sofrom log2seq.header import *brings only these public names — not the stdlib modules it uses. See Header Rules. -
log2seq.statement—StatementParserand theActionclasses (Split,Fix,FixIP,FixParenthesis,FixPartial,Remove,RemovePartial,ConditionalSplit). Also defines__all__. See Statement Rules. -
log2seq.preset— ready-made parsers:default(),init-equivalentdefault_header_parsers()/default_statement_parser(), andapache_errorlog_parser(). See Presets.
- Building a Parser — the narrative version of this page.
- Architecture Overview — internals, for contributors.