I'm using SQLAlchemy 0.6.3 with Pylons 1.0, and my sqlalchemy.log file is being polluted by "BEGIN" calls:
2011-01-14 10:15:17 SELECT /* redacted */
2011-01-14 10:15:22 BEGIN
2011-01-14 10:15:27 BEGIN
2011-01-14 10:15:32 BEGIN
2011-01-14 10:15:37 BEGIN
2011-01-14 10:15:42 BEGIN
2011-01-14 10:15:47 BEGIN
2011-01-14 10:开发者_StackOverflow15:52 BEGIN
2011-01-14 10:15:53 SELECT /* redacted */
2011-01-14 10:16:33 BEGIN
2011-01-14 10:16:35 BEGIN
2011-01-14 10:16:42 BEGIN
2011-01-14 10:16:49 BEGIN
2011-01-14 10:16:52 BEGIN
This is really annoying, and gets in the way of seeing useful SQL code being executed by SQLAlchemy. Is there anyway to prevent SQLAlchemy printing these out, or even from executing these altogether? They don't seem to be doing anything.
One approach would be to figure out why SQLAlchemy is doing these, and see if they can be avoid.
Another would be to filter them out at logging level. Python's logging
module supports filters.
Here's a quick stand-alone example, adapted from this example from Python docs:
import logging
class BeginFilter(logging.Filter):
""" This is a filter which drops BEGIN statements. """
def filter(self, record):
return False if record.msg == "BEGIN" else True
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)-15s %(name)-5s %(levelname)-8s %(message)s")
a1 = logging.getLogger("a.b.c")
f = BeginFilter()
a1.addFilter(f)
a1.debug("SELECT ...")
a1.debug("BEGIN")
a1.debug("BEGIN")
a1.debug("BEGIN")
a1.debug("BEGIN")
a1.debug("SELECT ...")
a1.debug("BEGIN")
精彩评论