This commit is contained in:
Sayyid Hamid Mahdavi 2025-12-03 18:32:23 +03:30
parent d67f4f6069
commit 3443cba7bd
2 changed files with 15 additions and 31 deletions

View file

@ -250,20 +250,14 @@ CACHES = {
} }
} }
LOKI_BASE_PUBLIC_URL = config('LOKI_BASE_PUBLIC_URL', default=None, cast=str)
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'disable_existing_loggers': False, 'disable_existing_loggers': False,
'formatters': { 'formatters': {
# 'verbose': {
# 'format': '{levelname} AT: {asctime} LINE: {lineno} LOGGER: {name} FILE: {pathname} MESSAGE: {message}',
# 'style': '{',
# },
# 'simple': {
# 'format': '{levelname} {message}',
# 'style': '{',
# },
'loki': { 'loki': {
'class': "utils.logs.LokiFormatter", # required 'class': 'utils.logs.LokiFormatter', # required
}, },
}, },
@ -273,32 +267,23 @@ LOGGING = {
'class': 'utils.logs.LokiHandler', # Required 'class': 'utils.logs.LokiHandler', # Required
'formatter': 'loki', # Loki formatter. Required 'formatter': 'loki', # Loki formatter. Required
'timeout': 1, # Post request timeout, default is 0.5. Optional 'timeout': 1, # Post request timeout, default is 0.5. Optional
'url': 'https://loki.winsoo.ir/loki/api/v1/push', # Loki url. Defaults to localhost. Optional. 'url': f'{LOKI_BASE_PUBLIC_URL}/loki/api/v1/push', # Loki url. Defaults to localhost. Optional.
# 'auth': ("user", "password"), # Basic auth to authenticate with loki. Default is None (i.e. no auth). Optional # 'auth': ("user", "password"), # Basic auth to authenticate with loki. Default is None (i.e. no auth). Optional
'tags': {"app": "promotions", "env": "production"}, 'tags': {"app": "promotions", "env": "production"}, # Tags / Labels to attach to the log. Optional, but strongly encoraged to use.
# Tags / Labels to attach to the log. Optional, but strongly encoraged to use. 'mode': 'thread', # Push mode. Can be 'sync' or 'thread'. Sync is blocking, thread is non-blocking. Defaults to sync. Optional.
'mode': 'thread', },
# Push mode. Can be 'sync' or 'thread'. Sync is blocking, thread is non-blocking. Defaults to sync. Optional. 'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
# 'formatter': 'verbose',
}, },
# 'file': {
# 'level': 'DEBUG',
# 'class': 'logging.handlers.TimedRotatingFileHandler',
# 'filename': 'logs/app.log',
# 'formatter': 'verbose',
# "when": 'D',
# "backupCount": 5,
# },
}, },
'loggers': { 'loggers': {
'django': { '': {
'handlers': ['loki'], 'handlers': ['console', 'loki'],
'level': 'INFO', 'level': 'INFO',
'propagate': True,
}, },
# 'root': {
# 'handlers': ['file'],
# 'level': 'DEBUG',
# 'propagate': True,
# },
}, },
} }

View file

@ -27,7 +27,7 @@ class LokiHandler(logging.Handler):
try: try:
payload = self.formatter.format(record) payload = self.formatter.format(record)
_push_message( self._push_message(
self._url, json=payload, timeout=self._timeout, auth=self._auth self._url, json=payload, timeout=self._timeout, auth=self._auth
) )
except requests.exceptions.ReadTimeout: except requests.exceptions.ReadTimeout:
@ -41,7 +41,6 @@ class LokiHandler(logging.Handler):
self.formatter = fmt self.formatter = fmt
def _push_message(self, *args, **kwargs): def _push_message(self, *args, **kwargs):
print('hehe')
if self._mode == "sync": if self._mode == "sync":
return _push_message(*args, **kwargs) return _push_message(*args, **kwargs)