[IPython-dev] iplogging suggestions added

Robert Kern robert.kern at gmail.com
Sat Dec 11 20:19:15 EST 2010


On 2010-12-11 18:07 , Omar Andrés Zapata Mesa wrote:
> Hi Robert.
> The solution was create a class IpLogger that inherit from logging.Logger
> the new code us in repo.

I'm sorry, but I think you misunderstood my point. You should not add Handlers 
or Formatters inside a library (except for the NullHandler), no matter how you 
accomplish this. The decision of what messages get displayed and how they get 
displayed must be left up to the application author (to set the defaults) and 
the application user (possibly overriding the defaults), not the library author. 
Please reread the section of the logging documentation I pointed out:

   http://docs.python.org/library/logging#configuring-logging-for-a-library

My recommendation is to provide the following:

#########

import logging


# Provide the ColorFormatter just as you have it.
FORMAT = ...
class ColorFormatter(logging.Formatter):
     ...

class NullHandler(logging.Handler):
     def emit(self, record):
         pass


def get_logger(name=None):
     """ Provide a logger with a default NullHandler for use within the IPython
     library code.
     """
     logger = logging.getLogger(name)
     logger.addHandler(NullHandler())
     return logger

#########

In each module that needs to do logging, you will do the following:

   from IPython.utils.iplogging import get_logger
   logger = get_logger(__name__)

That's it. No more configuration there. You must configure logging exactly once 
at the application level. As Brian suggestion, a Configurable is probably a good 
way to organize this since you want the application authors to provide a default 
but allow users to override this. Please see the documentation for the variety 
of ways one can configure logging:

   http://docs.python.org/library/logging#configuration

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the IPython-dev mailing list