How do you log properly in Python?

Python errors and files · Freshers and experienced

Model answer

Use the logging module instead of print: configure a level and format once, create loggers per module with logging.getLogger(__name__), and use levels (debug, info, warning, error). Use logger.exception inside except blocks to include the traceback. Never log secrets.

Learn this topic

Handling Errors in Python: try, except, finally and raise in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions