38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
"""
|
|
Helper functions for logging.
|
|
"""
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def audit_log(name, **kwargs):
|
|
"""
|
|
DRY helper used to emit an INFO-level log message.
|
|
|
|
Messages logged with this function are used to construct an audit trail. Log messages
|
|
should be emitted immediately after the event they correspond to has occurred and, if
|
|
applicable, after the database has been updated. These log messages use a verbose
|
|
key-value pair syntax to make it easier to extract fields when parsing the application's
|
|
logs.
|
|
|
|
This function is variadic, accepting a variable number of keyword arguments.
|
|
|
|
Arguments:
|
|
name (str): The name of the message to log. For example, 'payment_received'.
|
|
|
|
Keyword Arguments:
|
|
Indefinite. Keyword arguments are strung together as comma-separated key-value
|
|
pairs ordered alphabetically by key in the resulting log message.
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
# Joins sorted keyword argument keys and values with an "=", wraps each value
|
|
# in quotes, and separates each pair with a comma and a space.
|
|
payload = ', '.join([f'{k}="{v}"' for k, v in sorted(kwargs.items())])
|
|
message = f'{name}: {payload}'
|
|
|
|
log.info(message)
|