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

推荐订阅源

Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
罗磊的独立博客
雷峰网
雷峰网
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
B
Blog RSS Feed
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
小众软件
小众软件
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
S
SegmentFault 最新的问题

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
How to Use Unified Logging Config Across Your Project
2020-04-24 · via jdhao's digital space

When we are working on a project, we often need to log some message for easier debugging. How do we configure logging once and use it across the project?

Set up logging for the project#

Suppose we have the following file in our project:

main.py
module1.py
module2.py

main.py is the entry point of our project and use functions or classes from module1 and module2.

The best practice is to set up logging in main.py like this:

import module1
import module2
import logging
# other imports

logging_level = logging.DEBUG
main_logger = logging.getLogger()
main_logger.setLevel(logging_level)

# Set up a stream handler to log to the console
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging_level)
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
stream_handler.setFormatter(formatter)

# Add handler to logger
main_logger.addHandler(stream_handler)

# other codes

In the above code, we define a root logger main_logger via logging.getLogger() (notice that no name is provided to this method), and set up the handlers and logging level properly.

In module1 and module2, we only need to define a logger like this:

import logging

logger = logging.getLogger(__name__)

# then use logger.info() or logger.debug() in your code.

In other modules, if we define the logger using logging.getLogger(__name__), it will automatically use the settings from the root logger. This ensures that we only need to configure logging once in our project.

Changing logging level to disable some logging message#

There is also another benefit in using a unified config: we can easily control the logging message verbosity by changing the logging level. For example, in the above example, we set logging level in main.py to logging.DEBUG, so all logger.debug() and logger.info() message will be printed to the console. If we do not want to see debug messages anymore, we can set logging level to logging.INFO, which will disable any debug message across the whole project.

Disable logging message from other packages#

When we import 3rd party packages in our own project and set proper logging level for the project, if a logging message in 3rd party packages is above that level, the message will also be printed. Since we usually only care about logging messages in our project, we can disable logging message in 3rd party packages by setting their logger level higher, or by disabling logging entirely for 3rd party packages. Here is the code:

import logging

# suppose that the logger level of our module is logging.DEBUG, we set
# logger level of other module higher than logging.DEBUG
for m in ("urllib3", "foo", "bar"):
    logging.getLogger(m).setLevel(logging.CRITICAL)
    # To disable it entirely, use the following.
    # logging.getLogger(m).disabled = True

References#