forked from LiveCarta/BookConverter
add main logger for the project
- add logger to consumer.py - handle exceptions and log them
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from threading import Thread, active_count
|
||||
@@ -10,34 +12,62 @@ from access import Access
|
||||
from book import Book
|
||||
|
||||
|
||||
def convert_book(book_id, access):
|
||||
def configure_file_logger(name, filename='logs/converter_log.log', filemode='w+', logging_level=logging.INFO,
|
||||
logging_format='%(asctime)s - %(message)s'):
|
||||
logger = logging.getLogger(name)
|
||||
|
||||
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
file_path = os.path.join(folder_path, filename)
|
||||
|
||||
file_handler = logging.FileHandler(file_path, mode=filemode)
|
||||
logger.addHandler(file_handler)
|
||||
file_format = logging.Formatter(fmt=logging_format)
|
||||
file_handler.setFormatter(file_format)
|
||||
logger.setLevel(logging_level)
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
def convert_book(book_id, access, logger):
|
||||
logger.info(f'Start processing book-{book_id}.')
|
||||
logging_format = '%(asctime)s - %(levelname)s - %(message)s'
|
||||
|
||||
book = Book(book_id, access)
|
||||
book.conversion(logging_format=logging_format)
|
||||
try:
|
||||
book = Book(book_id, access, main_logger=logger)
|
||||
book.conversion(logging_format=logging_format)
|
||||
except Exception as exc:
|
||||
raise exc
|
||||
|
||||
print('Book has been proceeded.')
|
||||
logger.info(f'Book-{book_id} has been proceeded.')
|
||||
# print('Book has been proceeded.')
|
||||
|
||||
|
||||
def callback(ch, method, properties, body, access):
|
||||
def callback(ch, method, properties, body, access, logger):
|
||||
print(f'Message: {body}.')
|
||||
logger.info(f'Message: {body}.')
|
||||
try:
|
||||
data = json.loads(body)
|
||||
params = {
|
||||
'book_id': data['id'],
|
||||
'access': access
|
||||
'access': access,
|
||||
'logger': logger
|
||||
}
|
||||
|
||||
thread = Thread(target=convert_book, kwargs=params)
|
||||
thread.start()
|
||||
print(f'Active threads: {active_count()}.')
|
||||
logging.log(logging.INFO, f'Active threads: {active_count()}.')
|
||||
# print(f'Active threads: {active_count()}.')
|
||||
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
if hasattr(exc, 'message'):
|
||||
logger.error(f'{sys.exc_info()[0]}: {exc.message}')
|
||||
else:
|
||||
logger.error(f'{sys.exc_info()[0]}')
|
||||
|
||||
finally:
|
||||
pass
|
||||
# thread.join()
|
||||
print('Waiting for the message...')
|
||||
# print('Waiting for the message...')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -56,15 +86,20 @@ if __name__ == '__main__':
|
||||
connection = pika.BlockingConnection(parameters)
|
||||
channel = connection.channel()
|
||||
|
||||
logger = configure_file_logger('consumer', logging_format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
try:
|
||||
channel.queue_declare(queue=conf_param['queue'], passive=True)
|
||||
except ValueError as exc:
|
||||
logger.log(logging.ERROR, 'Queue is not declared.')
|
||||
raise exc
|
||||
|
||||
acs = Access()
|
||||
channel.basic_consume(queue=conf_param['queue'],
|
||||
auto_ack=True,
|
||||
on_message_callback=partial(callback, access=acs))
|
||||
|
||||
on_message_callback=partial(callback, access=acs, logger=logger))
|
||||
logger.info('Connection has been established.')
|
||||
print('Waiting for messages...')
|
||||
logger.info('Waiting for messages...')
|
||||
|
||||
channel.start_consuming()
|
||||
|
||||
Reference in New Issue
Block a user