Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to log messages _only once_ from all modules ?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


Ron.Barak at lsi

Nov 24, 2009, 4:59 AM

Post #1 of 9 (222 views)
Permalink
How to log messages _only once_ from all modules ?

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.
Attachments: image001.jpg (1.48 KB)
  image003.jpg (1.61 KB)
  client.log (0.33 KB)
  client.py (0.65 KB)
  server.py (0.52 KB)


Ron.Barak at lsi

Nov 24, 2009, 5:20 AM

Post #2 of 9 (219 views)
Permalink
How to log messages _only once_ from all modules ? [In reply to]

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.
Attachments: server.py (0.52 KB)
  client.py (0.65 KB)
  client.log (0.33 KB)


soltys at noabuse

Nov 24, 2009, 5:45 AM

Post #3 of 9 (221 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

Barak, Ron pisze:
> 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.
>
>

Have a look at http://docs.python.org/library/logging.html#logger-objects
First thing mentioned is Logger.propagate which is, what I believe, you're
looking for ;)

--
Soltys

"Free software is a matter of liberty not price"
--
http://mail.python.org/mailman/listinfo/python-list


rbarakx at gmail

Nov 24, 2009, 6:07 AM

Post #4 of 9 (217 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

On Nov 24, 3:45 pm, Soltys <sol...@noabuse.com> wrote:
> Barak, Ron pisze:
>
>
>
>
>
> > 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 readinghttp://docs.python.org/library/logging.htmldidn'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.
>
> Have a look athttp://docs.python.org/library/logging.html#logger-objects
> First thing mentioned is Logger.propagate which is, what I believe, you're
> looking for ;)
>
> --
> Soltys
>
> "Free software is a matter of liberty not price"- Hide quoted text -
>
> - Show quoted text -

Hi Soltys,
I actually tried that, without any noticeable effects, viz.:

$ cat server.py
import logging
import logging.handlers

class Server():
def __init__(self):
self.client_logger = logging.getLogger("client")
self.client_logger.setLevel(logging.DEBUG)
h = logging.FileHandler("client.log")
h.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)-12s %
(levelname)-8s %(message)s")
h.setFormatter(formatter)
self.client_logger.addHandler(h)
self.client_logger.propagate = 0

def util(self):
self.client_logger.warning('This message comes from Server
module')

$ cat client.py
import logging
import logging.handlers
from server import Server

class Client():
def __init__(self):
self.client_logger = logging.getLogger("client")
self.client_logger.setLevel(logging.DEBUG)
h = logging.FileHandler("client.log")
h.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)-12s %
(levelname)-8s %(message)s")
h.setFormatter(formatter)
self.client_logger.addHandler(h)
self.client_logger.propagate = 0

def client_test(self):
self.client_logger.warning("This message comes from Client
module")

if __name__ == "__main__":
ser = Server()
cli = Client()
ser.util()
cli.client_test()

$ rm client.log ; python client.py ; cat client.log
2009-11-24 16:06:35,710 client WARNING This message comes from
Server module
2009-11-24 16:06:35,710 client WARNING This message comes from
Server module
2009-11-24 16:06:35,710 client WARNING This message comes from
Client module
2009-11-24 16:06:35,710 client WARNING This message comes from
Client module

$

--
http://mail.python.org/mailman/listinfo/python-list


soltys at noabuse

Nov 24, 2009, 7:08 AM

Post #5 of 9 (214 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

Ron Barak pisze:
> On Nov 24, 3:45 pm, Soltys <sol...@noabuse.com> wrote:
>> Barak, Ron pisze:
>>
>>
>>
>>
>>
>>> 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 readinghttp://docs.python.org/library/logging.htmldidn'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.
>> Have a look athttp://docs.python.org/library/logging.html#logger-objects
>> First thing mentioned is Logger.propagate which is, what I believe, you're
>> looking for ;)
>>
>> --
>> Soltys
>>
>> "Free software is a matter of liberty not price"- Hide quoted text -
>>
>> - Show quoted text -
>
> Hi Soltys,
> I actually tried that, without any noticeable effects, viz.:
>
> $ cat server.py
> import logging
> import logging.handlers
>
> class Server():
> def __init__(self):
> self.client_logger = logging.getLogger("client")
> self.client_logger.setLevel(logging.DEBUG)
> h = logging.FileHandler("client.log")
> h.setLevel(logging.DEBUG)
> formatter = logging.Formatter("%(asctime)s %(name)-12s %
> (levelname)-8s %(message)s")
> h.setFormatter(formatter)
> self.client_logger.addHandler(h)
> self.client_logger.propagate = 0
>
> def util(self):
> self.client_logger.warning('This message comes from Server
> module')
>
> $ cat client.py
> import logging
> import logging.handlers
> from server import Server
>
> class Client():
> def __init__(self):
> self.client_logger = logging.getLogger("client")
> self.client_logger.setLevel(logging.DEBUG)
> h = logging.FileHandler("client.log")
> h.setLevel(logging.DEBUG)
> formatter = logging.Formatter("%(asctime)s %(name)-12s %
> (levelname)-8s %(message)s")
> h.setFormatter(formatter)
> self.client_logger.addHandler(h)
> self.client_logger.propagate = 0
>
> def client_test(self):
> self.client_logger.warning("This message comes from Client
> module")
>
> if __name__ == "__main__":
> ser = Server()
> cli = Client()
> ser.util()
> cli.client_test()
>
> $ rm client.log ; python client.py ; cat client.log
> 2009-11-24 16:06:35,710 client WARNING This message comes from
> Server module
> 2009-11-24 16:06:35,710 client WARNING This message comes from
> Server module
> 2009-11-24 16:06:35,710 client WARNING This message comes from
> Client module
> 2009-11-24 16:06:35,710 client WARNING This message comes from
> Client module
>
> $
>

Rename logger in server.py to server:
self.client_logger = logging.getLogger("server")

Rerun, you should get sth. like this:
$ cat client.log
2009-11-24 16:06:54,990 server WARNING This message comes from Server module
2009-11-24 16:06:54,990 client WARNING This message comes from Client module



--
Soltys

"Free software is a matter of liberty not price"
--
http://mail.python.org/mailman/listinfo/python-list


rbarakx at gmail

Nov 24, 2009, 7:14 AM

Post #6 of 9 (212 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

On Nov 24, 5:08 pm, Soltys <sol...@noabuse.com> wrote:
> Ron Barak pisze:
>
>
>
>
>
> > On Nov 24, 3:45 pm, Soltys <sol...@noabuse.com> wrote:
> >> Barak, Ron pisze:
>
> >>> 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 readinghttp://docs.python.org/library/logging.htmldidn'tenlighten me.
> >>> Could you suggest what should I change in the above scripts so that the log messages would appear only once ?
> >>> Thanks,
> >>> Ron.
> >> Have a look athttp://docs.python.org/library/logging.html#logger-objects
> >> First thing mentioned is Logger.propagate which is, what I believe, you're
> >> looking for ;)
>
> >> --
> >> Soltys
>
> >> "Free software is a matter of liberty not price"- Hide quoted text -
>
> >> - Show quoted text -
>
> > Hi Soltys,
> > I actually tried that, without any noticeable effects, viz.:
>
> > $ cat server.py
> > import logging
> > import logging.handlers
>
> > class Server():
> > def __init__(self):
> > self.client_logger = logging.getLogger("client")
> > self.client_logger.setLevel(logging.DEBUG)
> > h = logging.FileHandler("client.log")
> > h.setLevel(logging.DEBUG)
> > formatter = logging.Formatter("%(asctime)s %(name)-12s %
> > (levelname)-8s %(message)s")
> > h.setFormatter(formatter)
> > self.client_logger.addHandler(h)
> > self.client_logger.propagate = 0
>
> > def util(self):
> > self.client_logger.warning('This message comes from Server
> > module')
>
> > $ cat client.py
> > import logging
> > import logging.handlers
> > from server import Server
>
> > class Client():
> > def __init__(self):
> > self.client_logger = logging.getLogger("client")
> > self.client_logger.setLevel(logging.DEBUG)
> > h = logging.FileHandler("client.log")
> > h.setLevel(logging.DEBUG)
> > formatter = logging.Formatter("%(asctime)s %(name)-12s %
> > (levelname)-8s %(message)s")
> > h.setFormatter(formatter)
> > self.client_logger.addHandler(h)
> > self.client_logger.propagate = 0
>
> > def client_test(self):
> > self.client_logger.warning("This message comes from Client
> > module")
>
> > if __name__ == "__main__":
> > ser = Server()
> > cli = Client()
> > ser.util()
> > cli.client_test()
>
> > $ rm client.log ; python client.py ; cat client.log
> > 2009-11-24 16:06:35,710 client WARNING This message comes from
> > Server module
> > 2009-11-24 16:06:35,710 client WARNING This message comes from
> > Server module
> > 2009-11-24 16:06:35,710 client WARNING This message comes from
> > Client module
> > 2009-11-24 16:06:35,710 client WARNING This message comes from
> > Client module
>
> > $
>
> Rename logger in server.py to server:
> self.client_logger = logging.getLogger("server")
>
> Rerun, you should get sth. like this:
> $ cat client.log
> 2009-11-24 16:06:54,990 server WARNING This message comes from Server module
> 2009-11-24 16:06:54,990 client WARNING This message comes from Client module
>
> --
> Soltys
>
> "Free software is a matter of liberty not price"- Hide quoted text -
>
> - Show quoted text -

Many thanks Soltys, that did the trick (actually, no need for setting
propagate to 0), namely,

$ cat client.py
import logging
import logging.handlers
from server import Server

class Client():
def __init__(self):
self.client_logger = logging.getLogger("client")
self.client_logger.setLevel(logging.DEBUG)
h = logging.FileHandler("client.log")
h.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)-12s %
(levelname)-8s %(message)s")
h.setFormatter(formatter)
self.client_logger.addHandler(h)

def client_test(self):
self.client_logger.warning("This message comes from Client
module")

if __name__ == "__main__":
ser = Server()
cli = Client()
ser.util()
cli.client_test()

$ cat server.py
import logging
import logging.handlers

class Server():
def __init__(self):
self.client_logger = logging.getLogger("server")
self.client_logger.setLevel(logging.DEBUG)
h = logging.FileHandler("client.log")
h.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)-12s %
(levelname)-8s %(message)s")
h.setFormatter(formatter)
self.client_logger.addHandler(h)

def util(self):
self.client_logger.warning('This message comes from Server
module')

$ rm client.log ; python client.py ; cat client.log
2009-11-24 17:13:15,989 server WARNING This message comes from
Server module
2009-11-24 17:13:15,989 client WARNING This message comes from
Client module

--
http://mail.python.org/mailman/listinfo/python-list


vinay_sajip at yahoo

Nov 24, 2009, 12:59 PM

Post #7 of 9 (211 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

On Nov 24, 3:14 pm, Ron Barak <rbar...@gmail.com> wrote:
> Many thanks Soltys, that did the trick (actually, no need for setting
> propagate to 0), namely,
>
[snip]

It might work for now, but instantiating a handler in an instance
constructor can be an anti-pattern. If you ever create multiple client
instances, for example, you would create multiple handlers and add
those to the logger (which is essentially persistent for the lifetime
of the process), which could result in multiple messages, corrupted
output or exceptions (in the general case).

It's generally better to declare module-level loggers via

logger = logging.getLogger(__name__)

or, if better granularity is wanted, loggers with names prefixed by
__name__ + '.' - and handler set up should typically be done in one
place in such a way as to not inadvertently create handlers multiple
times. A common pattern is to just add handlers to the root logger
(e.g. the basicConfig() API does this) - other loggers automatically
get to use them, under normal circumstances.

Regards,

Vinay Sajip
--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

Nov 25, 2009, 3:58 AM

Post #8 of 9 (195 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

Barak, Ron wrote:
> 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:
>
> [snip example]
>
> Could you suggest what should I change in the above scripts so that
> the log messages would appear only once ?
>
> Thanks,
> Ron.
>
If you are starting with the logging facility, I would suggest to add
handlers only to the root logger (in your __main__ section).

Basically, never configure or add handlers to any logger except for the
root logger in your __main__ section. There are very few reasons why you
would break this rule. And when you'll be familiar with the logging
module you'll when to break it.


[server.py]

import logging
import logging.handlers

logger = logging.getLogger(__name__) # you'd better to create the logger
at the module level, you may want to log within the module function

def aFunction(a, b, ,c):
logger.debug('You called aFunction')

class Server():
def __init__(self):
self.logger = logger

def util(self):
self.logger.warning('This message comes from Server module ')


[client.py]

import logging
import logging.handlers
from server import Server

logger = logging.getLogger(__name__)

class Client():
def __init__(self):
self.logger = logger

def client_test(self):
self.logger.warning("This message comes from Client module")

if __name__ == "__main__":
rootLogger = logging.getLogger()
rootLogger.addHandler(logging.FileHandler("client.log"))
rootLogger.handlers[-1].setFormatter(logging.Formatter("%(asctime)s
%(name)-12s %(levelname)-8s %(message)s"))
rootLogger.setLevel(logging.DEBUG)
ser = Server()
cli = Client()
ser.util()
cli.client_test()

Happy logging,

Jean-Michel
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 25, 2009, 12:03 PM

Post #9 of 9 (193 views)
Permalink
Re: How to log messages _only once_ from all modules ? [In reply to]

Jean-Michel Pichavant wrote:

>
> Basically, never configure or add handlers to any logger except for the
> root logger in your __main__ section. There are very few reasons why you
> would break this rule. And when you'll be familiar with the logging
> module you'll when to break it.

I have never used logging, but if and when I have a need for it, the
advice above and your clear example will be a good guide to getting
started. Thank you from me too.

tjr

> [server.py]
>
> import logging
> import logging.handlers
>
> logger = logging.getLogger(__name__) # you'd better to create the logger
> at the module level, you may want to log within the module function
>
> def aFunction(a, b, ,c):
> logger.debug('You called aFunction')
>
> class Server():
> def __init__(self):
> self.logger = logger
>
> def util(self):
> self.logger.warning('This message comes from Server module ')
>
>
> [client.py]
>
> import logging
> import logging.handlers
> from server import Server
>
> logger = logging.getLogger(__name__)
>
> class Client():
> def __init__(self):
> self.logger = logger
>
> def client_test(self):
> self.logger.warning("This message comes from Client module")
>
> if __name__ == "__main__":
> rootLogger = logging.getLogger()
> rootLogger.addHandler(logging.FileHandler("client.log"))
> rootLogger.handlers[-1].setFormatter(logging.Formatter("%(asctime)s
> %(name)-12s %(levelname)-8s %(message)s"))
> rootLogger.setLevel(logging.DEBUG)
> ser = Server()
> cli = Client()
> ser.util()
> cli.client_test()
>
> Happy logging,
>
> Jean-Michel

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.