django runserver log add client(request) ip

2023-02-18 14:30:54 浏览数 (1)

source: 

https://code.djangoproject.com/ticket/16301

django source code location ( django 2.2 ):

..Libsite-packagesdjangocoreserversbasehttp.py ( WSGIRequestHandler -> log_message)

代码语言:javascript复制
class WSGIRequestHandler(simple_server.WSGIRequestHandler):
    protocol_version = 'HTTP/1.1'

    def address_string(self):
        # Short-circuit parent method to not call socket.getfqdn
        return self.client_address[0]

    def log_message(self, format, *args):
        extra = {
            'request': self.request,
            'server_time': self.log_date_time_string(),
        }

changed code:

代码语言:javascript复制
class WSGIRequestHandler(simple_server.WSGIRequestHandler):
    protocol_version = 'HTTP/1.1'

    def address_string(self):
        # Short-circuit parent method to not call socket.getfqdn
        return self.client_address[0]

    def log_message(self, format, *args):
        extra = {
            'request': self.request,
            'server_time': self.log_date_time_string()   " - %s" % self.client_address[0],
        }

django runserver log:

代码语言:javascript复制
System check identified no issues (0 silenced).
May 04, 2020 - 20:52:11
Django version 2.2.4, using settings 'django2.settings'
Starting development server at http://192.168.91.116:8002/
Quit the server with CTRL-BREAK.
[04/May/2020 20:52:25 - 192.168.91.116] "GET /bus/baidu HTTP/1.1" 200 611
[04/May/2020 20:52:25 - 192.168.91.116] "GET /static/js/my.js HTTP/1.1" 200 2950
[04/May/2020 20:52:25 - 192.168.91.116] "GET /static/css/my.css HTTP/1.1" 200 859
[04/May/2020 20:52:33 - 192.168.91.38] "GET /bus/baidu HTTP/1.1" 200 611

0 人点赞