user1340802
user1340802

Reputation: 1157

Python, logging and design

I have frequent doubts when I write python modules and classes, specifically:

Where/how should I put the main flag that controls logging and where/how to create the logger,

What is the Pythonic way of logging? How do you use the logging tools?

Upvotes: 2

Views: 324

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121904

You use logging everywhere, regardless.

In your modules, use:

import logging

log = logging.getLogger(__name__)

and log away. Use the different levels (log.info(), log.debug(), log.error(), etc.) depending on what type of message you are logging.

Your main entry point then configures the logging module output. That can include completely disabling any output, or directing debug information to a file, but logging errors to the console.

Your modules do not need to care, the logging module handles configuration of the output handlers and formatting as global state.

Upvotes: 5

Related Questions