From f6a7724f4ed857efe3f2d42c1d1c21da86608bb0 Mon Sep 17 00:00:00 2001 From: Angelina Tingaeva Date: Thu, 30 Nov 2023 12:38:52 +0500 Subject: [PATCH] refactor(logging): create logs for show request and response after running mapping.py --- logging_config.py | 46 ++++++++++++++++++++++++++++++++++++++ mapping.py | 13 +++++++++-- mapping_logging.bak | 45 +++++++++++++++++++++++++++++++++++++ users.mapping.asana2ya.csv | 9 -------- 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 logging_config.py create mode 100644 mapping_logging.bak delete mode 100644 users.mapping.asana2ya.csv diff --git a/logging_config.py b/logging_config.py new file mode 100644 index 0000000..cbb1e3d --- /dev/null +++ b/logging_config.py @@ -0,0 +1,46 @@ +import logging +import logging.handlers + + +def setup_logging(): + """ Создание логгера """ + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + + """ Создание обработчика для записи логов в файл """ + + file_handler = logging.handlers.RotatingFileHandler( + filename='mapping.log', + maxBytes=1024*1024, + backupCount=5 + ) + file_handler.setLevel(logging.DEBUG) + + """ Создание обработчика для вывода логов на консоль """ + + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.DEBUG) + + """ Форматтер для вывода в файл """ + + file_formatter = logging.Formatter( + '%(asctime)s [%(levelname)s] [%(name)s] [%(request)s] [%(response)s] - %(message)s' + ) + + """ Форматтер для вывода на консоль """ + + console_formatter = logging.Formatter( + '%(asctime)s [%(levelname)s] [%(name)s] - %(message)s' + ) + + """ Добавление форматтеров в обработчики """ + + file_handler.setFormatter(file_formatter) + console_handler.setFormatter(console_formatter) + + """ Добавление обработчиков в логгер """ + + logger.addHandler(file_handler) + logger.addHandler(console_handler) + + return logger diff --git a/mapping.py b/mapping.py index 7bfb879..a3068dc 100644 --- a/mapping.py +++ b/mapping.py @@ -7,7 +7,11 @@ import logging import requests -logging.basicConfig(level=logging.DEBUG, filename='mapping.log', filemode='w') +# logging.basicConfig(level=logging.DEBUG, filename='mapping.log', filemode='w') + +from logging_config import setup_logging + +logger = setup_logging() base_dir = os.path.dirname(os.path.realpath(__file__)) asana_data_json = os.path.join(base_dir, './data/data_asana.json') @@ -102,12 +106,17 @@ def create_tasks_in_tracker(data): # 'memberships': task['memberships'], # 'parent': task['parent'], 'status': task['status'], + # 'completedAt': task['completedAt'] } + + logger.debug('Запрос: %s', json.dumps(payload)) + response = requests.post( base_url, headers=headers, data=json.dumps(payload), ) + if response.status_code == 201: print('Задача успешно создана в ЯндексТрекер') else: @@ -115,7 +124,7 @@ def create_tasks_in_tracker(data): 'Ошибка при создании задачи в ЯндексТрекер:', response.content, ) - + logger.debug('Ответ: %s', response.content) # asana_data = get_data_from_asana() file = open(asana_data_json, "r", encoding="utf8") diff --git a/mapping_logging.bak b/mapping_logging.bak new file mode 100644 index 0000000..6b0499a --- /dev/null +++ b/mapping_logging.bak @@ -0,0 +1,45 @@ +import logging +import requests +import textwrap + + +class HttpFormatter(logging.Formatter): + def _formatHeaders(self, d): + return '\n'.join(f'{k}: {v}' for k, v in d.items()) + + def formatMessage(self, record): + result = super().formatMessage(record) + if record.name == 'httplogger': + result += textwrap.dedent(''' + ---------------- request ---------------- + {req.method} {req.url} + {reqhdrs} + {req.body} + ---------------- response ---------------- + {res.status_code} {res.reason} {res.url} + {reshdrs} + {res.text} + ''').format( + req=record.req, + res=record.res, + reqhdrs=self._formatHeaders(record.req.headers), + reshdrs=self._formatHeaders(record.res.headers), + ) + return result + + +formatter = HttpFormatter('{asctime} {levelname} {name} {message}', style='{') +handler = logging.StreamHandler() +handler.setFormatter(formatter) +logging.basicConfig(level=logging.DEBUG, handlers=[handler]) +logger = logging.getLogger('httplogger') + + +def logRoundtrip(response, *args, **kwargs): + extra = {'req': response.request, 'res': response} + logger.debug('HTTP roundtrip', extra=extra) + + +session = requests.Session() +session.hooks['response'].append(logRoundtrip) +session.get('http://secariolabs.com') diff --git a/users.mapping.asana2ya.csv b/users.mapping.asana2ya.csv deleted file mode 100644 index b188993..0000000 --- a/users.mapping.asana2ya.csv +++ /dev/null @@ -1,9 +0,0 @@ -Name,ID -Diza,2934206415688 -Sergey Volodin,1202859655765863 -Никита,698025068994499 -Malkova,547756841476 -Сергей Анисов,1204278463134045 -Cyrill Gafarov,2495641457848 -Илья,2495641457856 -IrinaOs,1203071663735925