How to log messages _only once_ from all modules ?

Barak, Ron Ron.Barak at lsi.com
Tue Nov 24 07:59:05 EST 2009


Hi,

I'm trying to add the logging module to my application, but I seem to be missing something.
My application (a wxPython one) has a main script that calls various helper classes.
I want the log messages from all modules to go to one central log file.

When I implement logging, I think that due to preparation, I get the same message more than once.

Here's an example:

$ cat -n server.py
     1  import logging
     2  import logging.handlers
     3
     4  class Server():
     5      def __init__(self):
     6          self.client_logger = logging.getLogger("client")
     7          self.client_logger.setLevel(logging.DEBUG)
     8          h = logging.FileHandler("client.log")
     9          h.setLevel(logging.DEBUG)
    10          formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
    11          h.setFormatter(formatter)
    12          self.client_logger.addHandler(h)
    13
    14      def util(self):
    15          self.client_logger.warning('This message comes from Server module')

$ cat -n client.py
     1  import logging
     2  import logging.handlers
     3  from server import Server
     4
     5  class Client():
     6      def __init__(self):
     7          self.client_logger = logging.getLogger("client")
     8          self.client_logger.setLevel(logging.DEBUG)
     9          h = logging.FileHandler("client.log")
    10          h.setLevel(logging.DEBUG)
    11          formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
    12          h.setFormatter(formatter)
    13          self.client_logger.addHandler(h)
    14
    15      def client_test(self):
    16          self.client_logger.warning("This message comes from Client module")
    17
    18  if __name__ == "__main__":
    19      ser = Server()
    20      cli = Client()
    21      ser.util()
    22      cli.client_test()
$ rm client.log ; python client.py ; cat client.log
2009-11-24 14:40:39,762 client       WARNING  This message comes from Server module
2009-11-24 14:40:39,762 client       WARNING  This message comes from Server module
2009-11-24 14:40:39,762 client       WARNING  This message comes from Client module
2009-11-24 14:40:39,762 client       WARNING  This message comes from Client module
Googling and reading http://docs.python.org/library/logging.html didn't enlighten me.

Could you suggest what should I change in the above scripts so that the log messages would appear only once ?

Thanks,
Ron.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 1512 bytes
Desc: image001.jpg
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.jpg
Type: image/jpeg
Size: 1650 bytes
Desc: image003.jpg
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.log
Type: application/octet-stream
Size: 340 bytes
Desc: client.log
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.py
Type: application/octet-stream
Size: 669 bytes
Desc: client.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: server.py
Type: application/octet-stream
Size: 533 bytes
Desc: server.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20091124/17b11925/attachment-0002.obj>


More information about the Python-list mailing list