views:

37

answers:

1

I'm starting to get into the Python logging module, but unless I want all messages to say "root" I have to create a logger for each module, and it's kind of a pain to do that over and over again.

I was thinking it would be handy if there were a magic __logger__() method that would return a logger for the current module, creating it if necessary. A magic __logger__ variable that could be called without parenthesis would be even better. How would I go about that?

For example, in a module named foo, I could call __logger__.debug('this is a debug message for the foo module'), and it would show up in my console as "DEBUG:foo:this is a debug message for the foo module".

+2  A: 

You can use:

logger = logging.getLogger(__name__)

at the top of your class, and use it like so:

logger.warn(...)
logger.log(...)
Lie Ryan
+1: You can even `debug = logger.debug; debug('message')` after this for syntactic, module limited, convenience.
msw