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

James Hartley jjhartley at gmail.com
Fri Sep 2 08:16:09 CEST 2011


Thanks, Peter for taking the time to respond. I need to study the reference
further, & your comments pointed out some of my misconceptions.  Thank you
for clearing up some of my half-researched understanding.

Jim

On Thu, Sep 1, 2011 at 10:53 PM, Peter Otten <__peter__ at web.de> wrote:

> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110901/021c7066/attachment.html>


More information about the Tutor mailing list