summaryrefslogtreecommitdiff
path: root/gitlab/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'gitlab/utils.py')
-rw-r--r--gitlab/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/gitlab/utils.py b/gitlab/utils.py
index f3d97f7..83642c0 100644
--- a/gitlab/utils.py
+++ b/gitlab/utils.py
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import logging
import pathlib
import traceback
import urllib.parse
@@ -31,6 +32,31 @@ class _StdoutStream:
print(chunk)
+class MaskingFormatter(logging.Formatter):
+ """A logging formatter that can mask credentials"""
+
+ def __init__(
+ self,
+ fmt: Optional[str] = logging.BASIC_FORMAT,
+ datefmt: Optional[str] = None,
+ style: str = "%", # should be Literal, but needs extensions for 3.7
+ validate: bool = True,
+ masked: Optional[str] = None,
+ ) -> None:
+ super().__init__(fmt, datefmt, style, validate) # type: ignore[arg-type]
+ self.masked = masked
+
+ def _filter(self, entry: str) -> str:
+ if not self.masked:
+ return entry
+
+ return entry.replace(self.masked, "[MASKED]")
+
+ def format(self, record: logging.LogRecord) -> str:
+ original = logging.Formatter.format(self, record)
+ return self._filter(original)
+
+
def response_content(
response: requests.Response,
streamed: bool,