loggers

Data:

_LOGGERS

List of instantiated loggers, used in init_logger() to return existing loggers without modification

LOG_FORMATS

//github.com/r1chardj0n3s/parse>`_

MESSAGE_FORMATS

Additional parsing patterns for logged messages

Functions:

init_logger([instance, module_name, ...])

Initialize a logger

Exceptions:

ParseError

Error parsing a logfile

Classes:

Log_Format(format, example[, conversions])

LogEntry(*, timestamp, name, level, message)

Single entry in a log

Log(*, entries)

Representation of a logfile in memory

_LOGGERS: list = [   'data.interfaces.tables',     'data.interfaces.tables.H5F_Group',     'data.models.subject',     'data.models.subject._Hash_Table',     'data.models.subject._History_Table',     'data.models.subject._Weight_Table']

List of instantiated loggers, used in init_logger() to return existing loggers without modification

init_logger(instance=None, module_name=None, class_name=None, object_name=None) logging.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 name attribute, that name will be prefixed to its log messages in the file

  • The loglevel for the file handler and the stdout is determined by prefs.get('LOGLEVEL'), and if none is provided WARNING is used by default

  • logs are rotated according to prefs.get('LOGSIZE') (in bytes) and prefs.get('LOGNUM') (number of backups of prefs.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 instance argument, or by specifying any of module_name , class_name , or object_name (at least one must be specified) which are combined with periods like module.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_name must be passed

  • module_name (None, str) – If no instance passed, the module name to create a logger for

  • class_name (None, str) – If no instance passed, the class name to create a logger for

  • object_name (None, str) – If no instance passed, the object name/id to create a logger for

Returns

logging.logger

exception ParseError[source]

Bases: RuntimeError

Error parsing a logfile

class Log_Format(format: str, example: str, conversions: Union[Dict[str, Callable], NoneType] = None)[source]

Bases: object

Attributes:

format

A format string parseable by parse

example

An example string (that allows for testing)

conversions

A dictionary matching keys in the format string to callables for post-parsing coercion

Methods:

parse(log_entry)

format: str

A format string parseable by parse

example: str

An example string (that allows for testing)

conversions: Optional[Dict[str, Callable]] = None

A dictionary matching keys in the format string to callables for post-parsing coercion

parse(log_entry: str) dict[source]
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 0x7f44429d8700>}),     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 0x7f44429d8700>}))

//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

class LogEntry(*, timestamp: datetime.datetime, name: str, level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR'], message: Union[str, dict])[source]

Bases: autopilot.root.Autopilot_Type

Single entry in a log

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Attributes:

timestamp

name

level

message

Methods:

parse_message(format)

Parse the message using a format string specified as a key in the MESSAGE_FORMATS dictionary (or a format string itself)

from_string(entry[, parse_message])

Create a LogEntry by parsing a string.

timestamp: datetime.datetime
name: str
level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR']
message: Union[str, dict]
parse_message(format: List[str])[source]

Parse the message using a format string specified as a key in the MESSAGE_FORMATS dictionary (or a format string itself)

replaces the message attribute.

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: Optional[List[str]] = None) autopilot.core.loggers.LogEntry[source]

Create a LogEntry by parsing a string.

Try to parse using any of the possible .LOG_FORMATS, raising a ParseError if none are successful

Parameters
  • entry (str) – single line of a logging file

  • parse_message (Optional[str]) – Parse messages with the MESSAGE_FORMATS key or format string

Returns

LogEntry

Raises

.ParseError

class Log(*, entries: List[autopilot.core.loggers.LogEntry])[source]

Bases: autopilot.root.Autopilot_Type

Representation of a logfile in memory

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Attributes:

entries

Methods:

from_logfile(file[, include_backups, ...])

Load a logfile (and maybe its backups) from a logfile location

entries: List[autopilot.core.loggers.LogEntry]
classmethod from_logfile(file: Union[pathlib.Path, str], include_backups: bool = True, parse_messages: Optional[List[str]] = 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 to prefs.LOGDIR

  • include_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_FORMATS key or format string

Returns

Log