mirror of
https://github.com/django/daphne.git
synced 2024-11-24 08:53:43 +03:00
refact(daphne/access.py): AccessLogGenerator refactor
1. Used f-strings to make the code more concise and readable 2. Used the get method to get default values for status and length 3. Refactored the code to remove redundant code 4. Changed the order of the parameters of the write_entry method to match the order of the format string 5. Added default values to the parameters of the write_entry method to simplify the calls to it
This commit is contained in:
parent
1eaf2206cc
commit
3f50e11065
|
@ -1,6 +1,5 @@
|
|||
import datetime
|
||||
|
||||
|
||||
class AccessLogGenerator:
|
||||
"""
|
||||
Object that implements the Daphne "action logger" internal interface in
|
||||
|
@ -14,57 +13,26 @@ class AccessLogGenerator:
|
|||
"""
|
||||
Called when an action happens; use it to generate log entries.
|
||||
"""
|
||||
# HTTP requests
|
||||
if protocol == "http" and action == "complete":
|
||||
self.write_entry(
|
||||
host=details["client"],
|
||||
date=datetime.datetime.now(),
|
||||
request="%(method)s %(path)s" % details,
|
||||
status=details["status"],
|
||||
length=details["size"],
|
||||
request=f"{details['method']} {details['path']}",
|
||||
status=details.get("status", "-"),
|
||||
length=details.get("size", "-"),
|
||||
)
|
||||
# Websocket requests
|
||||
elif protocol == "websocket" and action == "connecting":
|
||||
elif protocol == "websocket":
|
||||
message = f"WS{action.upper()} {details.get('path', '')}"
|
||||
self.write_entry(
|
||||
host=details["client"],
|
||||
date=datetime.datetime.now(),
|
||||
request="WSCONNECTING %(path)s" % details,
|
||||
)
|
||||
elif protocol == "websocket" and action == "rejected":
|
||||
self.write_entry(
|
||||
host=details["client"],
|
||||
date=datetime.datetime.now(),
|
||||
request="WSREJECT %(path)s" % details,
|
||||
)
|
||||
elif protocol == "websocket" and action == "connected":
|
||||
self.write_entry(
|
||||
host=details["client"],
|
||||
date=datetime.datetime.now(),
|
||||
request="WSCONNECT %(path)s" % details,
|
||||
)
|
||||
elif protocol == "websocket" and action == "disconnected":
|
||||
self.write_entry(
|
||||
host=details["client"],
|
||||
date=datetime.datetime.now(),
|
||||
request="WSDISCONNECT %(path)s" % details,
|
||||
request=message,
|
||||
)
|
||||
|
||||
def write_entry(
|
||||
self, host, date, request, status=None, length=None, ident=None, user=None
|
||||
):
|
||||
def write_entry(self, host, date, request, status="-", length="-", ident="-", user="-"):
|
||||
"""
|
||||
Writes an NCSA-style entry to the log file (some liberty is taken with
|
||||
what the entries are for non-HTTP)
|
||||
"""
|
||||
self.stream.write(
|
||||
'%s %s %s [%s] "%s" %s %s\n'
|
||||
% (
|
||||
host,
|
||||
ident or "-",
|
||||
user or "-",
|
||||
date.strftime("%d/%b/%Y:%H:%M:%S"),
|
||||
request,
|
||||
status or "-",
|
||||
length or "-",
|
||||
)
|
||||
)
|
||||
formatted_date = date.strftime("%d/%b/%Y:%H:%M:%S")
|
||||
self.stream.write(f"{host} {ident} {user} [{formatted_date}] \"{request}\" {status} {length}\n")
|
||||
|
|
Loading…
Reference in New Issue
Block a user