loggers
Functions:
|
Initialize a logger |
Exceptions:
Error parsing a logfile |
Classes:
|
|
Single entry in a log |
|
Representation of a logfile in memory |
Data:
//github.com/r1chardj0n3s/parse>`_ |
|
Additional parsing patterns for logged messages |
- init_logger(instance=None, module_name=None, class_name=None, object_name=None) Logger[source]
Initialize a logger
Loggers are created such that…
There is one logger per module (eg. all gpio objects will log to hardware.gpio)
If the passed object has a
nameattribute, that name will be prefixed to its log messages in the fileThe loglevel for the file handler and the stdout is determined by
prefs.get('LOGLEVEL'), and if none is providedWARNINGis used by defaultlogs are rotated according to
prefs.get('LOGSIZE')(in bytes) andprefs.get('LOGNUM')(number of backups ofprefs.get('LOGSIZE')to cycle through)
Logs are stored in
prefs.get('LOGDIR'), and are formatted like:"%(asctime)s - %(name)s - %(levelname)s : %(message)s"
Loggers can be initialized either by passing an object to the first
instanceargument, or by specifying any ofmodule_name,class_name, orobject_name(at least one must be specified) which are combined with periods likemodule.class_name.object_name- Parameters:
instance – The object that we are creating a logger for! if None, at least one of
module, class_name, or object_namemust be passedmodule_name (None, str) – If no
instancepassed, the module name to create a logger forclass_name (None, str) – If no
instancepassed, the class name to create a logger forobject_name (None, str) – If no
instancepassed, the object name/id to create a logger for
- Returns:
logging.logger
- exception ParseError[source]
Bases:
RuntimeErrorError parsing a logfile
- class Log_Format(format: str, example: str, conversions: Dict[str, Callable] | None = None)[source]
Bases:
objectAttributes:
A format string parseable by
parseAn example string (that allows for testing)
A dictionary matching keys in the
formatstring to callables for post-parsing coercionMethods:
parse(log_entry)
- LOG_FORMATS = ( Log_Format(format='{timestamp:Timestamp} - {name} - {level} : {message}', example="2022-03-07 16:56:48,954 - networking.node.Net_Node._T - DEBUG : RECEIVED: ID: _testpi_9879; TO: T; SENDER: _testpi; KEY: DATA; FLAGS: {'NOREPEAT': True}; VALUE: {'trial_num': 1197, 'timestamp': '2022-03-01T23:52:16.995387', 'frequency': 45255.0, 'amplitude': 0.1, 'ramp': 5.0, 'pilot': 'testpi', 'subject': '0895'}", conversions={'Timestamp': <function _convert_asc_timestamp at 0x7f81f665d040>}), Log_Format(format='[{timestamp:Timestamp}] {level} [{name}]: {message}', example='[2022-03-09 16:13:43,224] INFO [networking.node]: parent, module-level logger created: networking.node', conversions={'Timestamp': <function _convert_asc_timestamp at 0x7f81f665d040>}))
//github.com/r1chardj0n3s/parse>`_
- Type:
Possible formats of logging messages (to allow change over versions) as a `parse string <https
- MESSAGE_FORMATS = { 'node_msg_recv': '{action}: ID: {message_id}; TO: {to}; SENDER: {sender}; ' 'KEY: {key}; FLAGS: {flags}; VALUE: {value}', 'node_msg_sent': '{action} - ID: {message_id}; TO: {to}; SENDER: {sender}; ' 'KEY: {key}; FLAGS: {flags}; VALUE: {value}'}
Additional parsing patterns for logged messages
node_msg: Logging messages fromnetworking.node.Net_Node
- pydantic model LogEntry[source]
Bases:
Autopilot_TypeSingle entry in a log
Show JSON schema
{ "title": "LogEntry", "description": "Single entry in a log", "type": "object", "properties": { "timestamp": { "title": "Timestamp", "type": "string", "format": "date-time" }, "name": { "title": "Name", "type": "string" }, "level": { "title": "Level", "enum": [ "DEBUG", "INFO", "WARNING", "ERROR" ], "type": "string" }, "message": { "title": "Message", "anyOf": [ { "type": "string" }, { "type": "object" } ] } }, "required": [ "timestamp", "name", "level", "message" ] }
- Fields:
- parse_message(format: List[str])[source]
Parse the message using a format string specified as a key in the
MESSAGE_FORMATSdictionary (or a format string itself)replaces the
messageattribute.If parsing unsuccessful, no exception is raised because there are often messages that are not parseable in the logs!
- Parameters:
format (typing.List[str]) – List of format strings to try!
Returns:
- classmethod from_string(entry: str, parse_message: List[str] | None = None) LogEntry[source]
Create a LogEntry by parsing a string.
Try to parse using any of the possible .LOG_FORMATS, raising a
ParseErrorif none are successful- Parameters:
entry (str) – single line of a logging file
parse_message (Optional[str]) – Parse messages with the
MESSAGE_FORMATSkey or format string
- Returns:
- Raises:
.ParseError –
- pydantic model Log[source]
Bases:
Autopilot_TypeRepresentation of a logfile in memory
Show JSON schema
{ "title": "Log", "description": "Representation of a logfile in memory", "type": "object", "properties": { "entries": { "title": "Entries", "type": "array", "items": { "$ref": "#/definitions/LogEntry" } } }, "required": [ "entries" ], "definitions": { "LogEntry": { "title": "LogEntry", "description": "Single entry in a log", "type": "object", "properties": { "timestamp": { "title": "Timestamp", "type": "string", "format": "date-time" }, "name": { "title": "Name", "type": "string" }, "level": { "title": "Level", "enum": [ "DEBUG", "INFO", "WARNING", "ERROR" ], "type": "string" }, "message": { "title": "Message", "anyOf": [ { "type": "string" }, { "type": "object" } ] } }, "required": [ "timestamp", "name", "level", "message" ] } } }
- classmethod from_logfile(file: Path | str, include_backups: bool = True, parse_messages: List[str] | None = None)[source]
Load a logfile (and maybe its backups) from a logfile location
- Parameters:
file (
pathlib.Path, str) – If string, converted to Path. If relative (and relative file is not found), then attempts to find relative toprefs.LOGDIRinclude_backups (bool) – if
True(default), try and load all of the backup logfiles (that have .1, .2, etc appended)parse_messages (Optional[str]) – Parse messages with the
MESSAGE_FORMATSkey or format string
- Returns: