logger warning doesn't appear even though propagate flag is True
This program prints both the warnings:
#!/usr/bin/env python2.7
import logging
import logging.config
logging.config.fileConfig('log.conf')
log1 = logging.getLogger()
log2 = logging.getLogger('foo.bar')
log2.addHandler(logging.NullHandler())
log1.warn('warning 1')
log2.warn('warning 2')
However, this prints only the first warning:
#!/usr/bin/env python2.7
import logging
import logging.config
log2 = logging.getLogger('foo.bar')
logging.config.fileConfig('log.conf')
log1 = logging.getLogger()
log2.addHandler(logging.NullHandler())
log1.warn('warning 1')
log2.warn('warning 2')
My log.conf file is defined as:
[loggers]
keys=root
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s %(name)s %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S
Could you please explain why the second warning doesn't appear in the
second program?
--
http://mail.python.org/mailman/listinfo/python-list
Two similar logging programs but different ouputs
I have tried writing two programs which are doing similar activities.
The little difference between the two programs is that the first one
configures logger1 using addHandler() method while the second program
configures logger1 from log.conf file.
However, the output differs for both. The first program displays
warnings from both logger1 and logger2. The second program displays
warning from logger1 only. It does not display the warning from
logger2.
Could you please help me understand this difference? Programs and
log.conf file follow:
#!/usr/bin/env python2.7
# This program prints both the warnings
import logging
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure both loggers
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(name)s %(levelname)s %(message)s'))
logger1.addHandler(handler)
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#--
#!/usr/bin/env python2.7
# This program prints only the first warning
import logging
import logging.config
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure root loggers
logging.config.fileConfig('log.conf')
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#---
"""The file 'log.conf' is as follows:
[loggers]
keys=root
[handlers]
keys=streamHandler
[formatters]
keys=simpleFormatter
[logger_root]
handlers=streamHandler
[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(name)s %(levelname)s %(message)s
"""
#---
--
http://mail.python.org/mailman/listinfo/python-list
Re: Two similar logging programs but different ouputs
Thank you Vinay for the quick reply. I have a few more questions.
On Tue, Apr 19, 2011 at 3:27 AM, Vinay Sajip wrote:
> On Apr 18, 10:11 pm, Disc Magnet wrote:
>
>> Could you please help me understand this difference? Programs and
>> log.conf file follow:
>
> The first program prints two messages because loggers pass events to
> handlers attached to themselves and their ancestors. Hence, logger1's
> message is printed by logger1's handler, and logger2's message is
> printed by logger1's handler because logger1 is an ancestor of
> logger2.
>
> In the second case, logger foo.bar exists when fileConfig() is
> called, but it is not named explicitly in the configuration. Hence,
> it is disabled (as documented). Hence only logger1's message is
> printed.
I couldn't find this mentioned in the documentation at:
http://docs.python.org/library/logging.config.html#configuration-file-format
Could you please tell me where this is documented?
>
> NullHandler is a handler which does nothing - there is no point in
> adding it to a system which configures logging, and only any point in
> adding it to top-level loggers of libraries which may be used when
> logging is not configured by the using application (this is also
> documented).
>
Actually, I was learning how logging works with NullHandler because I
am going to use it with a library I am writing. The code that I shared
is meant for experimenting. Once, I am comfortable with the logging
API, I'll separate the code into different modules.
In the following code, foo.bar is not explicitly mentioned in the file
configuration. As per what you said, foo.bar should be disabled.
However, I get the following output:
root WARNING warning 1
foo WARNING warning 2
foo WARNING warning 2
foo.bar WARNING warning 3
foo.bar WARNING warning 3
Program and configuration file follow:
#!/usr/bin/env python2.7
# This program prints only the first warning
import logging
import logging.config
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo')
logger3 = logging.getLogger('foo.bar')
# Configure root loggers
logging.config.fileConfig('log.conf')
logger2.addHandler(logging.NullHandler())
logger3.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
logger3.warn('warning 3')
#---
"""The file 'log.conf' is as follows:
[loggers]
keys=root,foo
[handlers]
keys=streamHandler
[formatters]
keys=simpleFormatter
[logger_root]
handlers=streamHandler
[logger_foo]
qualname=foo
handlers=streamHandler
[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(name)s %(levelname)s %(message)s
"""
#---
--
http://mail.python.org/mailman/listinfo/python-list
When is PEP necessary?
Is PEP necessary to add a new package to the standard library? What if the community just wants to add a new module to an existing package? What if only a new function has to be added to a module in the standard library? What if only a line or two are to be added to a function in the standard library? -- http://mail.python.org/mailman/listinfo/python-list
Re: obviscating python code for distribution
On Mon, May 16, 2011 at 9:06 AM, Littlefield, Tyler wrote: > I'm putting lots of work into this. I would rather not have some script > kiddy dig through it, yank out chunks and do whatever he wants. I just want > to distribute the program as-is, not distribute it and leave it open to > being hacked. Obfuscating the code won't help here. Remember, "the enemy knows the system." -- http://mail.python.org/mailman/listinfo/python-list
