[python] Improve performance of application-logging ( Part 1 – server side)

Everybody knows that logging something on screen or to a file is inevitable when you create program. It makes thing visible to you , easy control and for debugging also.

But recently what I have faced is that the performance of my application ( a bunch of services written in python) has decreased so much when I try to print out so many thing to the console ( the primary I/O channel I have ever used).

So I’m finding some ways to make this process become better.

About the context, these log is actually not realtime-critical. I want want to know which step my program is at and what is the result of the executing. That’s all.

The first testing method I try to implement is using UDP broadcasting . Sound so much insanity , right ? I just want to separate the logging process to other task. Not on my main task. And also, that logging should be flexible enough that I can choose to write on the console, write events to database or to the file without any performance-losing from my main jobs.

I have already installed twisted on my machine. So I will take advantage of it.

Let’s build a UDP Event logger server, just modify the class name of UDPEchoServer example.

We have:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from pprint import pprint
class UDPLogger(DatagramProtocol):
 def datagramReceived(self, datagram, address):
 print 'signal from '
 pprint(address)
 print 'content of msg = ' + repr(datagram)
 self.transport.write(datagram, address)
def main():
 reactor.listenUDP(8000, UDPLogger())
 reactor.run()
if __name__ == '__main__':
 main()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: