Skip to content

Commit ca9a06a

Browse files
authored
Merge pull request #993 from nofusscomputing/development
2 parents 0e8f6a0 + 020bba5 commit ca9a06a

File tree

13 files changed

+624
-58
lines changed

13 files changed

+624
-58
lines changed

app/api/auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import datetime
22

33
from django.conf import settings
4-
from logging import Logger
54

65
from rest_framework import exceptions
76
from rest_framework.authentication import BaseAuthentication, get_authorization_header
87

98
from api.models.tokens import AuthToken
109

10+
from centurion.logging import CenturionLogger
11+
1112
# scheme.py
1213
from drf_spectacular.extensions import OpenApiAuthenticationExtension
1314

1415
class TokenScheme(OpenApiAuthenticationExtension):
1516
target_class = "api.auth.TokenAuthentication"
1617
name = "TokenAuthentication"
1718

18-
def get_security_definition(self, auto_schema):
19+
def get_security_definition(self, auto_schema):
1920
return {
2021
"type": "apiKey",
2122
"in": "header",
@@ -52,7 +53,7 @@ def authenticate(self, request):
5253

5354
auth = get_authorization_header(request).split()
5455

55-
log: Logger = settings.CENTURION_LOG.getChild('authentication')
56+
log: CenturionLogger = settings.CENTURION_LOG.getChild('authentication')
5657

5758
if not auth:
5859
return None

app/api/viewsets/common/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import django
22
import importlib
3-
import logging
43
import rest_framework
54

65
from django.conf import settings
@@ -11,6 +10,7 @@
1110
from rest_framework.response import Response
1211
from rest_framework import viewsets
1312

13+
from centurion.logging import CenturionLogger
1414
from core.mixins.centurion import Centurion
1515

1616
from api.permissions.default import DefaultDenyPermission
@@ -615,7 +615,7 @@ def allowed_methods(self):
615615
_Optional_, if specified will be add to list view metadata
616616
"""
617617

618-
_log: logging.Logger = None
618+
_log: CenturionLogger = None
619619

620620
def get_log(self):
621621

app/centurion/logging.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import logging
2+
import logging.handlers
3+
4+
5+
6+
class CenturionLogger(logging.Logger):
7+
8+
EMERG = 90
9+
ALERT = 80
10+
CRITICAL = 70
11+
ERROR = 60
12+
WARNING = 50
13+
WARN = WARNING
14+
NOTICE = 40
15+
INFO = 30
16+
DEBUG = 20
17+
TRACE = 10
18+
NOTSET = logging.NOTSET
19+
20+
_levelToName = {
21+
EMERG: "EMERG",
22+
ALERT: "ALERT",
23+
CRITICAL: "CRITICAL",
24+
ERROR: "ERROR",
25+
WARNING: "WARNING",
26+
NOTICE: "NOTICE",
27+
INFO: "INFO",
28+
DEBUG: "DEBUG",
29+
TRACE: "TRACE",
30+
NOTSET: "NOTSET",
31+
}
32+
33+
_nameToLevel = {name: level for level, name in _levelToName.items()}
34+
35+
def __init__(self, name, level=DEBUG, address="/dev/log"):
36+
super().__init__(name, level)
37+
38+
# Attach SysLogHandler
39+
handler = logging.handlers.SysLogHandler(address=address)
40+
handler.priority_map.update({
41+
"EMERG": "emerg",
42+
"ALERT": "alert",
43+
"CRITICAL": "crit",
44+
"ERROR": "err",
45+
"WARNING": "warning",
46+
"NOTICE": "notice",
47+
"INFO": "info",
48+
"DEBUG": "debug",
49+
"TRACE": "debug",
50+
})
51+
52+
# Use a custom Formatter that maps numeric levels to names from this instance only
53+
formatter = logging.Formatter(fmt="%(levelname)s: %(message)s")
54+
formatter.format = lambda record: self._levelToName.get(record.levelno, record.levelno) + ": " + record.getMessage()
55+
handler.setFormatter(formatter)
56+
57+
self.addHandler(handler)
58+
59+
# --- Override base class methods ---
60+
def critical(self, msg, *args, **kwargs):
61+
if self.isEnabledFor(self.CRITICAL):
62+
self._log(self.CRITICAL, msg, args, **kwargs)
63+
64+
def error(self, msg, *args, **kwargs):
65+
if self.isEnabledFor(self.ERROR):
66+
self._log(self.ERROR, msg, args, **kwargs)
67+
68+
def warning(self, msg, *args, **kwargs):
69+
if self.isEnabledFor(self.WARNING):
70+
self._log(self.WARNING, msg, args, **kwargs)
71+
72+
def info(self, msg, *args, **kwargs):
73+
if self.isEnabledFor(self.INFO):
74+
self._log(self.INFO, msg, args, **kwargs)
75+
76+
def debug(self, msg, *args, **kwargs):
77+
if self.isEnabledFor(self.DEBUG):
78+
self._log(self.DEBUG, msg, args, **kwargs)
79+
80+
# --- Additional syslog levels ---
81+
def emergency(self, msg, *args, **kwargs):
82+
if self.isEnabledFor(self.EMERG):
83+
self._log(self.EMERG, msg, args, **kwargs)
84+
85+
def alert(self, msg, *args, **kwargs):
86+
if self.isEnabledFor(self.ALERT):
87+
self._log(self.ALERT, msg, args, **kwargs)
88+
89+
def notice(self, msg, *args, **kwargs):
90+
if self.isEnabledFor(self.NOTICE):
91+
self._log(self.NOTICE, msg, args, **kwargs)
92+
93+
def makeRecord(
94+
self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None
95+
):
96+
record = super().makeRecord(name, level, fn, lno, msg, args, exc_info, func, extra, sinfo)
97+
# Override record.levelname from instance _levelToName
98+
record.levelname = self._levelToName.get(record.levelno, str(record.levelno))
99+
return record

0 commit comments

Comments
 (0)