I really missed something in common about the python registration module.
In the following code, I create a logger ( log
) object and add two handlers to it. One with INFO Level and one WARNING Level. Both of them should print to standard output. I expect that calling log.info(msg)
will result in one copy of msg
in my stdout and calling log.warn(msg)
. in two msg
instances printed on my stdout. Here is the code:
import logging import sys logging.basicConfig() log = logging.getLogger('myLogger') log.handlers = [] h1 = logging.StreamHandler(sys.stdout) h1.level = logging.INFO h1.formatter = logging.Formatter('H1 H1 %(message)s ') h2 = logging.StreamHandler(sys.stdout) h2.level = logging.WARNING h2.formatter = logging.Formatter('H2 H2 %(message)s') log.addHandler(h1) log.addHandler(h2) print 'log.level == %s'%logging.getLevelName(log.level) print 'log.info' log.info('this is some info') print 'done' print 'log.warn' log.warn('this is a warning') print 'done'
The exit is really very strange for me. Calling .info
does not have a visual effect. However, when you call warn
two copies of the msg message printed on stdout are displayed (this is normal), but also one copy printed on stderr (why?). This is the result of the above code. Note the formatting of the last line on this output. This line is printed on stderr.
log.level == NOTSET log.info done log.warn H1 H1 this is a warning H2 H2 this is a warning done WARNING:myLogger:this is a warning
So my questions are:
- Why does my
info
call fail to exit even though h1
set to INFO? - Why does my
warn
call lead to additional output in stderr?
source share