nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
8 of 8
1What does exception chaining (raise X from Y) accomplish, and how does it aid debugging?
2Your program is consuming increasing memory over time (a suspected memory leak) despite Python's garbage collector. How would you investigate this?
3What is the difference between except Exception and a bare except:? Why is the bare form discouraged?
4What is the purpose of the else and finally clauses in a try block?
5A production script fails intermittently with a RecursionError. How would you diagnose and fix it?
6What tools would you use to profile a slow Python function, and how do you interpret the output?
7How would you design a custom exception hierarchy for an application?
8How would you use Python's logging module effectively instead of print statements for debugging production code?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
08 / 08

How would you use Python's logging module effectively instead of print statements for debugging production code?

Difficulty: 5/10
Logging, Observability, Production Debugging

Use a configured logger with levels, handlers, and structured context instead of print

print writes to stdout and cannot be filtered, routed, or enriched. The logging module gives you a hierarchy of named loggers, level filtering, multiple handlers (console, file, syslog, JSON), and structured formatting that includes timestamp, level, logger name, and message. Use logger = logging.getLogger(name) so the logger name reflects module structure and can be configured per package. Configure handlers once at the entry point; library code should only create loggers and call methods. Use the lazy % formatting in log calls rather than f-strings so that string construction is skipped when the level is disabled. For production services, emit JSON to stdout and let the platform collect it, and include a correlation or request id via LoggerAdapter or a filter.

  1. 1

    Levels: DEBUG for development detail, INFO for lifecycle events, WARNING for recoverable anomalies, ERROR for failed operations, CRITICAL for process-threatening failures.

  2. 2

    Use logging.getLogger(name) in every module; never use the root logger directly in libraries.

  3. 3

    Call logging.basicConfig or dictConfig once at the entry point, not in libraries.

  4. 4

    Use logger.error('...', exc_info=True) or logger.exception(...) inside except to preserve the traceback.

  5. 5

    Prefer lazy logging: logger.info('user %s did %s', user_id, action) over f-strings, since the format is skipped when the level is off.

  6. 6

    Common mistake: calling logging.info at module import time before configuration, which uses the last-resort handler and prints unexpectedly.

  7. 7

    Common mistake: logging inside a hot loop without a level check, which can dominate runtime.

  8. 8

    Version note: dictConfig and structlog-style JSON logging are widely used in 3.2+. The default lastResort handler behavior has been stable for years.

Scenario Questions

0-2 years experience

  1. 1What is the difference between logging.info and print?
  2. 2How do you get the module name into your log messages automatically?

2-5 years experience

  1. 1Your library prints messages when imported. How do you fix it to use logging correctly?
  2. 2You need DEBUG logs in staging but only INFO in production. How do you configure this without code changes?

5-8 years experience

  1. 1You need structured JSON logs with a request id across many modules. How do you set this up?
  2. 2Your service produces huge volumes of logs and log cost is high. How do you reduce volume without losing debugging value?

8+ years experience

  1. 1Design an observability strategy combining logging, metrics, and tracing that minimizes overlap and keeps costs bounded.
  2. 2Explain how logging interacts with asyncio, thread pools, and multiprocessing, and how to keep correlation ids consistent.

Follow-up Questions

  • Why is logger.info('x %s', y) preferred over an f-string in a hot path?
  • How do you attach a request id to every log line in a web service?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.