Skip to main content
  1. Posts/

What Happened When Logging without Handlers in Python

··220 words·2 mins·
Python Logging
Table of Contents

I saw a post from stackoverflow asking why his logger does not work as expect? The code is like:

import logging

logger = logging.getLogger()"some message")

There is no message coming out. Okay, let’s check the level of this logger:


# output is 30 (logging.WARNING)

Then we can just change its level to DEBUG, it will certain work, right?


However, still no output! W*F, what happened?

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.

However, this is like a hack and never an encouraged solution.

Using logging.basicConfig()

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


This will create the logger. The logger.level will be the level we set here. Without a level param, the default level for root logger is WARNING.

A stream handler will be also created, with level NOTSET. This can be verified by printing the logger’s handlers:

# you will see: [<StreamHandler <stderr> (NOTSET)>]



Configure Python logging with dictConfig
··503 words·3 mins
Python Logging
Why Are Some Logging Messages Missing in Python?
··487 words·3 mins
Python Logging
Duplicate Logging Messages in Python
··265 words·2 mins
Python Logging