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 thelog()
method of a logger, aLogMessage
will be created and passed on to thewrite_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
- 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 methodlog(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 methodwrite_message(self, msg)
that accepts one argument: an instance of theLogMessage
class. If you need to do anything once in the beginning or the and, you can override thesetup()
and/orteardown()
methods respectively.Warning
The
log()
method should not be overwritten.