index
module utilities.logger
Logger
logger.py
Simple logging code that write messages to a log file. It uses five levels:
Level 0: no messages
Level 1: errors
Level 2: errors and warnings
Level 3: errors and warnings and info
Level 4: errors and warnings and info and debugging
Initialize the logger, typically from the main script, as follows
import utilities.logger as logger
logger.initialize_logger(<filename>, <level>)
This will open for writing a file <filename>.html. The <level> argument is
optional, the default is to print info, errors and warnings (level 3). After
initialization, the logger can be used from any module by importing it and using
its methods:
from utilities import logger
logger.info(<string>)
logger.debug(<string>)
logger.warn(<string>)
logger.error(<string>)
In addition, there is one method that writes to the log no matter what the
level:
logger.write(<string>)
Finally, it provides two convenience method that print directly to the standard
output:
logger.out(*args)
logger.outnl()
The first one writes the string representation of all arguments to the standard
output and the second writes a white line. Printing to the standard output can
be turned on and off with
set_stdout_printing(Boolean)
class Logger
Inherits from: object
The Logger class, has no other function than to store the logging
level and the log file.
Public Functions
__init__(self, filename, level=2)
Set logging level and the log file.
module functions
debug(string)
Print a debugging message to the log file.
error(string)
Print an error to the log file, also print it to standard error.
info(string)
Print an info string to the log file.
initialize_logger(filename, level=2)
Initialize the logger on <filename>, default logging level is 2.
Only initialize if logger has not been initialized yet.
out(*args)
Method to write to standard output rather than the html file. Intended
for quick and dirty debugging that should not be left in the code. Using
this method makes it easier to later remove the debugging code (as opposed
to using print statements right in the text). Works well only for
one-liners.
outnl()
report(fh=None)
set_level(integer)
set_stdout_printing(Boolean)
When this function is called, the out and outnl methods will print or not
print to the standard output, depending on the boolean value.
warn(string)
Print a warning to the log file.
write(string)
Print a string to the log file, no matter what the logging
level. This will be printed as an INFO string