<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.6000.16939" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009>Hi,</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>I'm trying to 
add the logging module to my application, but I seem to be missing 
something.<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>My 
application (a wxPython one) has a main script that calls various helper 
classes.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>I want the 
log messages from all modules to go to one central log file.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>When I 
implement logging, I think that due to preparation, I get the same message more 
than once.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>Here's an 
example:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face="Arabic Transparent" color=#0000ff><SPAN 
class=626543512-24112009>$ cat -n server.py<BR>     1  
import logging<BR>     2  import 
logging.handlers<BR>     3<BR>     
4  class Server():<BR>     
5      def __init__(self):<BR>     
6          self.client_logger = 
logging.getLogger("client")<BR>     
7          
self.client_logger.setLevel(logging.DEBUG)<BR>     
8          h = 
logging.FileHandler("client.log")<BR>     
9          
h.setLevel(logging.DEBUG)<BR>    
10          formatter = 
logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s 
%(message)s")<BR>    
11          
h.setFormatter(formatter)<BR>    
12          
self.client_logger.addHandler(h)<BR>    13<BR>    
14      def util(self):<BR>    
15          
self.client_logger.warning('This message comes from Server 
module')</SPAN></FONT></DIV>
<DIV><FONT face="Arabic Transparent" color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face="Arabic Transparent" color=#0000ff><SPAN 
class=626543512-24112009>$ cat -n client.py <BR>     1  
import logging<BR>     2  import 
logging.handlers<BR>     3  from server import 
Server<BR>     4<BR>     5  class 
Client():<BR>     6      def 
__init__(self):<BR>     
7          self.client_logger = 
logging.getLogger("client")<BR>     
8          
self.client_logger.setLevel(logging.DEBUG)<BR>     
9          h = 
logging.FileHandler("client.log")<BR>    
10          
h.setLevel(logging.DEBUG)<BR>    
11          formatter = 
logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s 
%(message)s")<BR>    
12          
h.setFormatter(formatter)<BR>    
13          
self.client_logger.addHandler(h)<BR>    14<BR>    
15      def client_test(self):<BR>    
16          
self.client_logger.warning("This message comes from Client 
module")<BR>    17<BR>    18  if __name__ == 
"__main__":<BR>    19      ser = 
Server()<BR>    20      cli = 
Client()<BR>    21      
ser.util()<BR>    22      
cli.client_test()</SPAN></FONT><FONT face="Arabic Transparent" 
color=#0000ff><SPAN class=626543512-24112009><BR></DIV></SPAN></FONT>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009><FONT 
face="Arabic Transparent">$ rm client.log ; python client.py ; cat client.log 
<BR>2009-11-24 14:40:39,762 client       
WARNING  This message comes from Server module<BR>2009-11-24 14:40:39,762 
client       WARNING  This message comes from 
Server module<BR>2009-11-24 14:40:39,762 
client       WARNING  This message comes from 
Client module<BR>2009-11-24 14:40:39,762 
client       WARNING  This message comes from 
Client module</FONT></SPAN></FONT><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009><BR></DIV></SPAN></FONT>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>Googling and 
reading <A 
href="http://docs.python.org/library/logging.html">http://docs.python.org/library/logging.html</A> didn't 
enlighten me.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>Could you 
suggest what should I change in the above scripts so that the log messages would 
appear only once ?</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN 
class=626543512-24112009>Thanks,</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=626543512-24112009>Ron.</DIV>
<DIV><BR></DIV></SPAN></FONT></BODY></HTML>