-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Add logging on api endpoints (#296)
* add logging on api endpoints * use central log config * add examplfe for a log filter * add example to mask sensitive data with regex * add more log filter examples * use same log config throughout the whole package
Showing
12 changed files
with
119 additions
and
16 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
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,33 @@ | ||
version: 1 | ||
disable_existing_loggers: False | ||
|
||
formatters: | ||
simple: | ||
format: "%(asctime)s %(levelname)s %(message)s" | ||
datefmt: "%Y-%m-%d %H:%M:%S" | ||
|
||
handlers: | ||
file: | ||
class: "logging.FileHandler" | ||
formatter: "simple" | ||
filename: ".log" | ||
mode: "w" | ||
filters: | ||
- "logfilter" | ||
|
||
filters: | ||
logfilter: | ||
(): mytoyota.utils.logging.log_filters.RedactingFilter | ||
patterns: | ||
- 'access_token\":\s*\"([^\"]*)' | ||
- 'id_token\":\s*\"([^\"]*)' | ||
- 'subscriberGuid\":\s*\"([^\"]*)' | ||
- 'contractId\":\s*\"([^\"]*)' | ||
- 'vin\":\s*\"([^\"]*)' | ||
- 'euiccid\":\s*\"([^\"]*)' | ||
- "guid':\\s*'([^']*)" | ||
|
||
root: | ||
level: "DEBUG" | ||
handlers: | ||
- "file" |
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,24 @@ | ||
"""Module with filter log filter classes.""" | ||
import logging | ||
import re | ||
|
||
|
||
class RedactingFilter(logging.Filter): | ||
"""Helper class to filter logs by given pattern via 'logging.Filter'.""" | ||
|
||
def __init__(self, patterns): | ||
"""Initialise RedactingFilter class.""" | ||
super(RedactingFilter, self).__init__() | ||
self._patterns = patterns | ||
|
||
def filter(self, record): | ||
"""Modify the log record to mask sensitive data.""" | ||
record.msg = self.mask_sensitive_data(record.msg) | ||
return True | ||
|
||
def mask_sensitive_data(self, msg): | ||
"""Mask sensitive data in logs by given patterns.""" | ||
for pattern in self._patterns: | ||
compiled_pattern = re.compile(pattern) | ||
msg = compiled_pattern.sub("****", msg) | ||
return msg |
File renamed without changes.
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,13 @@ | ||
"""Load a central config for logging.""" | ||
import logging | ||
import logging.config | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
BASE_DIR = Path(__file__).parent | ||
|
||
with open(file=BASE_DIR / "log_config.yaml", mode="r", encoding="utf-8") as config: | ||
config_dict = yaml.safe_load(config) | ||
|
||
logging.config.dictConfig(config_dict) |
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