From b94eaee8334ac38e16f801e2f3ca4de8e9127cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sun, 11 Sep 2022 07:50:08 +0200 Subject: [PATCH] Setup logging for background tasks --- bookmarks/services/tasks.py | 14 +++++++------- siteroot/settings/prod.py | 29 +++++++++++++++++++++++++++++ supervisord.conf | 5 +++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/bookmarks/services/tasks.py b/bookmarks/services/tasks.py index 99a79da..16e4c1c 100644 --- a/bookmarks/services/tasks.py +++ b/bookmarks/services/tasks.py @@ -29,28 +29,28 @@ def create_web_archive_snapshot(user: User, bookmark: Bookmark, force_update: bo def _load_newest_snapshot(bookmark: Bookmark): try: - logger.debug(f'Load existing snapshot for bookmark. url={bookmark.url}') + logger.info(f'Load existing snapshot for bookmark. url={bookmark.url}') cdx_api = bookmarks.services.wayback.CustomWaybackMachineCDXServerAPI(bookmark.url) existing_snapshot = cdx_api.newest() if existing_snapshot: bookmark.web_archive_snapshot_url = existing_snapshot.archive_url bookmark.save() - logger.debug(f'Using newest snapshot. url={bookmark.url} from={existing_snapshot.datetime_timestamp}') + logger.info(f'Using newest snapshot. url={bookmark.url} from={existing_snapshot.datetime_timestamp}') except NoCDXRecordFound: - logger.error(f'Could not find any snapshots for bookmark. url={bookmark.url}') + logger.info(f'Could not find any snapshots for bookmark. url={bookmark.url}') except WaybackError as error: logger.error(f'Failed to load existing snapshot. url={bookmark.url}', exc_info=error) def _create_snapshot(bookmark: Bookmark): - logger.debug(f'Create new snapshot for bookmark. url={bookmark.url}...') + logger.info(f'Create new snapshot for bookmark. url={bookmark.url}...') archive = waybackpy.WaybackMachineSaveAPI(bookmark.url, DEFAULT_USER_AGENT, max_tries=1) archive.save() bookmark.web_archive_snapshot_url = archive.archive_url bookmark.save() - logger.debug(f'Successfully created new snapshot for bookmark:. url={bookmark.url}') + logger.info(f'Successfully created new snapshot for bookmark:. url={bookmark.url}') @background() @@ -71,8 +71,8 @@ def _create_web_archive_snapshot_task(bookmark_id: int, force_update: bool): except TooManyRequestsError: logger.error( f'Failed to create snapshot due to rate limiting, trying to load newest snapshot as fallback. url={bookmark.url}') - except WaybackError: - logger.error(f'Failed to create snapshot, trying to load newest snapshot as fallback. url={bookmark.url}') + except WaybackError as error: + logger.error(f'Failed to create snapshot, trying to load newest snapshot as fallback. url={bookmark.url}', exc_info=error) # Load the newest snapshot as fallback _load_newest_snapshot(bookmark) diff --git a/siteroot/settings/prod.py b/siteroot/settings/prod.py index 0f468b0..cbd08f0 100644 --- a/siteroot/settings/prod.py +++ b/siteroot/settings/prod.py @@ -28,6 +28,35 @@ if host_name: else: ALLOWED_HOSTS = ['*'] +# Logging +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'simple': { + 'format': '{asctime} {levelname} {message}', + 'style': '{', + }, + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'simple' + } + }, + 'root': { + 'handlers': ['console'], + 'level': 'WARN', + }, + 'loggers': { + 'bookmarks': { + 'level': 'INFO', + 'handlers': ['console'], + 'propagate': False, + } + } +} + # Import custom settings # noinspection PyUnresolvedReferences from .custom import * diff --git a/supervisord.conf b/supervisord.conf index bdbe724..df825a2 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -5,6 +5,7 @@ loglevel=info [program:jobs] user=www-data command=sh background-tasks-wrapper.sh -stdout_logfile=/dev/stdout -stdout_logfile_maxbytes=0 +stdout_logfile=background_tasks.log +stdout_logfile_maxbytes=10MB +stdout_logfile_backups=5 redirect_stderr=true