Open Source Python | Lesson 051 | Intermediate | Estimated study time: 30–40 minutes | Python 3.8 or later for logging stacklevel.
A monitoring application may report thousands of events from a shared logging helper. If every record identifies the helper instead of the application function that requested the message, an engineer loses a valuable debugging clue. Python’s standard logging library offers caller attribution through the stacklevel argument, allowing reusable wrappers to preserve useful source locations.
Prerequisites and sequence
Review OSPython.050: efficient logging, OSPython.049: testing logs, OSPython.046: structured logging and OSPython.035: exception logging. A local Python interpreter and basic familiarity with functions, keyword arguments and unittest are sufficient.
Learning objectives
- Explain how logging records obtain source filename, line number and function name.
- Use stacklevel to attribute an event to the caller of a helper, including nested wrappers.
- Preserve lazy message formatting and distinguish stacklevel from stack_info and exc_info.
- Write repeatable tests for caller attribution using captured LogRecord objects.
- Recognize privacy, provenance and deployment boundaries for source-location metadata.
1. From function call to LogRecord
A stack frame represents an active function invocation, including its execution location. When an application calls a logger method and the event passes the logger’s effective severity threshold, logging constructs a LogRecord. The standard Logger.findCaller mechanism locates the relevant calling frame while skipping internal logging frames. Record attributes include pathname (full source path), filename (base filename), lineno (source line number), funcName (function name) and name (logger name). The latter identifies the logger, not necessarily the source function.
Handlers receive eligible records and apply their own levels, filters and formatters. A formatter can display source attributes with %(filename)s:%(lineno)d and %(funcName)s. Altering a formatter changes presentation, not the original caller selection. Source metadata is created before a handler formats the message.
2. Single and nested wrappers
The default stacklevel of 1 identifies the direct application-side logging call. If a helper calls logger.info, the recorded location usually points to the helper. Supplying stacklevel=2 advances attribution to the helper’s caller. The logging methods gained the stacklevel keyword in Python 3.8.
A reusable wrapper can accept a caller-selected stacklevel and add one when forwarding it to logging. For two application wrappers, an effective stacklevel of 3 is needed to reach the function outside both wrappers. Increment once for each wrapper that deliberately hides its own frame; avoid hardcoding a large number because calling paths may have different depths.
- Direct logger.info call: stacklevel=1 identifies the direct call site.
- One wrapper: its forwarded stacklevel=2 identifies the wrapper’s caller.
- Two nested wrappers: the outer wrapper requests stacklevel=2 from the inner wrapper, which forwards stacklevel=3 to the logger.
3. Runnable lab: verify the caller, not just the message
Save the following source as ospython051_stacklevel.py in a temporary working directory. The example uses only the standard library, an isolated named logger and fictional device identifiers. No network access, package installation or elevated privileges are required.
For a copy-ready Python file with standard space indentation, open the tested source file on GitHub.
import logging
import unittest
logger = logging.getLogger(‘lesson.caller’)
def plain(message, *args):
logger.info(message, *args)
def event(message, *args, stacklevel=1):
logger.info(message, *args, stacklevel=stacklevel + 1)
def report(device):
event(‘Device %s ready’, device, stacklevel=2)
def check_device():
plain(‘Device %s ready’, ‘lab-01’)
event(‘Device %s ready’, ‘lab-01’)
report(‘lab-01’)
class CallerTest(unittest.TestCase):
def test_attribution(self):
with self.assertLogs(logger, level=’INFO’) as captured:
check_device()
self.assertEqual([r.funcName for r in captured.records],
[‘plain’, ‘check_device’, ‘check_device’])
self.assertEqual([r.getMessage() for r in captured.records],
[‘Device lab-01 ready’] * 3)
self.assertEqual(len({r.lineno for r in captured.records}), 3)
if __name__ == ‘__main__’:
unittest.main()
Run python ospython051_stacklevel.py (or use python3 if required by the environment). The test captures three records. The first comes from plain, because the helper does not adjust attribution. The second comes from check_device, because event advances one frame. The third also comes from check_device, because report requests an additional frame.
The three records carry the same rendered message but different source line numbers. The assertions inspect LogRecord attributes directly, avoiding dependence on a formatter or absolute path. The complete example was executed under CPython 3.13.5 and passed one unittest test containing three assertions. Other versions and interpreters were not executed.
4. Formatting cost and stack diagnostics
Caller attribution is separate from message formatting. The helper forwards the format string and arguments separately, preserving logging’s deferred interpolation. Passing an already evaluated f-string would still incur its construction cost before the logger decides whether to emit a record. Expensive argument expressions likewise execute before the call; use a severity guard when necessary, as taught in lesson 050.
stacklevel selects the source location attributed to the record. stack_info=True adds information about the active call stack, whereas exc_info=True attaches exception details, commonly inside an exception handler. Neither option is needed merely to move the caller attribution by one frame. Tracebacks and stack dumps can increase log volume and expose sensitive implementation information; request them deliberately.
5. Operational workflow and security boundaries
For a local verification, reproduce the wrapper call with synthetic identifiers, inspect funcName and lineno in captured records, and compare direct and nested invocation paths. Test each wrapper separately and include failure or early-return paths when they have different call structures. If a formatter displays source location, confirm that the fields match the captured record. Keep line-number tests independent of unrelated source edits where possible.
Full pathname values can disclose usernames, repository layouts and deployment directories. Prefer filename or a controlled relative source identifier for broadly accessible logs. Avoid real customer identifiers, tokens and secrets in test messages. Source attribution identifies a selected frame; it is not cryptographic evidence of who initiated an action. Use authenticated audit events, access controls, time synchronization and protected retention for security investigations.
No production logger configuration needs to change for the lab. Changes to production logging levels, formatters, retention or log destinations require service-owner approval, privacy review, change control, rollback to the previous configuration and verification of downstream ingestion. Escalate missing or misleading caller metadata when it interferes with incident response.
Glossary
- Stack frame: execution context of a function invocation.
- LogRecord: structured event containing message arguments and event metadata.
- Caller attribution: source location assigned to a log event.
- Wrapper: a helper that invokes another function on behalf of its caller.
- Deferred interpolation: retaining message and arguments separately until formatting is needed.
Practice exercises
- A. Remove the stacklevel keyword from event. Predict which function names the second and third records will report; rerun the test.
- B. Change report to call event with stacklevel=1. Predict the third record’s funcName and modify its expected value to validate the prediction.
- C. Explain why logger.info(‘Device %s ready’, device) differs from logger.info(f’Device {device} ready’) when INFO is disabled.
- D. Add an assertion that all captured records use the name lesson.caller. Explain why it does not identify the calling function.
- E. Propose a production format that provides useful source information without exposing absolute deployment paths.
Knowledge check
- What are the differences among name, funcName, filename and lineno?
- What effective stacklevel identifies the caller outside two application wrappers?
- Does stacklevel add an exception traceback?
- Why should tests inspect captured LogRecord objects?
- Why is caller attribution insufficient as security proof?
Answer key
A: Both records identify event; the original test fails. B: The third identifies report, because the inner wrapper advances only one frame. C: The format-string version defers interpolation; the f-string is evaluated before logging is called. D: Use self.assertTrue(all(r.name == ‘lesson.caller’ for r in captured.records)); the logger name remains constant across call sites. E: Use %(filename)s:%(lineno)d %(funcName)s %(levelname)s %(message)s with an approved redaction and retention policy.
Knowledge check: 1. name is the logger identity, funcName the attributed function, filename the base source filename, lineno the attributed line. 2. Three for the demonstrated wrapper arrangement. 3. No; exc_info supplies exception details. 4. Records expose the original metadata independently of output formatting. 5. Applications can select or alter source metadata, so trusted identity and protected audit infrastructure are separate requirements.
Primary references
- Python 3.12 logging reference: Logger methods, stacklevel, findCaller and LogRecord attributes.
- Python 3.12 unittest: assertLogs and captured records.
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below.


Leave a Reply