Skip to content Skip to sidebar Skip to footer

Python Logging Best Practice For Reusable Modules Intended To Be Included With Other Code

I'm developing a reusable Python module (for Python 2.7 if it matters). I am wondering what the best practices are with regard to logging for others who wish to include my module

Solution 1:

Here is a great blog post that outlines some best practices, which I have tried to adopt as my own: http://eric.themoritzfamily.com/learning-python-logging.html

He goes through all the details and rationale, but essentially it comes down to a couple simple principles.

  1. Use getLogger based on your module's __name__ and start logging:

    importlogginglogger= logging.getLogger(__name__)
     logger.info( ... )
     logger.debug( ... )
    
  2. Don't define handlers or configure the appropriate log-level in your module, because they may interfere with the "external calling program" you have in mind. You (or your users) can set up the handlers and the desired level of logging detail for all the sub-modules as needed, in the main application code.

Post a Comment for "Python Logging Best Practice For Reusable Modules Intended To Be Included With Other Code"