This commit is contained in:
Mark Bidewell 2025-01-23 12:22:39 +01:00 committed by GitHub
commit fa0a7c5031
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 29 deletions

View File

@ -1,4 +1,6 @@
import datetime import logging
logger = logging.getLogger(__name__)
class AccessLogGenerator: class AccessLogGenerator:
@ -8,7 +10,16 @@ class AccessLogGenerator:
""" """
def __init__(self, stream): def __init__(self, stream):
self.stream = stream if stream:
logger.propagate = False
handler = logging.StreamHandler(stream)
formatter = logging.Formatter(
'%(host)s %(ident)s %(user)s [%(asctime)s] "%(message)s" '
"%(status)s %(length)s",
"%d/%b/%Y:%H:%M:%S",
)
handler.setFormatter(fmt=formatter)
logger.addHandler(handler)
def __call__(self, protocol, action, details): def __call__(self, protocol, action, details):
""" """
@ -18,8 +29,8 @@ class AccessLogGenerator:
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(), request="%(method)s" % details,
request="%(method)s %(path)s" % details, details="%(path)s" % details,
status=details["status"], status=details["status"],
length=details["size"], length=details["size"],
) )
@ -27,44 +38,48 @@ class AccessLogGenerator:
elif protocol == "websocket" and action == "connecting": elif protocol == "websocket" and action == "connecting":
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), request="WSCONNECTING",
request="WSCONNECTING %(path)s" % details, details="%(path)s" % details,
) )
elif protocol == "websocket" and action == "rejected": elif protocol == "websocket" and action == "rejected":
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), request="WSREJECT",
request="WSREJECT %(path)s" % details, details="%(path)s" % details,
) )
elif protocol == "websocket" and action == "connected": elif protocol == "websocket" and action == "connected":
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), request="WSCONNECT",
request="WSCONNECT %(path)s" % details, details="%(path)s" % details,
) )
elif protocol == "websocket" and action == "disconnected": elif protocol == "websocket" and action == "disconnected":
self.write_entry( self.write_entry(
host=details["client"], host=details["client"],
date=datetime.datetime.now(), request="WSDISCONNECT",
request="WSDISCONNECT %(path)s" % details, details="%(path)s" % details,
) )
def write_entry( def write_entry(
self, host, date, request, status=None, length=None, ident=None, user=None self, host, request, details, status=None, length=None, ident=None, user=None
): ):
""" """
Writes an NCSA-style entry to the log file (some liberty is taken with Writes an access log. If a file is specified, an NCSA-style entry to the log file
what the entries are for non-HTTP) (some liberty is taken with what the entries are for non-HTTP). The format can be
overriden with logging configuration for 'daphne.access'
""" """
self.stream.write(
'%s %s %s [%s] "%s" %s %s\n' logger.info(
% ( "%s %s",
host,
ident or "-",
user or "-",
date.strftime("%d/%b/%Y:%H:%M:%S"),
request, request,
status or "-", details,
length or "-", extra={
) "host": host,
"request": request,
"details": details,
"ident": ident or "-",
"user": user or "-",
"status": status or "-",
"length": length or "-",
},
) )

View File

@ -228,6 +228,10 @@ class CommandLineInterface:
elif args.verbosity >= 1: elif args.verbosity >= 1:
access_log_stream = sys.stdout access_log_stream = sys.stdout
access_logger = (
AccessLogGenerator(access_log_stream) if access_log_stream else None
)
# Import application # Import application
sys.path.insert(0, ".") sys.path.insert(0, ".")
application = import_by_path(args.application) application = import_by_path(args.application)
@ -270,9 +274,7 @@ class CommandLineInterface:
websocket_connect_timeout=args.websocket_connect_timeout, websocket_connect_timeout=args.websocket_connect_timeout,
websocket_handshake_timeout=args.websocket_connect_timeout, websocket_handshake_timeout=args.websocket_connect_timeout,
application_close_timeout=args.application_close_timeout, application_close_timeout=args.application_close_timeout,
action_logger=( action_logger=access_logger if access_log_stream else None,
AccessLogGenerator(access_log_stream) if access_log_stream else None
),
root_path=args.root_path, root_path=args.root_path,
verbosity=args.verbosity, verbosity=args.verbosity,
proxy_forwarded_address_header=self._get_forwarded_host(args=args), proxy_forwarded_address_header=self._get_forwarded_host(args=args),