开发者

Adding magic global methods to modules

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 开发者_Go百科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


You can use:

logger = logging.getLogger(__name__)

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

logger.warn(...)
logger.log(...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜