healthchecks_healthchecks/hc/logs/__init__.py
2024-05-24 13:58:18 +03:00

30 lines
765 B
Python

from __future__ import annotations
import logging
import socket
from django.db import Error
FORMATTER = logging.Formatter()
class Handler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
# Import Record now not earlier, to avoid AppRegistryNotReady exception
from hc.logs.models import Record
traceback = ""
if record.exc_info:
traceback = FORMATTER.formatException(record.exc_info)
try:
Record.objects.create(
host=socket.gethostname(),
name=record.name,
level=record.levelno,
message=record.getMessage(),
traceback=traceback,
)
except Error as e:
print(e)