loggers

Functions:

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

Initialize a logger

Exceptions:

ParseError

Error parsing a logfile

Classes:

Log_Format(format, example[, conversions])

LogEntry

Single entry in a log

Log

Representation of a logfile in memory

Data:

LOG_FORMATS

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

MESSAGE_FORMATS

Additional parsing patterns for logged messages

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 0x7fa14fce6a60>}),     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 0x7fa14fce6a60>}))

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

pydantic model LogEntry[source]

Bases: autopilot.root.Autopilot_Type

Single 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
field timestamp: datetime.datetime [Required]
field name: str [Required]
field level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR'] [Required]
field message: Union[str, dict] [Required]
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.utils.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

pydantic model Log[source]

Bases: autopilot.root.Autopilot_Type

Representation 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"
         ]
      }
   }
}

Fields
field entries: List[autopilot.utils.loggers.LogEntry] [Required]
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