-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathstorages.py
39 lines (29 loc) · 1.16 KB
/
storages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
This module contains custom Django storages
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from django.conf import settings
from django.contrib.messages.storage.fallback import FallbackStorage
from django.utils.translation import override
if TYPE_CHECKING:
from django.utils.functional import Promise
logger = logging.getLogger(__name__)
class MessageLoggerStorage(FallbackStorage):
"""
Custom messages storage to add debug logging for all messages
Set the :setting:`django:MESSAGE_STORAGE` setting to the module path of this class to enable logging.
"""
def add(self, level: int, message: str | Promise, extra_tags: str = "") -> None:
"""
Add a mew message
:param level: The level of the message, see :ref:`message-level-constants`
:param message: The message
:param extra_tags: Additional level tags
"""
if settings.MESSAGE_LOGGING_ENABLED:
# Show logs in English if message was provided as lazy translated string
with override("en"):
logger.log(level, message)
super().add(level, message, extra_tags)