log_redaction
Helpers for logging EHR request failures without identifiers.
Module
Functions
describe_error_chain_for_log
def describe_error_chain_for_log(error: BaseException) ‑> str:Describe an error and the errors it was raised from, without messages.
A wrapping error (an AuthenticationError raised from whatever the token
source raised) names the layer that gave up, not the cause; the cause is
further down the chain. Each link is described by describe_error_for_log,
so no message -- which can carry a URL, a username or an identifier -- is
included.
Arguments
error: The outermost exception.
Returns
The descriptions from outermost to innermost, joined by " <- ", e.g.
"AuthenticationError <- HTTPError (status 404)".
describe_error_for_log
def describe_error_for_log(error: BaseException) ‑> str:Describe an error for logging without its message.
The message of an HTTP library error is the full request URL, which can carry identifiers, patient IDs and signed query strings, so only the exception type and the HTTP status (when the error has a response) are given.
Arguments
error: The exception to describe.
Returns The exception type name, followed by the HTTP status if there is one.
redact_url_for_log
def redact_url_for_log(url: str | None, id_after: Collection[str] | None = None) ‑> str:Drop the query string of a URL or path and redact its identifier segments.
Arguments
url: An absolute URL or a path.id_after: Path segments whose following segment is an identifier. When omitted, any segment starting with an uppercase letter (a FHIR resource type, as inPatient/123) is treated as one.
Returns The URL without query string or fragment, with each identifier segment redacted.
text_shape
def text_shape(text: str, keep: Collection[str] = ()) ‑> str:Describe the shape of free text without revealing any of it.
Digits become 9. A run of letters becomes a, unless its upper-case
form is in keep, in which case it is given in upper case. Whitespace
runs become one space, punctuation is kept, and any other unprintable
character becomes ?. A shape longer than 40 characters is cut so that
it is 40 long and ends in ….
Arguments
text: The text to describe.keep: Upper-case tokens that are not identifying (e.g."OD") and so are kept readable.
Returns
The masked shape, e.g. "a 99/99 CC" for "John 20/40 cc" with
keep={"CC"}.