Python: logging and TCP handler
<p>I wrote my TCP handler as follows (adapted from: <a href="https://docs.python.org/2/library/socketserver.html#socketserver-tcpserver-example" rel="nofollow">https://docs.python.org/2/library/socketserver.html#socketserver-tcpserver-example</a>):</p>

<pre><code>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import SocketServer

from MyModule import myFunction

class MyHandler(SocketServer.StreamRequestHandler):

    def handle(self):
        self.data = self.rfile.readline().strip()
        result = myFunction(self.data)
        self.wfile.write(result)

if __name__ == "__main__":
    HOST, PORT = myhost, myport
    server = SocketServer.TCPServer((HOST, PORT), MyHandler)
    server.serve_forever()
</code></pre>

<p>It works perfectly and now I'm trying to add a logger:</p>

<pre><code>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import SocketServer
import logging
from logging.handlers import TimedRotatingFileHandler

from MyModule import myFunction

class MyHandler(SocketServer.StreamRequestHandler):

    def __init__(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        self.formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
        self.file_handler = TimedRotatingFileHandler('my_log_file.log', when='D', interval=1, utc=True)
        self.file_handler.setLevel(logging.DEBUG)
        self.file_handler.setFormatter(self.formatter)
        self.logger.addHandler(self.file_handler)

    def handle(self):
        self.data = self.rfile.readline().strip()
        result = myFunction(self.data)
        self.wfile.write(result)

        self.logger.info(result)

if __name__ == "__main__":
    HOST, PORT = myhost, myport
    server = SocketServer.TCPServer((HOST, PORT), MyHandler)
    server.serve_forever()
</code></pre>

<p>When I run it I get the following error:</p>

<p><code>TypeError: __init__() takes exactly 1 argument (4 given)</code></p>

<p>I don't understand what the 4 arguments given are.
Is there anything wrong with the code other than that?</p>

<p>EDIT: Full TraceBack:</p>

<pre><code>Exception happened during processing of request from ('MyIP', 54028)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
TypeError: __init__() takes exactly 1 argument (4 given)
</code></pre>

<p><code>MyHandler</code> is a subclass of <code>SocketServer.StreamRequestHandler</code> which is a subclass of <code>BaseRequestHandler</code>. The <a href="https://github.com/python/cpython/blob/2.7/Lib/SocketServer.py#L201" rel="nofollow">call signature of <code>BaseRequestHandler.__init__</code></a> is </p>

<pre><code>def __init__(self, request, client_address, server):
</code></pre>

<p>The traceback error message shows that inside the <a href="https://github.com/python/cpython/blob/2.7/Lib/SocketServer.py#L329" rel="nofollow"><code>BaseServer.finish_request</code> method</a></p>

<pre><code>self.RequestHandlerClass(request, client_address, self)
</code></pre>

<p>is called. <code>self.RequestHandlerClass</code> is <code>MyHandler</code>. Therefore,
<code>MyHandler.__init__</code> should have call signature</p>

<pre><code>class MyHandler(SocketServer.StreamRequestHandler):
    def __init__(self, request, client_address, server):
</code></pre>

<p>instead of </p>

<pre><code>class MyHandler(SocketServer.StreamRequestHandler):
    def __init__(self):
</code></pre>

<hr>

<p>When <code>self.RequestHandlerClass(request, client_address, self)</code> is called, Python calls 
the <code>RequestHandlerClass</code> method with <code>self</code> as its first argument. In other words, 
<code>RequestHandlerClass(self, request, client_address, self)</code> gets called. <code>self, request, client_address, self</code> are the four arguments that are getting passed to <code>MyHandler</code>. 
The error message</p>

<pre><code>TypeError: __init__() takes exactly 1 argument (4 given)
</code></pre>

<p>is complaining that <code>MyHandler.__init__</code> was defined to expect only 1 argument and yet it was being passed 4 arguments.</p>

python python-2.7 logging tcp