Changing logging level only for my code?

jmp jeanmichel at sequans.com
Mon Feb 8 07:07:48 EST 2016


On 02/08/2016 12:29 PM, egarrulo at gmail.com wrote:
> I am using the "logging" module for my own package, but changing the level from "INFO" to "DEBUG" enables debugging statements from third-party libraries as well.  How can I avoid them?  Here is the code I am using:
>
>      import logging
>
>      logger = logging.getLogger(__name__)
>      log_file = logging.FileHandler("activity.log", mode='w')
>      log_file.setFormatter(logging.Formatter(fmt='%(levelname)s: %(message)s'))
>      logger.addHandler(log_file)
>      logging.basicConfig(level=logging.INFO) # or DEBUG
>
> Thank you.
>

Hi,

basicConfig will configure the *root* logger, the parent of all loggers. 
You need to set it to DEBUG, otherwise no DEBUG logs will be displayed.

However you can set levels to loggers independently, and you simply need 
to configure the third party logger to whatever level you want.

logging.basicConfig(level=logging.DEBUG)
logging.getLogger("yourthirdpartyloggername").setLevel(logging.INFO)

JM




More information about the Python-list mailing list