Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Twisted use Python logging?

Tags:

I've got a project where I'm using Twisted for my web server. When exceptions occur (such as network errors), it's printing to the console.

I've already got logging through Python's built-in log module - is there any way to tell the reactor to use that instead?

What's the usual pattern for this?

like image 823
Dave Avatar asked Mar 22 '10 15:03

Dave


People also ask

How do I enable logging in Python?

You can configure logging as shown above using the module and class functions or by creating a config file or a dictionary and loading it using fileConfig() or dictConfig() respectively. These are useful in case you want to change your logging configuration in a running application.

Does Python logging use log4j?

log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


2 Answers

Found it. It's actually quite easy:

from twisted.python import log observer = log.PythonLoggingObserver(loggerName='logname') observer.start() 

You just set loggerName to the same logger name that you're using in logging.getLogger().

like image 136
Dave Avatar answered Sep 29 '22 22:09

Dave


You can use twisted.python.log. For example:

from twisted.python import log log.msg('Hello, world.') 
like image 27
Justin Ethier Avatar answered Sep 29 '22 21:09

Justin Ethier