Doing logging wrong: python-markdown
Python Markdown 2.0.1 contains this very interesting snippet of code, observe:
def message(level, text): """ A wrapper method for logging debug messages. """ logger = logging.getLogger('MARKDOWN') if logger.handlers: # The logger is configured logger.log(level, text) if level > WARN: sys.exit(0) elif level > WARN: raise MarkdownException, text else: warnings.warn(text, MarkdownWarning)
This function is used to implement so-called "logging", and calls to it are spread throughout the distribution's code.
Among other things, the author(s) thought it'd be neat to log each and every text parsed, so if you're debugging - good luck reading any of your logs.
But that's not the real issue here. If you were to be clever and do the following:
import logging class NullHandler(logging.Handler): def emit(self, record): pass markdown_logger = logging.getLogger("MARKDOWN") markdown_logger.addHandler(NullHandler())
Then the sys.exit(0) part triggers.
Add a hypothetical situation, your application is a Web application (not an uncommon use for Markdown) and you deploy your code with FastCGI and a WSGI bridge.
When Markdown fails parsing some code or otherwise errors, it'll... Exit the process. Just like that.
Complete and utter disaster. What we're seeing is application code in a library.
How to stop this from happening then? Well, um... Monkey-patch markdown.message is the only real solution to the problem.
The other solution, which at least stops you from being spammed with crap from Markdown, is to do logging.getLogger("MARKDOWN").setLevel(logging.INFO).
Thank you for this!
Markdown's output has added gigabytes to my paster.log and made it incredibly hard to debug. I had to use your setLevel oneliner, but would like to be able to configure it properly with the rest of my app's logging.
