loggers
– Set up the logging system¶
Set up the VHC logging system.
Two file handler are created:
log.txt
: logs everything with level aboveLOWER_LEVEL
v_results.txt
: logs everything with level aboveERORR_LEVEL
. This is intended only to notify about failed tests
The two handler are then added to the logger and a
logging.LoggerAdapter
instance is created passing it an empty
VCheck
instance, to avoid having to pass it to every logger
call. The logging.LoggerAdapter
instance is then saved using
similar name conventions as for logging.Logger
The correct way to use it is to use the logger is:
- initialise the logger with
set_logger()
- update the
LoggerAdapter
for before calling every driver function withupdate_logger()
- where needed retrieve the “adapted” logger with
getLogger()
and log the message and with a normal logger. - alternatively you can get the standard logger and then log a message adding
the keyword option
extra
Examples¶
>>> import os
>>> from libvhc import loggers
>>> from libvhc import VCheck
>>> # set up the logger
>>> # in the example as in the code, the name of the logger is set
>>> # to the ``path``.
>>> path = "tests/data/doctest"
>>> loggers.set_logger(path, os.path.join(path, loggers.LOG_FILE.format(0)),
... os.path.join(path, loggers.RESULT_FILE.format(0)))
>>> recipe = "flat"
>>> checks = ['flat:check1', 'flat:check2']
>>> # do the actual work
>>> log = loggers.getLogger(path)
>>> def check(vcheck, path, args):
... # do the check
... log.info("all fine")
... log.error("are you sure?")
>>> with loggers.logger_context(path):
... for c in checks:
... vcheck = VCheck(recipe, c)
... loggers.update_logger(vcheck, path)
... check(vcheck, path, [])
-
libvhc.loggers.
LOWER_LEVEL
= 20¶ lower logging level: INFO
-
libvhc.loggers.
ERORR_LEVEL
= 40¶ error logging level: ERROR
-
libvhc.loggers.
JSON_KEY
= '+++VHCJSON+++ '¶ Log message key to filter o
-
libvhc.loggers.
LOG_FILE
= 'log_{}.txt'¶ Template for the log file name
-
libvhc.loggers.
RESULT_FILE
= 'v_results_{}.txt'¶ Template for the result file name
-
libvhc.loggers.
JSON_FILE
= 'v_json_{}.txt'¶ Template for the json file name
-
class
libvhc.loggers.
JSONFormatter
(print_json, *args, **kwargs)[source]¶ -
format
(self, record)[source]¶ Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
-
-
libvhc.loggers.
_name
(name)[source]¶ Return the name to use (privately) with the module (private) dictionaries.
Use the return string of this function instead of the name itself in parts that have interaction with child processes, to allow for future modifications.
Parameters: - name : string
Returns: - string
name
-
libvhc.loggers.
set_logger
(name, logfile, result_file, json_file, vcheck=VCheck('', ''))[source]¶ Create and add the logging handlers to the logger
name
. Save aLoggerAdapter
created with the givenVCheck
instance.Parameters: - name : string
name of the logger
- logfile, result_file : string
name of the log and the result files
- json_file : string
name of the optional json output file. If empty, do not create a json log file
- vcheck : instance of
VCheck
, optional store the recipe name and the check currently executing
-
libvhc.loggers.
update_logger
(vcheck, name)[source]¶ Update the
VCheck
entry of theLoggerAdapter
namedname
Parameters: - vcheck : instance of
VCheck
store the recipe name and the check currently executing
- name : string
name of the logger
Raises: - KeyError
if the logger called
name
has not been created usingset_logger()
- vcheck : instance of
-
libvhc.loggers.
getLogger
(name, adapter=True)[source]¶ Returns the logger named
name
.Parameters: - name : string
name of the logger
- adapter : bool, optional
get the adapted logger, instead of the original one
Returns: logging.LoggerAdapter
orlogging.Logger
instance
Raises: - KeyError
if the
name
logger is not found
-
libvhc.loggers.
get_listener
(name)[source]¶ Return the queue listener
Parameters: - name : string
name of the logger
Returns: SetupQueueListener
can be used once in a
with
Raises: - KeyError
if the
name
listener is not found
-
libvhc.loggers.
get_queue
(name)[source]¶ Returns the queue saved under the
name
.If it does not exist, create it first
Parameters: - path : string
path of the two above files
Returns: - queue-like instance
-
libvhc.loggers.
logger_context
(name)[source]¶ Yield the listener and the queue associated with
name
from within awith
statementParameters: - name : string
name of the listener and the queue
Yields: - queue-like instance
SetupQueueListener
-
libvhc.loggers.
init_logger_adapt
(name, queue_, level=20, extras=VCheck('', ''))[source]¶ Initialise the logger and save the adapter passing
extras
to itParameters: - name : string
name of the logger
- queue_ : queue-like instance
- level : int, optional
minimum logging level
- extras : dict-like instance
extras for the
logging.LoggerAdapter
-
libvhc.loggers.
clear_logger
(name)[source]¶ Remove the queue, the listener and reset the logger to pristine situation.
Parameters: - name : string
name of the queue and logger
-
libvhc.loggers.
_init_logger
(name, queue_, level=20)[source]¶ Initialise the logger, using the
QueueHandler
Parameters: - name : string
name of the logger
- queue_ : queue-like instance
- level : int, optional
minimum logging level
Returns: - logger :
logging.Logger
-
libvhc.loggers.
_make_file_handler
(fname, level, fmt, filter_=None)[source]¶ Create the file handler
Parameters: - fname : string
name of the file where the logging happens
- level : int
level of the handler
- fmt : string or
logging.Formatter
formatting of the handler
- filter_ :
logging.Filter
filter to apply to the handler
Returns: - fhandler : instance of
logging.FileHandler
configured handler