From 08cc965af46b2f4d925bb0dcd3b78a057aed4037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Sat, 8 Aug 2020 11:00:28 +0200 Subject: [PATCH] feat(logs): formatte joliement la sortie --- drupal2spip_lal/base/convert.py | 12 +++++- drupal2spip_lal/settings/base.py | 57 ++++++++++++++++++++++++++ drupal2spip_lal/settings/production.py | 51 +---------------------- requirements/base.txt | 4 ++ 4 files changed, 72 insertions(+), 52 deletions(-) diff --git a/drupal2spip_lal/base/convert.py b/drupal2spip_lal/base/convert.py index b021110..832c823 100644 --- a/drupal2spip_lal/base/convert.py +++ b/drupal2spip_lal/base/convert.py @@ -1,3 +1,4 @@ +import logging import os import re from datetime import datetime @@ -20,6 +21,9 @@ from drupal2spip_lal.spip import models as spip # - convertir en format SPIP ? +logger = logging.getLogger('drupal2spip_lal') + + def convert_timestamp(timestamp): return make_aware(datetime.fromtimestamp(timestamp)) @@ -159,14 +163,18 @@ def convert_node(node, options): ] spip.Urls.objects.bulk_create(urls) - print('Article {} created from node {}.'.format(article.pk, node.pk)) + logger.info( + 'Article {} created from node {}.'.format(article.pk, node.pk) + ) elif update: article = spip.Articles( pk=spip_urls.last().id_objet, **article_attributes ) article.save() - print('Article {} updated from node {}.'.format(article.pk, node.pk)) + logger.info( + 'Article {} updated from node {}.'.format(article.pk, node.pk) + ) if article: user_attributes = { diff --git a/drupal2spip_lal/settings/base.py b/drupal2spip_lal/settings/base.py index 6d3100a..755fa92 100644 --- a/drupal2spip_lal/settings/base.py +++ b/drupal2spip_lal/settings/base.py @@ -248,6 +248,63 @@ SESSION_COOKIE_PATH = APP_LOCATION # https://docs.djangoproject.com/en/stable/ref/settings/#csrf-cookie-path CSRF_COOKIE_PATH = APP_LOCATION +# LOGGING +# ------------------------------------------------------------------------------ +# https://docs.djangoproject.com/en/stable/topics/logging/ +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'verbose': { + 'format': '%(asctime)s - %(levelname)s - %(module)s: %(message)s', + }, + 'colored': { + '()': 'colorlog.ColoredFormatter', + 'format': ( + '%(log_color)s%(asctime)s - ' + '%(levelname)s - %(name)s: %(message)s' + ), + 'log_colors': { + 'DEBUG': 'bold_cyan', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'bold_red', + }, + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'colored', + }, + 'file': { + 'level': 'INFO', + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filename': var_dir('log/drupal2spip_lal.log'), + 'formatter': 'verbose', + 'when': 'midnight', + 'interval': 1, + 'backupCount': 30, + }, + }, + 'loggers': { + 'django': { + 'level': 'WARNING', + 'handlers': ['file', 'console'], + 'propagate': True, + }, + 'drupal2spip_lal': { + 'level': 'DEBUG', + 'handlers': ['file', 'console'], + 'propagate': True, + }, + }, +} +if not os.path.isdir(var_dir('log')): + os.mkdir(var_dir('log'), mode=0o750) + # ------------------------------------------------------------------------------ # APPLICATION AND 3RD PARTY LIBRARY SETTINGS # ------------------------------------------------------------------------------ diff --git a/drupal2spip_lal/settings/production.py b/drupal2spip_lal/settings/production.py index 906b0f8..bb1eebe 100644 --- a/drupal2spip_lal/settings/production.py +++ b/drupal2spip_lal/settings/production.py @@ -6,12 +6,10 @@ Production settings. - load secret key from environment variables - set other production configurations """ -import os - from django.core.exceptions import ImproperlyConfigured from .base import * # noqa -from .base import TEMPLATES, env, var_dir +from .base import TEMPLATES, env # CONFIGURATION VALIDATION # ------------------------------------------------------------------------------ @@ -54,53 +52,6 @@ TEMPLATES[0]['OPTIONS']['loaders'] = [ ) ] -# LOGGING -# ------------------------------------------------------------------------------ -# https://docs.djangoproject.com/en/stable/topics/logging/ -LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'verbose': { - 'format': '%(asctime)s - %(levelname)s - %(module)s: %(message)s' - } - }, - 'handlers': { - 'mail_admins': { - 'level': 'ERROR', - 'class': 'django.utils.log.AdminEmailHandler', - }, - 'file': { - 'level': 'DEBUG', - 'class': 'logging.handlers.TimedRotatingFileHandler', - 'filename': var_dir('log/drupal2spip_lal.log'), - 'formatter': 'verbose', - 'when': 'midnight', - 'interval': 1, - 'backupCount': 30, - }, - }, - 'loggers': { - 'django': { - 'level': 'WARNING', - 'handlers': ['file'], - 'propagate': True, - }, - 'django.request': { - 'level': 'WARNING', - 'handlers': ['file', 'mail_admins'], - 'propagate': True, - }, - 'drupal2spip_lal': { - 'level': 'INFO', - 'handlers': ['file', 'mail_admins'], - 'propagate': True, - }, - }, -} -if not os.path.isdir(var_dir('log')): - os.mkdir(var_dir('log'), mode=0o750) - # ------------------------------------------------------------------------------ # APPLICATION AND 3RD PARTY LIBRARY SETTINGS # ------------------------------------------------------------------------------ diff --git a/requirements/base.txt b/requirements/base.txt index fdf3dca..680b91a 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -11,3 +11,7 @@ phpserialize # ------------------------------------------------------------------------------ request beautifulsoup4 + +# Logs +# ------------------------------------------------------------------------------ +colorlog