4.9. WBEM operation logging¶
New in pywbem 0.11 and redesigned in pywbem 0.12. Finalized in pywbem 0.13.
4.9.1. Pywbem logging overview¶
The pywbem package implements selected logging using the Python
This section describes logging for use of the pywbem package as a WBEM client.
Section Logging in the listener describes logging for use of the pywbem
package as a WBEM listener.
Pywbem logging is used to record information passing between the pywbem client and WBEM servers but not as a general recorder for errors, state, etc. within pywbem. In effect it is a trace tool. Pywbem errors are generally passed to the pywbem API user as Python exceptions rather than being recorded in a log by a pywbem logger.
Pywbem logging uses two Python
Logger objects which are
termed pywbem loggers:
- API logger (Python logger name: ‘pywbem.api’) - Logs API calls and returns,
WBEMConnectionmethods that drive WBEM operations (see WBEM operations). This logs the API parameters and results, including CIM objects/exceptions. It also logs the creation of
WBEMConnectionobjects to capture connection information in order to determine the connection to which a particular log record belongs.
- HTTP logger (Python logger name: ‘pywbem.http’) - Logs HTTP requests and responses between the pywbem client and WBEM server. This logs the HTTP request data and response data including HTTP headers and CIM-XML payload.
Pywbem uses the
logging.DEBUG logging level for both loggers.
Pywbem adds a null handler to the logger named ‘pywbem’, in order to prevent the “No handlers could be found for logger …” warning. This follows best practices recommended in Configuring logging for a library and in several articles, for example in this article. Because this warning is no longer issued on Python 3.4 and higher, pywbem adds a null handler only on Python 2.7.
Because pywbem logs only at the
logging.DEBUG logging level, these
log events will not be printed by the Python root logger by default, and
therefore it is not necessary that pywbem attaches a null handler to any of its
There are two steps to setting up pywbem logging:
Configure Python logging parameters
Sets the Python logging parameters for a pywbem logger or its parent loggers, such as log handler, message format, and logging level.
This can be done with Python logging functions or with the functions described in Logging configuration functions.
Activate WBEM connection(s) for logging and set detail level
In order to save the cycles for capturing the possibly large amounts of data needed for creating the log records, pywbem logging is inactive by default. Logging can be activated for an existing WBEM connection, or for all subsequently created WBEM connections.
Because the pywbem loggers can generate large quantities of information, the user can control the quantity of information produced by each pywbem logger by setting a detail level for each logger when activating a WBEM connection for logging.
Activation and setting detail levels are pywbem features so it requires using the functions described in Logging configuration functions.
4.9.2. Logging configuration functions¶
The pywbem loggers may be configured and/or WBEM connections may be activated for logging through the following pywbem functions.
These functions are the only mechanism for setting the detail level of a pywbem logger and for activating WBEM connection(s) for logging.
configure_logger()- Configure the pywbem loggers and optionally activate WBEM connections for logging and setting a log detail level.
configure_loggers_from_string()- Configure the pywbem loggers and optionally activate WBEM connections for logging and setting a log detail level, from a log configuration string.
This is most useful in defining the pywbem logging from a command line tool such as pywbemcli so the complete logger configuration can be compressed into a single command line string.
4.9.3. Logging configuration examples¶
Examples for using
configure_logger() for configuring pywbem loggers and
for activating WBEM connections for logging:
Example: Configure the ‘pywbem.api’ logger with detail level ‘summary’ and output to stderr, and activate all subsequently created WBEM connections for logging:
configure_logger('api', log_dest='stderr', detail_level='summary', connection=True) # All of the following connections will log to stderr with summary output: conn1 = WBEMConnection(...) conn2 = WBEMConnection(...)
Example: Configure all pywbem loggers with the default detail level (‘all’) and output to a file, and activate a single existing WBEM connection for logging:
conn = WBEMConnection(...) configure_logger('all', log_dest='file', log_filname='my_logfile.log', connection=conn)
Examples for configuring the pywbem loggers using Python logging methods, and using the pywbem logging configuration functions only for setting the detail level and for activating WBEM connections for logging:
Example: Configure the pywbem parent logger (named ‘pywbem’) for logging to a rotating file, configure both pywbem loggers for detail level ‘summary’, and activate all subsequent WBEM connections for logging:
import logging from logging.handlers import RotatingFileHandler logger = logging.getLogger('pywbem') handler = RotatingFileHandler("my_log.log", maxBytes=2000, backupCount=10) handler.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) # configure without setting log_dest configure_logger('api', detail_level='summary', connection=True) # All of the following connections will log using the rotating file handler: conn1 = WBEMConnection(...) conn2 = WBEMConnection(...)
Examples for using
configure_loggers_from_string() for configuring the
pywbem loggers and for activating WBEM connections for logging:
Example: Configure the ‘pywbem.api’ logger with detail level ‘summary’, output to stderr, and activate all subsequently created WBEM connections for logging:
configure_loggers_from_string('api=stderr:summary', connection=True) # All of the following connections will log: conn1 = WBEMConnection(...) conn2 = WBEMConnection(...)
Example: Configure both pywbem loggers with the default detail level (‘all’) and output to the file ‘my_log.log’, and activate a single existing WBEM connection object (conn) for logging:
conn = WBEMConnection(...) configure_loggers_from_string('all=file', log_filname='my_log.log', connection=conn)
4.9.4. Log records¶
The following is an example of log output with detail level ‘summary’, where ‘1-32073’ is the connection identifier:
2018-03-17 11:39:09,877-pywbem.api.1-32073-Connection:1-32073 WBEMConnection(url='http://localhost', creds=None, default_namespace='root/cimv2') 2018-03-17 11:41:13,176-pywbem.api.1-32073-Request:1-32073 EnumerateClasses(ClassName=None, DeepInheritance=None, IncludeClassOrigin=None, IncludeQualifiers=None, LocalOnly=None, namespace=None) 2018-03-17 11:41:13,635-pywbem.api.1-32073-Return:1-32073 EnumerateClasses(list of CIMClass; count=103)
The keywords in each log record (‘Connection’, ‘Request’, ‘Return’ or ‘Exception’) identify whether the log record is connection data, API/HTTP request data, API/HTTP response data, or API exception data.
The loggers that actually create the log records are children of the configured
pywbem loggers and are unique for each
Their logger names are created from the configured logger names by appending
the connection identifier (
Thus the names of the loggers that actually create the log records are of the