Loaders¶
Predefined Loaders¶
Bro/Zeek log loader.
-
class
zlogging.loader.
JSONParser
(model=None)¶ Bases:
zlogging.loader.BaseParser
JSON log parser.
- Parameters
model (
Model
class, optional) – Field declrations forJSONParser
, as in JSON logs the field typing information are omitted by the Bro/Zeek logging framework.- Variables
model (
Model
class, optional) – Field declrations forJSONParser
, as in JSON logs the field typing information are omitted by the Bro/Zeek logging framework.- Warns
JSONParserWarning – If
model
is not specified.
-
property
format
¶ str: Log file format.
-
parse_file
(file)¶ Parse log file.
-
parse_line
(line, lineno=0)¶ Parse log line as one-line record.
- Parameters
line (bytes) – A simple line of log.
lineno (Optional[int]) – Line number of current line.
- Returns
The parsed log as a plain
dict
.- Raises
JSONParserError – If failed to serialise the
line
from JSON.- Return type
Dict[str, Any]
-
class
zlogging.loader.
ASCIIParser
(type_hook=None, enum_namespaces=None, bare=False)¶ Bases:
zlogging.loader.BaseParser
ASCII log parser.
- Parameters
- Variables
__type__ (
dict
mappingstr
andBaseType
class) – Bro/Zeek type parser hooks.enum_namespaces (
List[str]
) – Namespaces to be loaded.bare (bool) – If
True
, do not loadzeek
namespace by default.
-
property
format
¶ str: Log file format.
-
parse_file
(file)¶ Parse log file.
- Parameters
file (_io.BufferedReader) – Log file object opened in binary mode.
- Returns
- The parsed log as a
Model
per line.
- Return type
- Warns
ASCIIParserWarning – If the ASCII log file exited with error, see
ASCIIInfo.exit_with_error
for more information.
-
parse_line
(line, lineno=0, separator=b'\t', parser=None)¶ Parse log line as one-line record.
- Parameters
line (bytes) – A simple line of log.
lineno (Optional[int]) – Line number of current line.
separator (Optional[bytes]) – Data separator.
parser (
List
ofBaseType
, required) – Field data type parsers.
- Returns
The parsed log as a plain
dict
.- Raises
ASCIIPaserError – If
parser
is not provided; or failed to serialiseline
as ASCII.- Return type
Dict[str, Any]
-
zlogging.loader.
parse_json
(filename, parser=None, model=None, *args, **kwargs)¶ Parse JSON log file.
- Parameters
filename (os.PathLike) – Log file name.
parser (
JSONParser
, optional) – Parser class.model (
Model
class, optional) – Field declarations forJSONParser
, as in JSON logs the field typing information are omitted by the Bro/Zeek logging framework.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed JSON log data.
- Return type
-
zlogging.loader.
load_json
(file, parser=None, model=None, *args, **kwargs)¶ Parse JSON log file.
- Parameters
file (_io.BufferedReader) – Log file object opened in binary mode.
parser (
JSONParser
, optional) – Parser class.model (
Model
class, optional) – Field declarations forJSONParser
, as in JSON logs the field typing information are omitted by the Bro/Zeek logging framework.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed JSON log data.
- Return type
-
zlogging.loader.
loads_json
(data, parser=None, model=None, *args, **kwargs)¶ Parse JSON log string.
- Parameters
data (AnyStr) – Log string as binary or encoded string.
parser (
JSONParser
, optional) – Parser class.model (
Model
class, optional) – Field declarations forJSONParser
, as in JSON logs the field typing information are omitted by the Bro/Zeek logging framework.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed JSON log data.
- Return type
-
zlogging.loader.
parse_ascii
(filename, parser=None, type_hook=None, enum_namespaces=None, bare=False, *args, **kwargs)¶ Parse ASCII log file.
- Parameters
filename (os.PathLike) – Log file name.
parser (
ASCIIParser
, optional) – Parser class.type_hook (
dict
mappingstr
andBaseType
class, optional) – Bro/Zeek type parser hooks. User may customise subclasses ofBaseType
to modify parsing behaviours.enum_namespaces (
List[str]
, optional) – Namespaces to be loaded.bare (
bool
, optional) – IfTrue
, do not loadzeek
namespace by default.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed ASCII log data.
- Return type
-
zlogging.loader.
load_ascii
(file, parser=None, type_hook=None, enum_namespaces=None, bare=False, *args, **kwargs)¶ Parse ASCII log file.
- Parameters
file (_io.BufferedReader) – Log file object opened in binary mode.
parser (
ASCIIParser
, optional) – Parser class.type_hook (
dict
mappingstr
andBaseType
class, optional) – Bro/Zeek type parser hooks. User may customise subclasses ofBaseType
to modify parsing behaviours.enum_namespaces (
List[str]
, optional) – Namespaces to be loaded.bare (
bool
, optional) – IfTrue
, do not loadzeek
namespace by default.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed ASCII log data.
- Return type
-
zlogging.loader.
loads_ascii
(data, parser=None, type_hook=None, enum_namespaces=None, bare=False, *args, **kwargs)¶ Parse ASCII log string.
- Parameters
data (AnyStr) – Log string as binary or encoded string.
parser (
ASCIIParser
, optional) – Parser class.type_hook (
dict
mappingstr
andBaseType
class, optional) – Bro/Zeek type parser hooks. User may customise subclasses ofBaseType
to modify parsing behaviours.enum_namespaces (
List[str]
, optional) – Namespaces to be loaded.bare (
bool
, optional) – IfTrue
, do not loadzeek
namespace by default.*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
The parsed ASCII log data.
- Return type
-
zlogging.loader.
parse
(filename, *args, **kwargs)¶ Parse Bro/Zeek log file.
- Parameters
filename (os.PathLike) – Log file name.
*args – See
parse_json()
andparse_ascii()
for more information.**kwargs – See
parse_json()
andparse_ascii()
for more information.
- Returns
The parsed JSON log data.
- Raises
ParserError – If the format of the log file is unknown.
- Return type
-
zlogging.loader.
load
(file, *args, **kwargs)¶ Parse Bro/Zeek log file.
- Parameters
file (_io.BufferedReader) – Log file object opened in binary mode.
*args – See
load_json()
andload_ascii()
for more information.**kwargs – See
load_json()
andload_ascii()
for more information.
- Returns
The parsed JSON log data.
- Raises
ParserError – If the format of the log file is unknown.
- Return type
-
zlogging.loader.
loads
(data, *args, **kwargs)¶ Parse Bro/Zeek log string.
- Parameters
data (AnyStr) – Log string as binary or encoded string.
*args – See
loads_json()
andloads_ascii()
for more information.**kwargs – See
loads_json()
andloads_ascii()
for more information.
- Returns
The parsed JSON log data.
- Raises
ParserError – If the format of the log file is unknown.
- Return type
Abstract Base Loaders¶
-
class
zlogging.loader.
BaseParser
¶ Bases:
object
Basic log parser.
-
abstract property
format
¶ str: Log file format.
-
parse
(filename)¶ Parse log file.
- Parameters
filename (os.PathLike) – Log file name.
- Returns
- Return type
-
abstract
parse_file
(file)¶ Parse log file.
-
abstract
parse_line
(line, lineno=0)¶ Parse log line as one-line record.
- Parameters
line (bytes) – A simple line of log.
lineno (Optional[int]) – Line number of current line.
- Returns
The parsed log as a plain
dict
.- Return type
Dict[str, Any]
-
load
(file)¶ Parse log file.
-
loads
(line, lineno=0)¶ Parse log line as one-line record.
- Parameters
line (bytes) – A simple line of log.
lineno (Optional[int]) – Line number of current line.
- Returns
The parsed log as a plain
dict
.- Return type
Dict[str, Any]
-
abstract property