Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21272 > unrolled thread
| Started by | J <dreadpiratejeff@gmail.com> |
|---|---|
| First post | 2012-03-06 11:09 -0500 |
| Last post | 2012-03-06 12:03 -0500 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Help me with weird logging problem J <dreadpiratejeff@gmail.com> - 2012-03-06 11:09 -0500
Re: Help me with weird logging problem Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2012-03-06 08:19 -0800
Re: Help me with weird logging problem J <dreadpiratejeff@gmail.com> - 2012-03-06 12:03 -0500
| From | J <dreadpiratejeff@gmail.com> |
|---|---|
| Date | 2012-03-06 11:09 -0500 |
| Subject | Help me with weird logging problem |
| Message-ID | <mailman.430.1331050187.3037.python-list@python.org> |
Hi,
I'm trying to add a couple log handlers to a program. The end goal is
to log things at INFO or above to console, and if a -v option is set
to ALSO log everything at DEBUG or above to a file. However, while I
DO get the log file created, and log messages are appearing there,
Debug messages are not...
Here's some sample code that is essentially the guts of the logging
setup right now, based on what I read in the docs:
#!/usr/bin/python
import logging
import sys
verbose = True
# Set up the logger
console_format = '%(levelname)-8s %(message)s'
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(console_format))
console_handler.setLevel(logging.INFO)
logger = logging.getLogger()
logger.addHandler(console_handler)
if verbose:
verbose_format = '%(asctime)s %(levelname)-8s %(message)s'
verbose_handler = logging.FileHandler('testlog.log')
verbose_handler.setLevel(logging.DEBUG)
verbose_handler.setFormatter(logging.Formatter(verbose_format))
logger.addHandler(verbose_handler)
logging.debug("Setting log level to DEBUG for verbosity")
logging.info("INFO event logged")
logging.warning("WARNING event logged")
logging.error("ERROR event logged")
logging.critical("CRITICAL event logged")
logging.debug("DEBUG event logged")
When I run this I get the following console and log file output:
bladernr@klaatu:~/development/cert-submit-script-improvements/bin$ ./logtest.py
WARNING WARNING event logged
ERROR ERROR event logged
CRITICAL CRITICAL event logged
bladernr@klaatu:~/development/cert-submit-script-improvements/bin$ cat
testlog.log
2012-03-06 11:05:40,297 WARNING WARNING event logged
2012-03-06 11:05:40,297 ERROR ERROR event logged
2012-03-06 11:05:40,298 CRITICAL CRITICAL event logged
So I AM getting logging, but I am not getting the DEBUG level logs in
the log file, nor am I getting INFO level logs on console.
Any idea what I'm doing wrong?
[toc] | [next] | [standalone]
| From | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| Date | 2012-03-06 08:19 -0800 |
| Message-ID | <c9633d71-4156-405d-9aa5-4e692b973f9c@gi10g2000vbb.googlegroups.com> |
| In reply to | #21272 |
On Mar 6, 4:09 pm, J <dreadpiratej...@gmail.com> wrote: > > Any idea what I'm doing wrong? Levels can be set on loggers as well as handlers, and you're only setting levels on the handlers. The default level on the root logger is WARNING. A logger checks its level first, and only if the event passes that test will it be passed to the handlers (which will also perform level tests). So, a logger.setLevel(logging.DEBUG) should be all you need to add before logging anything. Regards, Vinay Sajip
[toc] | [prev] | [next] | [standalone]
| From | J <dreadpiratejeff@gmail.com> |
|---|---|
| Date | 2012-03-06 12:03 -0500 |
| Message-ID | <mailman.434.1331053419.3037.python-list@python.org> |
| In reply to | #21273 |
On Tue, Mar 6, 2012 at 11:19, Vinay Sajip <vinay_sajip@yahoo.co.uk> wrote: > On Mar 6, 4:09 pm, J <dreadpiratej...@gmail.com> wrote: >> >> Any idea what I'm doing wrong? > > Levels can be set on loggers as well as handlers, and you're only > setting levels on the handlers. The default level on the root logger > is WARNING. A logger checks its level first, and only if the event > passes that test will it be passed to the handlers (which will also > perform level tests). > > So, a logger.setLevel(logging.DEBUG) should be all you need to add > before logging anything. > > Regards, > > Vinay Sajip > -- > http://mail.python.org/mailman/listinfo/python-list Thank you very much, Vinay :) I thought it was something simple like that I had overlooked or misunderstood. Cheers, Jeff
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web