Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35720 > unrolled thread
| Started by | andrew cooke <andrew@acooke.org> |
|---|---|
| First post | 2012-12-28 11:57 -0800 |
| Last post | 2012-12-29 04:31 +0000 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.python
Confused about logger config from within Python (3) andrew cooke <andrew@acooke.org> - 2012-12-28 11:57 -0800
Re: Confused about logger config from within Python (3) andrew cooke <andrew@acooke.org> - 2012-12-28 16:41 -0800
Re: Confused about logger config from within Python (3) Peter Otten <__peter__@web.de> - 2012-12-29 01:56 +0100
Re: Confused about logger config from within Python (3) andrew cooke <andrew@acooke.org> - 2012-12-28 18:29 -0800
Re: Confused about logger config from within Python (3) Dave Angel <d@davea.name> - 2012-12-28 22:02 -0500
Re: Confused about logger config from within Python (3) andrew cooke <andrew@acooke.org> - 2012-12-28 18:29 -0800
Re: Confused about logger config from within Python (3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-29 05:05 +0000
Re: Confused about logger config from within Python (3) Roy Smith <roy@panix.com> - 2012-12-29 08:48 -0500
Re: Confused about logger config from within Python (3) Terry Reedy <tjreedy@udel.edu> - 2012-12-29 19:18 -0500
Re: Confused about logger config from within Python (3) Roy Smith <roy@panix.com> - 2012-12-29 20:08 -0500
Re: Confused about logger config from within Python (3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-29 04:31 +0000
| From | andrew cooke <andrew@acooke.org> |
|---|---|
| Date | 2012-12-28 11:57 -0800 |
| Subject | Confused about logger config from within Python (3) |
| Message-ID | <a51d0c8a-adc9-4d8e-b3cc-affd7c6aad95@googlegroups.com> |
When I use a config file things seem to work (in other projects), but for my current code I hoped to configure logging from Python.
I distilled my problem down to the following test, which does not print anything. Please can someone explain why? I was expecting the module's logger to delegate to root, which has the DEBUG level set.
from logging import DEBUG, root, getLogger
from unittest import TestCase
class LoggingTest(TestCase):
def test_direct(self):
root.setLevel(DEBUG)
getLogger(__name__).debug("hello world")
Thanks,
Andrew
[toc] | [next] | [standalone]
| From | andrew cooke <andrew@acooke.org> |
|---|---|
| Date | 2012-12-28 16:41 -0800 |
| Message-ID | <3149c22d-ae6d-43b4-8f88-3cc5fa91094c@googlegroups.com> |
| In reply to | #35720 |
similarly, if i run the following, i see only "done":
from logging import DEBUG, root, getLogger
if __name__ == '__main__':
root.setLevel(DEBUG)
getLogger(__name__).debug("hello world")
print('done')
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-12-29 01:56 +0100 |
| Message-ID | <mailman.1413.1356742618.29569.python-list@python.org> |
| In reply to | #35723 |
andrew cooke wrote:
> similarly, if i run the following, i see only "done":
>
> from logging import DEBUG, root, getLogger
>
> if __name__ == '__main__':
> root.setLevel(DEBUG)
> getLogger(__name__).debug("hello world")
> print('done')
You need a handler. The easiest way to get one is logging.basicConfig().
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.getLogger().debug("goodbye, cruel world")
DEBUG:root:goodbye, cruel world
Other revolutionary ideas: read the docs
<http://docs.python.org/dev/howto/logging.html#logging-basic-tutorial> ;)
[toc] | [prev] | [next] | [standalone]
| From | andrew cooke <andrew@acooke.org> |
|---|---|
| Date | 2012-12-28 18:29 -0800 |
| Message-ID | <36b97c31-8d50-4361-8cd2-3f06b34c6af0@googlegroups.com> |
| In reply to | #35724 |
On Friday, 28 December 2012 21:56:46 UTC-3, Peter Otten wrote: > Other revolutionary ideas: read the docs > > <http://docs.python.org/dev/howto/logging.html#logging-basic-tutorial> ;) how do you think i knew about the root handler without reading the damn docs you condescending asshole? anyway, thanks for the help. andrew
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-28 22:02 -0500 |
| Message-ID | <mailman.1420.1356750188.29569.python-list@python.org> |
| In reply to | #35731 |
On 12/28/2012 09:29 PM, andrew cooke wrote: > On Friday, 28 December 2012 21:56:46 UTC-3, Peter Otten wrote: > <snip> > reading the damn docs you condescending *****? > You've made four posts this year to this forum, in two threads, to ask for help. In that same year, Peter Otten has voluntarily helped others with 325 messages. As far as I can see, he hasn't started any threads, just helped where he could. So your way of giving thanks to him, and to the hundreds of others who give so generously of their time, is to call him a potty-name? People like you make me rethink my own commitment to helping. Fortunately, there aren't too many of you. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | andrew cooke <andrew@acooke.org> |
|---|---|
| Date | 2012-12-28 18:29 -0800 |
| Message-ID | <mailman.1419.1356748166.29569.python-list@python.org> |
| In reply to | #35724 |
On Friday, 28 December 2012 21:56:46 UTC-3, Peter Otten wrote: > Other revolutionary ideas: read the docs > > <http://docs.python.org/dev/howto/logging.html#logging-basic-tutorial> ;) how do you think i knew about the root handler without reading the damn docs you condescending asshole? anyway, thanks for the help. andrew
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-12-29 05:05 +0000 |
| Message-ID | <50de7a0a$0$29967$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #35723 |
On Fri, 28 Dec 2012 16:41:20 -0800, andrew cooke wrote:
> similarly, if i run the following, i see only "done":
>
> from logging import DEBUG, root, getLogger
>
> if __name__ == '__main__':
> root.setLevel(DEBUG)
> getLogger(__name__).debug("hello world")
> print('done')
In Python 2.7, the above prints:
py> from logging import DEBUG, root, getLogger
py> root.setLevel(DEBUG)
py> getLogger(__name__).debug("hello world"); print("done")
No handlers could be found for logger "__main__"
done
In Python 3.2 and 3.3, the message about no handlers is not printed,
which is an interesting difference. (Somebody who knows more about the
logging package than I do might be able to state why that difference.) So
it would help if you told us what version of Python you're running.
This works as expected:
py> lg = getLogger(__name__)
py> lg.level = logging.DEBUG
py> lg.debug("hello world"); print("done")
DEBUG:__main__:hello world
done
since the default logging level is WARNING, as the tutorial explains:
[quote]
The default level is WARNING, which means that only events of this level
and above will be tracked, unless the logging package is configured to do
otherwise.
[end quote]
http://docs.python.org/dev/howto/logging.html
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-12-29 08:48 -0500 |
| Message-ID | <roy-DE6FD3.08481229122012@news.panix.com> |
| In reply to | #35740 |
In article <50de7a0a$0$29967$c3e8da3$5496439d@news.astraweb.com>, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > In Python 3.2 and 3.3, the message about no handlers is not printed, > which is an interesting difference. (Somebody who knows more about the > logging package than I do might be able to state why that difference.) I hope that means that http://bugs.python.org/issue994421 has finally been fixed :-)
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-12-29 19:18 -0500 |
| Message-ID | <mailman.1455.1356826759.29569.python-list@python.org> |
| In reply to | #35748 |
On 12/29/2012 8:48 AM, Roy Smith wrote: > In article <50de7a0a$0$29967$c3e8da3$5496439d@news.astraweb.com>, > Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > >> In Python 3.2 and 3.3, the message about no handlers is not printed, >> which is an interesting difference. (Somebody who knows more about the >> logging package than I do might be able to state why that difference.) > > I hope that means that http://bugs.python.org/issue994421 has finally > been fixed :-) I added an update ;-). If Vijay wanted the message to be a warning rather than an exception, the warnings module and mechanism could have been used (once it was available -- I am not sure when that was). But I think a default handler is even better. Given that you disagreed with his disposition of the issue, you might have posted to this list for other opinions. I also hope you appreciate that Vijay has stuck with maintenance and upgrade of the module for nearly a decade now. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-12-29 20:08 -0500 |
| Message-ID | <roy-58EC9B.20081429122012@news.panix.com> |
| In reply to | #35792 |
In article <mailman.1455.1356826759.29569.python-list@python.org>,
Terry Reedy <tjreedy@udel.edu> wrote:
> On 12/29/2012 8:48 AM, Roy Smith wrote:
> > In article <50de7a0a$0$29967$c3e8da3$5496439d@news.astraweb.com>,
> > Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> >
> >> In Python 3.2 and 3.3, the message about no handlers is not printed,
> >> which is an interesting difference. (Somebody who knows more about the
> >> logging package than I do might be able to state why that difference.)
> >
> > I hope that means that http://bugs.python.org/issue994421 has finally
> > been fixed :-)
>
> I added an update ;-).
>
> If Vijay wanted the message to be a warning rather than an exception,
> the warnings module and mechanism could have been used (once it was
> available -- I am not sure when that was). But I think a default handler
> is even better.
Having a default handler is clearly a good fix.
> I also hope you appreciate that Vijay has stuck with maintenance and
> upgrade of the module for nearly a decade now.
Absolutely.
BTW, I recently discovered a truly awesome thing about logging and
nosetests. Apparently, nose attaches a hander to the root logger at
debug level and buffers anything that gets sent there. If a test fails,
it prints anything that logger captured. So, for example (intentionally
breaking one of my tests):
-------------------- >> begin captured logging << --------------------
requests.packages.urllib3.connectionpool: INFO: Starting new HTTP
connection (1): localhost.lic.songza.com
requests.packages.urllib3.connectionpool: DEBUG: "GET
/foo:80/api/v2/?format=json ('HTTP/1.1',)" 404 None
--------------------- >> end captured logging << ---------------------
This rocks!
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-12-29 04:31 +0000 |
| Message-ID | <50de7232$0$29967$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #35720 |
On Fri, 28 Dec 2012 11:57:29 -0800, andrew cooke wrote:
> When I use a config file things seem to work (in other projects), but
> for my current code I hoped to configure logging from Python.
>
> I distilled my problem down to the following test, which does not print
> anything. Please can someone explain why? I was expecting the module's
> logger to delegate to root, which has the DEBUG level set.
>
> from logging import DEBUG, root, getLogger
> from unittest import TestCase
>
> class LoggingTest(TestCase):
> def test_direct(self):
> root.setLevel(DEBUG)
> getLogger(__name__).debug("hello world")
Nothing gets printed because you don't do anything except define a class.
Try instantiating the class, then calling the test_direct method. The
most convenient way to do so is with the unittest module:
# after defining the class above
import unittest
unittest.main()
which then prints:
py> unittest.main()
No handlers could be found for logger "__main__"
.
----------------------------------------------------------------------
Ran 1 test in 0.045s
OK
So you need a handler.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web