Skip to content

Instantly share code, notes, and snippets.

@dokterbob
Created September 19, 2011 12:29
Show Gist options
  • Save dokterbob/1226398 to your computer and use it in GitHub Desktop.
Save dokterbob/1226398 to your computer and use it in GitHub Desktop.
def sanitize_log_data(secret, data=None, leave_characters=4):
"""
Clean private/secret data from log statements and other data.
Assumes data and secret are strings. Replaces all but the first
`leave_characters` of `secret`, as found in `data`, with '*'.
If no data is given, all but the first `leave_characters` of secret
are simply replaced and returned.
"""
replace_secret = (secret[:leave_characters] +
(len(secret) - leave_characters) * '*')
if data:
return data.replace(secret, replace_secret)
return replace_secret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment