惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
B
Blog RSS Feed
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
D
Docker
Jina AI
Jina AI
IT之家
IT之家
人人都是产品经理
人人都是产品经理
L
LangChain Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
What Happened When Logging without Handlers in Python
2022-05-27 · via jdhao's digital space

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

import logging


logger = logging.getLogger()

logger.info("some message")

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

print(logger.level)

# output is 30 (logging.WARNING)

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

logger.setLevel(logging.DEBUG)

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.
logger.setLevel(logging.DEBUG)
logging.lastResort.setLevel(logging.DEBUG)

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.

logging.basicConfig(level=logging.DEBUG)

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:

print(logger.handlers)
# you will see: [<StreamHandler <stderr> (NOTSET)>]

reference#