dspjm
dspjm

Reputation: 5840

Python: How to override default logging handler?

Suppose I have got a logger like:

logger = logging.getLogger(__name__)

Then I add a file handler to it. I want all logs go to the file and all info and above log to be printed on screen.

I know I should set the level of the file handler to logging.INFO.

However, if I use logger.setLevel(logging.INFO), then the debug logs won't go to the file. If I use logger.setLevel(logging.DEBUG), then all debug logs will be printed on screen.

How to solve this problem?

Upvotes: 1

Views: 4716

Answers (1)

Bharel
Bharel

Reputation: 26900

You should use logger.setLevel(logging.DEBUG) on the logger and handler.setLevel(logging.INFO) on the screen handler. That way the logger gets all messages, the file handler gets all messages but the screen or stream handler gets only INFO or higher.

Upvotes: 2

Related Questions