Logging

Logger implementations

The default implementations of loggers are listed here, these are the classes that should be used by users.

Note

See Logger for more info on how to use its implementations.

class ConsoleLogger[source]

A logger that writes messages to the console

The messages are written with the following syntax:

[  <Type>  ] <Date> <Time> - <Message>
class TextLogger(fname, overwrite=False)[source]

A logger that writes messages to a text file

The messages are written with the following syntax:

[  <Type>  ] <Date> <Time> - <Message>
Parameters
  • fname (str) – The name of the output file

  • overwrite (bool, optional) – Whether to overwrite the contents of the output file if it already exists or to append the messages to the end of the file (default: False)

Logger Interface

These are the classes that should be used when creating a custom logger.

class LogMessage(msg, msg_type)[source]

Container class for log messages

This class contains all the necessary information about a log message and is used to send this information from the base class Logger to an actual implementation of a logger. More specifically, when a user calls the log() method of a logger, a LogMessage will be created and passed on to the write_message() implementation.

Parameters
  • msg (str) – The message

  • msg_type (LogType, optional) – The message type that indicates its severity, this should be one of the options given by the enumeration LogType (default: LogType.INFO)

msg

The text message given to the log() function

Type

str

type

The type of the log message

Type

LogType

time

The time of the log message

Type

datetime

timestring

A formatted string with the date and time of the message

Type

str

class Logger[source]

Base class that provides an interface for different loggers

To be able to log messages using a logger of your choice, e.g. TextLogger, create an instance of that logger. You can then use the method log(msg, msg_type) to actually write a log message to a file.

To implement a new logger, create a class that inherits from Logger and at least has a method write_message(self, msg) that accepts one argument: an instance of the LogMessage class. If you need to do anything once in the beginning or the and, you can override the setup() and/or teardown() methods respectively.

Warning

The log() method should not be overwritten.

log(msg, msg_type=LogType.INFO)[source]

Write a log message

Parameters
  • msg (str) – The message

  • msg_type (LogType, optional) – The message type that indicates its severity, this should be one of the options given by the enumeration LogType (default: LogType.INFO)