ChaimG
ChaimG

Reputation: 7514

How to use Python's built in logger for INFO messages

I am having difficulty with the built-in logger module. WARNING messages show up but not INFO messages. Like so:

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.info('Info')  # This line was inserted.
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.warning('Warning')
WARNING:root:Warning
>>> logging.info('Info')
>>>

Interestingly, after walking away from my desk for an hour or so and then starting a new session I got the following:

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.warning('Warning')
WARNING:root:Warning
>>> logging.info('Info')
INFO:root:Info
>>> 

It worked! The question is why did the first version not work.

Note: The question has been edited.

Upvotes: 1

Views: 521

Answers (2)

MattCorr
MattCorr

Reputation: 371

The default level that logging is set to is warning. Any messages below that level(such as info messages) do not display. To change that, use this code:

logging.getLogger().setLevel(logging.INFO)

Reference:

logging.info doesn't show up on console but warn and error do

Upvotes: -1

Guoliang
Guoliang

Reputation: 895

I'm confused with your output, it may worth to try again using just the logging code: e.g.

Python 2.7.10 (default, May 23 2015, 09:40:32)
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.warning('warning')
WARNING:root:warning
>>> logging.debug('debug')
DEBUG:root:debug
>>> logging.info('info')
INFO:root:info

you may refer to https://docs.python.org/2/library/logging.html to get more details.

Upvotes: 2

Related Questions