mirror of
https://github.com/django/daphne.git
synced 2025-03-13 11:45:46 +03:00
Convert Access Log Generator to use Python logging
This commit is contained in:
parent
32ac73e1a0
commit
d96fa9fcff
|
@ -1,5 +1,7 @@
|
||||||
import datetime
|
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] "%{request} %(message)s" '
|
||||||
|
'%(status)s %(length)s'
|
||||||
|
"%d/%b/%Y:%H:%M:%S")
|
||||||
|
handler.setFormatter(fmt=formatter)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
else:
|
||||||
|
logger.addHandler(logging.NullHandler)
|
||||||
|
|
||||||
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",
|
||||||
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,45 @@ 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 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)
|
||||||
"""
|
"""
|
||||||
self.stream.write(
|
|
||||||
'%s %s %s [%s] "%s" %s %s\n'
|
logger.info(
|
||||||
% (
|
f"{request} {details}",
|
||||||
host,
|
extra={
|
||||||
ident or "-",
|
"host": host,
|
||||||
user or "-",
|
"request": request,
|
||||||
date.strftime("%d/%b/%Y:%H:%M:%S"),
|
"details": details,
|
||||||
request,
|
"ident": ident or "-",
|
||||||
status or "-",
|
"user": user or "-",
|
||||||
length or "-",
|
"status": status or "-",
|
||||||
)
|
"length": length or "-",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -228,6 +228,8 @@ 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 +272,9 @@ 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
|
||||||
AccessLogGenerator(access_log_stream) if access_log_stream else None
|
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),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user