How to use logging NullHandler in python 2.6

三世轮回 提交于 2019-12-02 00:09:50

Depending on how distributed your issue is, the solution from the Python Guide (and ultimately from requests source) might work: try importing NullHandler and in the except ImportError clause, define the class like so:

# Set default logging handler to avoid "No handler found" warnings.
import logging
try:  # Python 2.7+
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

logging.getLogger(__name__).addHandler(NullHandler())

Though if using the logging namespace, you'll then want to insert the class:

# in the except clause, after the class def:
logging.NullHandler = NullHandler

Here is my recommended solution:

#!/usr/bin/env python2.6

import os
import logging
modlog = logging.getLogger(__name__)
modlog.addHandler(logging.FileHandler(os.devnull))

# ... remaining code
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!