This commit is contained in:
Valerii 2023-09-07 10:37:27 +00:00 committed by GitHub
commit 4d92501992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,57 +14,30 @@ class AccessLogGenerator:
""" """
Called when an action happens; use it to generate log entries. Called when an action happens; use it to generate log entries.
""" """
# HTTP requests
if protocol == "http" and action == "complete": if protocol == "http" and action == "complete":
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), date=datetime.datetime.now(),
request="%(method)s %(path)s" % details, request=f"{details['method']} {details['path']}",
status=details["status"], status=details.get("status", "-"),
length=details["size"], length=details.get("size", "-"),
) )
# Websocket requests elif protocol == "websocket":
elif protocol == "websocket" and action == "connecting": message = f"WS{action.upper()} {details.get('path', '')}"
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), date=datetime.datetime.now(),
request="WSCONNECTING %(path)s" % details, request=message,
)
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,
) )
def write_entry( def write_entry(
self, host, date, request, status=None, length=None, ident=None, user=None self, host, date, request, status="-", length="-", ident="-", user="-"
): ):
""" """
Writes an NCSA-style entry to the log file (some liberty is taken with Writes an NCSA-style entry to the log file (some liberty is taken with
what the entries are for non-HTTP) what the entries are for non-HTTP)
""" """
formatted_date = date.strftime("%d/%b/%Y:%H:%M:%S")
self.stream.write( self.stream.write(
'%s %s %s [%s] "%s" %s %s\n' f'{host} {ident} {user} [{formatted_date}] "{request}" {status} {length}\n'
% (
host,
ident or "-",
user or "-",
date.strftime("%d/%b/%Y:%H:%M:%S"),
request,
status or "-",
length or "-",
)
) )