aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/dblogger.py
blob: 4b4e3a9def05d969d199dfea0f8dc63fce29b196 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from logging import LogRecord, StreamHandler


class DatabaseLogHandler(StreamHandler):
    """Logs entries into the database."""

    def emit(self, record: LogRecord) -> None:
        """Write the given `record` into the database."""
        # This import needs to be deferred due to Django's application
        # registry instantiation logic loading this handler before the
        # application is ready.
        from pydis_site.apps.api.models.log_entry import LogEntry

        entry = LogEntry(
            application='site',
            logger_name=record.name,
            level=record.levelname.lower(),
            module=record.module,
            line=record.lineno,
            message=self.format(record)
        )
        entry.save()