Logging
Logging integration between the Pact library and the standard logging module.
The Pact FFI and Pact plugins run outside the Python interpreter, so their log
output does not reach logging on its own. This module initialises the FFI
once, on the first use of Pact or Verifier,
and re-emits every event through logging:
- Events from the Pact library are emitted on the
pact.ffilogger, with the Rust module path appended and its::separators replaced by.(for examplepact.ffi.pact_matching.matchers). Each record carrieslog_idandtest_run_idas extra attributes.log_ididentifies the operation which emitted the event: the mock server ID while the server handles a request, orverify:<provider name>while verifying a provider.test_run_idis the ID set on the emitting thread withset_test_run_id. - Events from plugins are emitted on the
pact.pluginslogger, withplugin_instance_id,test_run_idandplugin_targetas extra attributes.
Nothing needs to be configured. Verbosity is controlled with logging:
The FFI forwards the library's events at the level set explicitly on the
pact.ffi logger, or at DEFAULT_LEVEL when it has none.
That level is fixed when the FFI is initialised, so set it before creating the
first Pact or Verifier. Every plugin event is forwarded, and the level of
the pact.plugins logger filters them like any other records. The Pact
library's TRACE level is exposed as TRACE, below logging.DEBUG.
Log records can be attributed to the test which caused them by setting a test
run ID with set_test_run_id. The pytest plugin in
pact.testing.pytest and the unittest mixin in
pact.testing.unittest do this automatically.
Importing this module, which importing Pact or Verifier does, replaces
pact_ffi.init, pact_ffi.init_with_log_level, pact_ffi.log_to_stderr,
pact_ffi.log_to_stdout and pact_ffi.log_to_file with functions which emit
a UserWarning and do nothing else. A logger applied through pact_ffi.init,
pact_ffi.init_with_log_level or pact_ffi.log_to_stderr before this module
was imported is kept: events are still forwarded to logging, at the more
restrictive of that sink's level and the forwarding level, and a warning is
emitted on the pact logger.
Attributes¶
DEFAULT_LEVEL = logging.INFO
module-attribute
¶
Level at which the Pact library forwards events when the pact.ffi logger has
no explicit level.
INFO is the default because the library's DEBUG output is dominated by
per-FFI-call tracing, per-request matching dumps and, when a plugin is
loaded, transport chatter from hyper, rustls and reqwest, none of
which helps once a test has already passed.
TRACE = 5
module-attribute
¶
Level of the Pact library's TRACE events, below logging.DEBUG.
The level is registered with the name TRACE, unless another library has
already named level 5 or registered TRACE as another level. verboselogs,
for example, names level 5 SPAM, and records at this level then show that
name.
logger = logging.getLogger('pact')
module-attribute
¶
Functions:¶
ensure_initialised() -> None
¶
Initialise the Pact library's logging once.
Applies a logger with no visible sink at the level returned by
_ffi_level, registers the callbacks which re-emit library and plugin
events through logging, and registers an atexit hook which
deregisters them again.
The forwarding level cannot be raised once the logger is applied. A later
call which finds a more verbose level set on the pact.ffi logger logs a
warning on the pact logger, once.
If a logger was already applied, through one of the pact_ffi.log_to_*
functions called before this module was imported, a warning is logged on the
pact logger and the callbacks are registered against that logger
instead.
Source code in src/pact/logging.py
forwarding_level() -> int
¶
logging level at which the Pact library forwards events.
Plugin events are not filtered by this level: the FFI hands every one of them to the plugin log callback.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The level set explicitly on the |
int
|
|
Source code in src/pact/logging.py
from_ffi_level(level: str) -> int
¶
Convert a Pact library level name to a logging level.
| PARAMETER | DESCRIPTION |
|---|---|
level
|
One of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The matching |
Source code in src/pact/logging.py
set_test_run_id(test_run_id: str | None) -> None
¶
Set the test run ID for the current thread.
The ID is reported as the test_run_id attribute on the records the Pact
library emits from the current thread. It is also attached to every
request sent to a plugin from the current thread, so the log entries the
plugin sends back while handling that request carry it too. Lines a
plugin writes to its stderr do not.
Records from the library's own threads do not carry this ID, and are
identified by their log_id instead. While the mock server handles a
request, log_id is the mock server ID and test_run_id is None.
While a provider is verified, log_id is verify:<provider name> and
most records carry an ID which the verifier generates for the run as
test_run_id.
| PARAMETER | DESCRIPTION |
|---|---|
test_run_id
|
The identifier to attach, such as pytest's
TYPE:
|
Source code in src/pact/logging.py
to_ffi_level(level: int) -> pact_ffi.LevelFilter
¶
Convert a logging level to the FFI level filter admitting it.
| PARAMETER | DESCRIPTION |
|---|---|
level
|
A
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LevelFilter
|
The |
LevelFilter
|
|