Bdfy
Bdfy

Reputation: 24709

How to print info level to stdout?

How to print info level to stdout ?

import logging
import sys
root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

root.info('test1')
root.error('test')

result only error level:

2013-11-14 11:44:19,675 - root - ERROR - test

Why ?

Upvotes: 0

Views: 49

Answers (2)

Whoami
Whoami

Reputation: 14418

To give additional Input,

It is something like, when we pass a message to the logger, First check happens if the logger permits the logging level, If allowed it is passed to the Handler1... Handler2, and applies the same logic.

Upvotes: 0

falsetru
falsetru

Reputation: 369334

The code set the level of the handler, not logger.

Replace following line:

ch.setLevel(logging.INFO)

with:

root.setLevel(logging.INFO)

Upvotes: 4

Related Questions