diff --git a/main/settings.py b/main/settings.py index 06af4e9..c6667e2 100644 --- a/main/settings.py +++ b/main/settings.py @@ -250,20 +250,14 @@ CACHES = { } } +LOKI_BASE_PUBLIC_URL = config('LOKI_BASE_PUBLIC_URL', default=None, cast=str) + LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { - # 'verbose': { - # 'format': '{levelname} AT: {asctime} LINE: {lineno} LOGGER: {name} FILE: {pathname} MESSAGE: {message}', - # 'style': '{', - # }, - # 'simple': { - # 'format': '{levelname} {message}', - # 'style': '{', - # }, 'loki': { - 'class': "utils.logs.LokiFormatter", # required + 'class': 'utils.logs.LokiFormatter', # required }, }, @@ -273,32 +267,23 @@ LOGGING = { 'class': 'utils.logs.LokiHandler', # Required 'formatter': 'loki', # Loki formatter. Required '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 - 'tags': {"app": "promotions", "env": "production"}, - # 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. + 'tags': {"app": "promotions", "env": "production"}, # 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. + }, + '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': { - 'django': { - 'handlers': ['loki'], + '': { + 'handlers': ['console', 'loki'], 'level': 'INFO', + 'propagate': True, }, - # 'root': { - # 'handlers': ['file'], - # 'level': 'DEBUG', - # 'propagate': True, - # }, }, } diff --git a/utils/logs/handlers.py b/utils/logs/handlers.py index a647255..284e6a4 100644 --- a/utils/logs/handlers.py +++ b/utils/logs/handlers.py @@ -27,7 +27,7 @@ class LokiHandler(logging.Handler): try: payload = self.formatter.format(record) - _push_message( + self._push_message( self._url, json=payload, timeout=self._timeout, auth=self._auth ) except requests.exceptions.ReadTimeout: @@ -41,7 +41,6 @@ class LokiHandler(logging.Handler): self.formatter = fmt def _push_message(self, *args, **kwargs): - print('hehe') if self._mode == "sync": return _push_message(*args, **kwargs)