When coding python, I use the logging module a lot.
After some bad experiences and reading articles like this one, I try to prevent import-time executed code wherever possible.
However, for the sake of simplicity, I tend to get my logging object right at the beginning of the module file:
# -*- coding: utf-8 -*-
import logging
logger = logging.getLogger('product.plugin.foo.bar')
This way, my logger is globally accessible and I can just write “logger.error()” anywhere. The alternative is to create it class-wide:
class Bar(object):
logger = logging.getLogger('product.plugin.foo.bar')
However, now I have to type the Class name everytime. To prevent typing the class name, I am tempted to use “self” instead, which will fail in static methods.
def my_method(self):
Bar.logger.error('foo')
def my_method_2(self):
self.logger.error('foo') # ok...
@staticmethod
def my_method_2():
self.logger.error('foo') # boom!
So, at first, it looks like creating the logger object module-wide seems like the right thing to do – still it feels like I could end up in import-related trouble when doing it like this…
It’s fine. I even use the same variable name
logger. Any logging is better than no logging, but I find it’s nice practise to only expose the logger variable, keep the module hidden away so your code only references the logger, and hence the namespace you’ve designated for the module.If you later need to refine the namespaces for code within the module, you can use
self.loggerwithin those classes, or shadow the global logger where necessary.Update0
Taking note of S.Lott’s contribution below. Also note that generally you don’t want
from x import *anyway.