-
-
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.
Merge pull request #3 from slashtechno/restructure-project
Minimize code in in `__main__.py` by utilizing files under `utils/`
- Loading branch information
Showing
7 changed files
with
583 additions
and
605 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,5 @@ | ||
from llmail.utils.logging import set_primary_logger, logger | ||
from llmail.utils.cli_args import args, bot_email | ||
|
||
# https://stackoverflow.com/a/31079085 | ||
__all__ = ["set_primary_logger", "logger", "args", bot_email] |
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,27 @@ | ||
import re | ||
from sys import stderr | ||
|
||
from loguru import logger | ||
|
||
from llmail.utils.cli_args import args | ||
|
||
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) | ||
|
||
|
||
set_primary_logger(args.log_level, args.redact_email_addresses) |
Oops, something went wrong.