Skip to content

Commit aaa03d4

Browse files
committed
fix: _log() Multiple Values for 'stacklevel'.
The error `_log() got multiple values for keyword argument 'stacklevel'` may occur when you pass both a positional argument and a keyword argument with the same name (`stacklevel`) to the `Logger._log()` function. This fix checks if the `stacklevel` is a key of the `kwargs` dictionary before calling the `Logger._log()` function, considering it if present. Signed-off-by: Paulo Vital <[email protected]>
1 parent fb165ce commit aaa03d4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/instana/instrumentation/logging.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ def log_with_instana(wrapped, instance, argv, kwargs):
1616
# argv[0] = level
1717
# argv[1] = message
1818
# argv[2] = args for message
19-
if sys.version_info >= (3, 13):
20-
stacklevel = 3
19+
20+
# We take into consideration if `stacklevel` is already present in `kwargs`.
21+
# This prevents the error `_log() got multiple values for keyword argument 'stacklevel'`
22+
if "stacklevel" in kwargs.keys():
23+
stacklevel = kwargs.pop("stacklevel")
2124
else:
2225
stacklevel = 2
26+
if sys.version_info >= (3, 13):
27+
stacklevel = 3
28+
2329
try:
24-
tracer, parent_span, _ = get_tracer_tuple()
25-
2630
# Only needed if we're tracing and serious log
2731
if tracing_is_off() or argv[0] < logging.WARN:
2832
return wrapped(*argv, **kwargs, stacklevel=stacklevel)
2933

34+
tracer, parent_span, _ = get_tracer_tuple()
35+
3036
msg = str(argv[1])
3137
args = argv[2]
3238
if args and len(args) == 1 and isinstance(args[0], Mapping) and args[0]:

tests/clients/test_logging.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ def log_custom_warning():
9696
self.assertEqual(self.caplog.records[0].funcName, "log_custom_warning")
9797

9898
self.logger.removeHandler(handler)
99+
100+
def test_stacklevel_as_kwarg(self):
101+
with tracer.start_active_span("test"):
102+
self.logger.warning("foo %s", "bar", stacklevel=2)
103+
104+
spans = self.recorder.queued_spans()
105+
self.assertEqual(2, len(spans))
106+
self.assertEqual(2, spans[0].k)
107+
108+
self.assertEqual("foo bar", spans[0].data["log"].get("message"))

0 commit comments

Comments
 (0)