-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add option ato redact email addresses in logs
fix: Multiple "Re: " prefixes fix: Regex only working with a single "Re: " or "Fwd: " prefix
- Loading branch information
1 parent
fe2c78f
commit 411e840
Showing
4 changed files
with
146 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import re | ||
from loguru import logger | ||
from sys import stderr | ||
|
||
logging_file = stderr | ||
|
||
|
||
def redact_email_sink(message: str): | ||
"""Custom sink function that redacts email addresses before logging.""" | ||
email_pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" | ||
redacted_message = re.sub(email_pattern, "[redacted]", message) | ||
print(redacted_message, file=logging_file) | ||
|
||
|
||
def set_primary_logger(log_level, redact_email_addresses): | ||
"""Set up the primary logger with the specified log level. Output to stderr and use the format specified.""" | ||
logger.remove() | ||
# ^10 is a formatting directive to center with a padding of 10 | ||
logger_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> |<level>{level: ^10}</level>| <level>{message}</level>" | ||
sink = redact_email_sink if redact_email_addresses else stderr | ||
logger.add(sink=sink, format=logger_format, colorize=True, level=log_level) |