Using Custom Filters to Obfuscate Email Addresses in Logs

When logging sensitive information such as email addresses, it’s important to protect user privacy by obfuscating or anonymizing the data. In this guide, we’ll demonstrate how to create a custom filter to obfuscate email addresses in your FastAPI application’s logs.

Creating a Custom Filter

Create a Custom Filter Class:

Python

import logging

class EmailObfuscationFilter(logging.Filter):
def filter(self, record):
if ’email’ in record.msg:
record.msg = record.msg.replace(record.msg.split(‘@’)[0], ‘redacted’)
return True

This filter checks if the log message contains the word “email.” If it does, it replaces the username portion of the email address with “redacted.” You can customize the obfuscation logic to suit your specific needs.

Add the Filter to a Logger:

Python

logger = logging.getLogger(name)
logger.addFilter(EmailObfuscationFilter())

Using the Filter in Endpoints

Python

@app.get("/users/{user_id}")
def read_user(user_id: int):
    user = get_user(user_id)
    logger.info(f"Retrieved user: {user}")
    return user

Additional Considerations

  • Customizable Obfuscation: You can modify the obfuscation logic to suit your specific requirements. For example, you might want to replace the entire email address with a placeholder.
  • Multiple Filters: You can add multiple filters to a logger to apply different obfuscation rules.
  • Performance: Be mindful of the performance impact of custom filters, especially when dealing with large log volumes.
  • Other Sensitive Information: Consider creating filters to obfuscate other sensitive information such as phone numbers, credit card numbers, or personal identifiers.

By using custom filters to obfuscate email addresses and other sensitive information, you can protect user privacy and comply with data protection regulations.

Integrating Logtail for Cloud-Based Logging in FastAPI
Adding JSON-Formatted Log Files

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?