-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customize default uvicorn logging config to include authenticated use…
…r/service, correlation ID. (#750) * Customize default uvicorn logging config to include correlation ID. * Update CHANGELOG * Include current principal in access logs. * Fix regression: 'tiled serve config' defaults to no logs. * Fix order to restore correlation ID. * TST: Deal with color codes in log output. * Do not log timestamp by default. * Add --log-timestamps to opt in to timestamps. * Test 'tiled serve config ...'
- Loading branch information
1 parent
db95b91
commit 447a3d7
Showing
7 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
LOGGING_CONFIG = { | ||
"disable_existing_loggers": False, | ||
"filters": { | ||
"principal": { | ||
"()": "tiled.server.principal_log_filter.PrincipalFilter", | ||
}, | ||
"correlation_id": { | ||
"()": "asgi_correlation_id.CorrelationIdFilter", | ||
"default_value": "-", | ||
"uuid_length": 16, | ||
}, | ||
}, | ||
"formatters": { | ||
"access": { | ||
"()": "uvicorn.logging.AccessFormatter", | ||
"datefmt": "%Y-%m-%dT%H:%M:%S", | ||
"format": ( | ||
"[%(correlation_id)s] " | ||
'%(client_addr)s (%(principal)s) - "%(request_line)s" ' | ||
"%(status_code)s" | ||
), | ||
"use_colors": True, | ||
}, | ||
"default": { | ||
"()": "uvicorn.logging.DefaultFormatter", | ||
"datefmt": "%Y-%m-%dT%H:%M:%S", | ||
"format": "[%(correlation_id)s] %(levelprefix)s %(message)s", | ||
"use_colors": True, | ||
}, | ||
}, | ||
"handlers": { | ||
"access": { | ||
"class": "logging.StreamHandler", | ||
"filters": ["principal", "correlation_id"], | ||
"formatter": "access", | ||
"stream": "ext://sys.stdout", | ||
}, | ||
"default": { | ||
"class": "logging.StreamHandler", | ||
"filters": ["correlation_id"], | ||
"formatter": "default", | ||
"stream": "ext://sys.stderr", | ||
}, | ||
}, | ||
"loggers": { | ||
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False}, | ||
"uvicorn.error": { | ||
"handlers": ["default"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
}, | ||
"version": 1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from logging import Filter, LogRecord | ||
|
||
from ..utils import SpecialUsers | ||
from .app import current_principal | ||
|
||
|
||
class PrincipalFilter(Filter): | ||
"""Logging filter to attach username or Service Principal UUID to LogRecord""" | ||
|
||
def filter(self, record: LogRecord) -> bool: | ||
principal = current_principal.get() | ||
if isinstance(principal, SpecialUsers): | ||
short_name = f"{principal.value}" | ||
elif principal.type == "service": | ||
short_name = f"service:{principal.uuid}" | ||
else: # principal.type == "user" | ||
short_name = ",".join( | ||
f"'{identity.id}'" for identity in principal.identities | ||
) | ||
record.principal = short_name | ||
return True |