refactor(logging): create logs for show request and response after running mapping.py
This commit is contained in:
parent
3c6221ad90
commit
f6a7724f4e
46
logging_config.py
Normal file
46
logging_config.py
Normal file
@ -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
|
13
mapping.py
13
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")
|
||||
|
45
mapping_logging.bak
Normal file
45
mapping_logging.bak
Normal file
@ -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')
|
@ -1,9 +0,0 @@
|
||||
Name,ID
|
||||
Diza,2934206415688
|
||||
Sergey Volodin,1202859655765863
|
||||
Никита,698025068994499
|
||||
Malkova,547756841476
|
||||
Сергей Анисов,1204278463134045
|
||||
Cyrill Gafarov,2495641457848
|
||||
Илья,2495641457856
|
||||
IrinaOs,1203071663735925
|
|
Loading…
Reference in New Issue
Block a user