Skip to content

Commit ba1a178

Browse files
authored
Add CallsiteParameter.QUAL_MODULE (#812)
* Add CallsiteParameter.QUAL_MODULE Fixes #492 * Add PR# * Simplify
1 parent 3922dda commit ba1a178

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
3838
- `structlog.BytesLogger` now has a `name` attribute which allows you to use it with the `structlog.stdlib.add_logger_name()` processor *without* using the standard library integration.
3939
[#786](https://github.com/hynek/structlog/pull/786)
4040

41+
- `structlog.processors.CallsiteParameterAdder` now supports `CallsiteParameter.QUAL_MODULE` that adds the qualified import name of the module of the callsite, or `__main__` if the module is the entry point.
42+
This is only available for *structlog*-originated events since the standard library has no equivalent (except for the convention of setting the logger's name to `__name__`).
43+
[#812](https://github.com/hynek/structlog/pull/812)
44+
4145

4246
### Changed
4347

‎src/structlog/processors.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ class CallsiteParameter(enum.Enum):
731731
732732
.. versionadded:: 25.5.0
733733
`QUAL_NAME` parameter.
734+
735+
.. versionadded:: 26.1.0
736+
`QUAL_MODULE` parameter.
734737
"""
735738

736739
#: The full path to the python source file of the callsite.
@@ -742,6 +745,8 @@ class CallsiteParameter(enum.Enum):
742745
#: of `logging.LogRecord` objects and will be the basename, without
743746
#: extension, of the full path to the python source file of the callsite.
744747
MODULE = "module"
748+
#: The fully qualified import name of the module of the callsite.
749+
QUAL_MODULE = "qual_module"
745750
#: The name of the function that the callsite was in.
746751
FUNC_NAME = "func_name"
747752
#: The qualified name of the callsite (includes scope and class names).
@@ -779,6 +784,10 @@ def _get_callsite_qual_name(module: str, frame: FrameType) -> Any:
779784
return frame.f_code.co_qualname # will crash on Python <3.11
780785

781786

787+
def _get_callsite_qual_module(module: str, frame: FrameType) -> Any:
788+
return module
789+
790+
782791
def _get_callsite_lineno(module: str, frame: FrameType) -> Any:
783792
return frame.f_lineno
784793

@@ -854,6 +863,7 @@ class CallsiteParameterAdder:
854863
CallsiteParameter.PATHNAME: _get_callsite_pathname,
855864
CallsiteParameter.FILENAME: _get_callsite_filename,
856865
CallsiteParameter.MODULE: _get_callsite_module,
866+
CallsiteParameter.QUAL_MODULE: _get_callsite_qual_module,
857867
CallsiteParameter.FUNC_NAME: _get_callsite_func_name,
858868
CallsiteParameter.QUAL_NAME: _get_callsite_qual_name,
859869
CallsiteParameter.LINENO: _get_callsite_lineno,

‎tests/processors/test_processors.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,14 @@ class TestCallsiteParameterAdder:
304304
"process_name",
305305
}
306306

307-
# Exclude QUAL_NAME from the general set to keep parity with stdlib
308-
# LogRecord-derived parameters. QUAL_NAME is tested separately.
307+
# Exclude QUAL_NAME and QUAL_MODULE from the general set to keep parity with
308+
# stdlib LogRecord-derived parameters. QUAL_NAME and QUAL_MODULE are tested
309+
# separately.
309310
_all_parameters = {
310311
p
311312
for p in set(CallsiteParameter)
312-
if p is not CallsiteParameter.QUAL_NAME
313+
if p
314+
not in (CallsiteParameter.QUAL_NAME, CallsiteParameter.QUAL_MODULE)
313315
}
314316

315317
def test_all_parameters(self) -> None:
@@ -341,6 +343,18 @@ def test_qual_name_structlog(self) -> None:
341343
f"{self.__class__.__name__}.test_qual_name_structlog"
342344
)
343345

346+
def test_qual_module_structlog(self) -> None:
347+
"""
348+
QUAL_MODULE is added for structlog-originated events.
349+
"""
350+
processor = CallsiteParameterAdder(
351+
parameters={CallsiteParameter.QUAL_MODULE}
352+
)
353+
event_dict: EventDict = {"event": "msg"}
354+
actual = processor(None, None, event_dict)
355+
356+
assert "tests.processors.test_processors" == actual["qual_module"]
357+
344358
def test_qual_name_logging_origin_absent(self) -> None:
345359
"""
346360
QUAL_NAME is not sourced from stdlib LogRecord and remains absent

0 commit comments

Comments
 (0)