Skip to content

Commit 969bc89

Browse files
added DjangoHttp404LogFilter
1 parent 965b6eb commit 969bc89

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

python_logging_filters/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import logging
2+
import re
3+
4+
5+
class DjangoHttp404LogFilter(logging.Filter):
6+
"""Suppresses Django's default 'Not Found: ...' log messages.
7+
8+
See the Django docs how to attach filters to logging handlers via
9+
the LOGGING setting.
10+
"""
11+
PATTERN = re.compile("^Not Found:")
12+
13+
def filter(self, record: logging.LogRecord) -> bool:
14+
return (
15+
self.PATTERN.fullmatch(record.msg) is not None
16+
and record.levelno == logging.WARNING
17+
and record.name.startswith("django")
18+
)

0 commit comments

Comments
 (0)