-
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 secret masking in logs (#21)
- Loading branch information
Showing
7 changed files
with
127 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .config import * | ||
from .formatters import * |
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,70 @@ | ||
import dataclasses | ||
import logging.config as logging_config | ||
import sys | ||
import typing | ||
|
||
import lib.utils.logging.formatters as formatters | ||
|
||
LogLevel = typing.Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] | ||
|
||
|
||
def initialize( | ||
config: dict[str, typing.Any], | ||
) -> None: | ||
logging_config.dictConfig(config) | ||
|
||
|
||
@dataclasses.dataclass | ||
class LoggerConfig: | ||
propagate: bool | ||
level: LogLevel | ||
|
||
|
||
def create_config( | ||
log_level: LogLevel, | ||
log_format: str, | ||
loggers: dict[str, LoggerConfig] | None = None, | ||
) -> dict[str, typing.Any]: | ||
config: dict[str, typing.Any] = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"handlers": { | ||
"default": { | ||
"stream": sys.stdout, | ||
"level": log_level, | ||
"formatter": "default", | ||
"class": "logging.StreamHandler", | ||
}, | ||
}, | ||
"formatters": { | ||
"default": { | ||
"()": lambda: formatters.CustomFormatter( | ||
fmt=log_format, | ||
), | ||
}, | ||
}, | ||
"root": { | ||
"level": log_level, | ||
"handlers": ["default"], | ||
}, | ||
} | ||
|
||
if loggers: | ||
config["loggers"] = { | ||
logger_name: { | ||
"handlers": ["default"], | ||
"level": logger_config.level, | ||
"propagate": logger_config.propagate, | ||
} | ||
for logger_name, logger_config in loggers.items() | ||
} | ||
|
||
return config | ||
|
||
|
||
__all__ = [ | ||
"LogLevel", | ||
"LoggerConfig", | ||
"create_config", | ||
"initialize", | ||
] |
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,31 @@ | ||
import logging | ||
import typing | ||
|
||
_SECRETS: dict[str, str] = dict() # value: replace_value | ||
|
||
|
||
def register_secret(value: str, replace_value: str) -> None: | ||
_SECRETS[value] = replace_value | ||
|
||
|
||
class CustomFormatter(logging.Formatter): | ||
def __init__( | ||
self, | ||
*args: typing.Any, | ||
**kwargs: typing.Any, | ||
) -> None: | ||
super().__init__(*args, **kwargs) | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
log_message = super().format(record) | ||
|
||
for value, replace_value in _SECRETS.items(): | ||
log_message = log_message.replace(value, f"***{replace_value}***") | ||
|
||
return log_message | ||
|
||
|
||
__all__ = [ | ||
"CustomFormatter", | ||
"register_secret", | ||
] |