[Python-Dev] Using logging in the stdlib and its unit tests
Glenn Linderman
v+python at g.nevcal.com
Sat Dec 11 07:25:54 CET 2010
On 12/10/2010 9:06 PM, Nick Coghlan wrote:
> Anyway, the shortest way I could find of setting this up (debug
> silenced, info written to stdout, warning and above written to
> stderr):
>
> import sys, logging
> root = logging.getLogger()
> # Turns out the level of the root logger is set to WARNING by default
> # That should be much easier to find out from the logging docs!
> root.setLevel(logging.DEBUG)
> # Send WARNING and above to stderr
> # basicConfig() is no help, since it sets the specified level on the
> root logger
> # but NOT on the default handler it installs. To emulate console output
> # we want to skip the formatting anyway.
> err = logging.StreamHandler()
> err.setLevel(logging.WARNING)
> root.addHandler(err)
> # Send INFO to stdout
> def is_info(record):
> return record.levelno == logging.INFO
> out = logging.StreamHandler(stream=sys.stdout)
> out.addFilter(is_info)
> root.addHandler(out)
Wow, that's way longer than I expected... almost as long as my "cheap
logger". Greg didn't flesh out the "setup" stuff, but you have, so this
is educational. Thanks.
Not sure I want my INFO to go to stdout, by the way. That corresponds
to "debugging print statement" behavior, but even my "cheap logger"
actually puts stuff in a file instead, and gets it out of the way of the
normal output.
I'd suggest that this be "simplified" to put all the messages in the
same (disk) file, but then I'm sure that there are other preferences
than mine... you clearly thought about what you wanted/thought would be
useful, in coming up with the above.
On 12/10/2010 9:24 PM, Nick Coghlan wrote:
> This could actually make a reasonably good basic for a "task oriented"
> subsection of the logging documentation. Something like:
Yep, agree. But sadly, for each point, there may be multiple options
(your StreamHandler, but I'd want a FileHandler; your separation of
messages by level, my wanting them combined; etc.)
Your comment about basicConfig setting the level on the root logger, but
not on the default handler making it useless is opaque to me, but is
there perhaps room for another basic setup API that could get the setup
code down to a line or two in simple cases? Maybe 3 parameters:
1. minimum level to be generated, which would be passed through to the
root logger and anything else defined by this basic setup API
2. whether debug and info should go to the same or different stream/file
as warn+ messages. Maybe this is a stream or filename or None, the last
implying to use the warn+ output.
3. Where to send the warn+ output.
maybe a 4. Maximum (size, count) of the round-robin log files, assuming
that either or both of 2 & 3 specify files.
Would that be a useful set of functionality to bundle? And could it be
extended, when the user wants more power, or would it have to be
replaced, because it gets in the way of the user that wants more power?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20101210/38ef8dae/attachment.html>
More information about the Python-Dev
mailing list