[Tutor] use of logging module is shared by all?

Peter Otten __peter__ at web.de
Fri Sep 2 07:53:25 CEST 2011


James Hartley wrote:

> I'm just needing to verify some behavior.
> 
> Functionality within the logging module is exercised by calling functions
> defined within the module itself.  I am using SQLAlchemy for database
> access, but it can be configured to dump out intermediate access
> information
> & queries to the logging module -- which is great.  It really helps in
> debugging.
> 
> However, if I want to write to a log which is separate from what
> SQLAlchemy is doing, am I correct stating that I will not be able to do so
> through the logging module?
> 
> Thanks for any insight which can be shared.

Loggers are typically organized in a tree; sqlalchemy will probably log to

logging.getLogger("sqlalchemy").warn("whatever")

or something like

logging.getLogger("sqlalchemy.some.sublogger").critical("test")

You can prevent the rootlogger from seeing these messages with

sq = logging.getLogger("sqlalchemy")
sq.propagate = False

If for example you want to see messages in stdout by default, but write 
SQLAlchemy's messages to a file you'd do (untested)

import logging
import sys
logging.basicConfig(stream=sys.stdout)

sq = logging.getLogger("sqalchemy")
sqhandler = logging.FileHandler("alchemy.log")
sqformatter = logging.Formatter(logging.BASIC_FORMAT)
sqhandler.setFormatter(sqformatter)
sq.addHandler(sqhandler)
sq.propagate = False

sq.critical("alchemy") # goes to file alchemy.log
logging.getLogger("mine").critical("mine") # goes to stdout




More information about the Tutor mailing list