149

When I log an event with logging.info, it doesn't appear in the Python terminal.

import logging
logging.info('I am info')  # no output

In contrast, events logged with logging.warn do appear in the terminal.

import logging
logging.warn('I am warning')  # outputs "I am warning"

Is there a environment level change I can to make logging.info print to the console? I want to avoid making changes in each Python file.

9 Answers 9

217

The root logger always defaults to WARNING level. Try calling

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

and you should be fine.

13
  • 1
    No, you only need to call it once. The logger is built as a hierarchy with and all logging boils down to a root logger. By not specifying any argument to getLogger(), it is returning you the root logger. As long as you don't modify the other loggers you only need to modify the root logger.
    – Ztyx
    Sep 24, 2012 at 21:14
  • 25
    Do you know why logging.basicConfig(level=logging.INFO) doesn't work? I can't see it clearly on the documentation. Dec 24, 2012 at 12:30
  • 19
    This Does not work on Python 3.5: Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux >>> import logging >>> rootLog = logging.getLogger() >>> rootLog.setLevel(logging.INFO) >>> rootLog.info('all the kings horses') >>> rootLog.warning('all the kings men') all the kings men
    – Jeff K
    Apr 18, 2019 at 18:55
  • 2
    FWIW, this post explains why the root logger and logging module level method calls are best avoided: electricmonk.nl/log/2017/08/06/… The tl;dr is: "Don’t log directly against the root logger. That means: no logging.basicConfig() and no usage of module-level loggers such as logging.warning(), as they have unintended side-effects."
    – Owen
    May 13, 2019 at 17:28
  • 17
    @jeffk, same with me 3.6.8 doesn't print info messages even when setLevel is set to logging.INFO Feb 14, 2020 at 20:20
49

Like @ztyx said that default logger level is WARNING. You have to set it to a lower level

You can do it by using logging.basicConfig and setting logger level:

logging.basicConfig(level=logging.DEBUG)
2
  • 12
    I'm wondering why basicConfig() doesn't work for me, although logging.getLogger().setLevel() suitably work? Jun 20, 2019 at 14:33
  • 5
    @ShayanAmani - According to the documentation, "This function does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True."
    – vy32
    Mar 18, 2021 at 0:59
30

The above solutions didn't work for me, but the code here did:

# set up logging to file
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='/temp/myapp.log',
                    filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

(I omitted parts of the code for the sake of readability)

3
  • 1
    This is the only thing that worked for me. I had a line with logging.error("Connection timed out!") and even with the level=logging.DEBUG in the basicConfig(), it wouldn't print to console. Adding the handler did, thanks so much!!
    – BruceWayne
    Mar 21, 2019 at 17:52
  • Keep in mind that the handler you are using is playing a role. If, for example, your code had the NullHandler, nothing would be printed regardless of the logging lever.
    – George
    Apr 15, 2019 at 1:32
  • Same here - if i omit the level argument in basicConfig or set it above INFO then the console logger will never log anything. If i leave out basicConfig then i can call setLevel on a logger all day (and i can see the level change by calling getEffectiveLevel) but it will never log anything below WARNING level. I'm not actually sure that that isnt the correct behaviour but its not what I was expecting.
    – Hal
    Apr 23, 2020 at 22:59
17

This will work

import logging

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

logger.info('its working')
6

In more recent versions of Python 3 (tested with Python 3.8), console logging requires creating a console handler to correctly show info messages.

The following example is modified from the Configuring Logging example in the Python documentation:

import logging

# create logger
logger = logging.getLogger('__name__')
level = logging.INFO
logger.setLevel(level)

# ----> console info messages require these lines <----
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(level)

# add ch to logger
logger.addHandler(ch)
# -----------------------------------------------------

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

Running the above code generates the following output:

info message
warn message
error message
critical message

Here is this same code without the console handler.

import logging

# create logger
logger = logging.getLogger('__name__')
level = logging.INFO
logger.setLevel(level)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

Without the console handler, the output does not include the info message:

warn message
error message
critical message

I do not understand why this is the case as it seems unnecessary.

1
  • 1
    weird, I can confirm streamhandler is necessary. Thanks for saving my time
    – marcadian
    Mar 24 at 0:00
4

What's the minimum required code for a working module-level logger? I did an experiment (with python version 3.8.6).

The take-away is:

  • logging.basicConfig() is needed (however, specifying level=... is NOT needed)
  • it's necessary to configure the root logger: logging.getLogger().setLevel(...)

So, a minimum working example is:

The library/module code does NOT need to configure the logger:

# library/module code: lib.py
import logging
LOGGER = logging.getLogger('x.y.z')

def some_function():
    LOGGER.info("hi")

The application code need to configure the logger with 2 lines at minimum:

# Application Code
import logging, lib

logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)  # configure root logger

main()  # code that will trigger lib

Here's the experiment:

In [1]: import logging

In [2]: lg = logging.getLogger('x.y.z')

In [3]: lg.info(1)

In [4]: logging.basicConfig()

In [5]: lg.info(1)

In [6]: logging.basicConfig(level=logging.INFO)

In [7]: lg.info(1)

In [8]: logging.basicConfig()

In [9]: logging.getLogger().setLevel(logging.INFO)

In [10]: lg.info(1)
INFO:x.y.z:1
1
  • Why isn't logging.basicConfig(level=logging.INFO) enough? Is this a bug?
    – moi
    Feb 19 at 9:14
0

For those using absl.logging, the equivalent command is

from absl import logging
logging.set_verbosity(logging.INFO)
0

If you are using Django to power your server, you just simply need to change the log level in your settings.py file as such:

"handlers": {
                "console": {
--                  "level": "WARNING",
++                  "level": "INFO",
                    "class": "logging.StreamHandler",
                    "formatter": "stackdriver",
                }
            },

More examples in the documentation here: https://docs.djangoproject.com/en/4.0/topics/logging/#configuring-logging-1

0

logging.info() will use the root logger for logging.

According to official doc, if you do not set an explicit handler for the logger, a special handler called lastResort will be used. See the code here. By default the logging level of lastResort (it is stream handler) is 30. we can change its level to output info message.

# setting both the logger and handler's level will work as expected.
logger.setLevel(logging.DEBUG)
logging.lastResort.setLevel(logging.DEBUG)

However, this is like a hack and never a encouraged action.

Using logging.basicConfig()

If we want to do logging real quick, we can use the method logging.basicConfig.

logging.basicConfig(level=logging.DEBUG)

This will create the logger. The logger.level will be the level we set here. A stream handler will be also created, with level NOTSET. Without a level param, the default level for root logger is WARNING.

reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.