Replace default handler of Python logger

前端 未结 2 1928
刺人心
刺人心 2021-01-31 02:20

I\'ve got the following code running on each request of a wsgi (web2py) application:

import logging, logging.handlers
from logging import StreamHandler, Formatte         


        
2条回答
  •  一个人的身影
    2021-01-31 02:54

    You can remove the default handler from the getLogger() using this:

    logging.getLogger().removeHandler(logging.getLogger().handlers[0])
    

    Or clear the existing handlers first before adding handlers that you want:

    logging.getLogger().handlers.clear()
    

    After doing so, these logs will no longer display except the new handlers you have added:

    DEBUG: Do stuff
    WARNING: Do stuff
    

提交回复
热议问题