From a103df8fe1361758e30e7ed1ee665316c0e731a5 Mon Sep 17 00:00:00 2001 From: shirshasa Date: Thu, 20 Aug 2020 16:26:43 +0300 Subject: [PATCH] update logging errors --- src/consumer.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/consumer.py b/src/consumer.py index 1d2e78b..2c57c7f 100644 --- a/src/consumer.py +++ b/src/consumer.py @@ -41,7 +41,7 @@ def convert_book(book_id, access, logger, libra_locker): raise exc logger.info(f'Book-{book_id} has been proceeded.') - # print('Book has been proceeded.') + print('Book has been proceeded.') def callback(ch, method, properties, body, logger, libra_locker): @@ -67,12 +67,10 @@ def callback(ch, method, properties, body, logger, libra_locker): if hasattr(exc, 'message'): logger.error(f'{sys.exc_info()[0]}: {exc.message}') else: - logger.error(f'{sys.exc_info()[0]}') + logger.error(f'{sys.exc_info()[0]}: {str(exc)}') finally: pass - # thread.join() - # print('Waiting for the message...') def local_convert_book(filename, locker): @@ -107,21 +105,24 @@ def local_run(books): def server_run(): + logger = configure_file_logger('consumer', logging_format='%(asctime)s - %(levelname)s - %(message)s') + folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - # config_path = Path(os.path.join(folder_path, "config/config.json")) config_path = Path(os.path.join(folder_path, "config/queue_config.json")) with open(config_path, "r") as f: conf_param = json.load(f) host = conf_param.get('host') or pika.ConnectionParameters().DEFAULT_HOST port = conf_param.get('port') or pika.ConnectionParameters().DEFAULT_PORT - - credentials = pika.PlainCredentials(username=conf_param['username'], password=conf_param['password']) - parameters = pika.ConnectionParameters(host=host, port=port, credentials=credentials) - connection = pika.BlockingConnection(parameters) - channel = connection.channel() - - logger = configure_file_logger('consumer', logging_format='%(asctime)s - %(levelname)s - %(message)s') + channel = None + try: + credentials = pika.PlainCredentials(username=conf_param['username'], password=conf_param['password']) + parameters = pika.ConnectionParameters(host=host, port=port, credentials=credentials) + connection = pika.BlockingConnection(parameters) + channel = connection.channel() + except Exception as exc: + logger.log(logging.ERROR, f'Problems with queue connection.\n' + str(exc)) + raise exc try: channel.queue_declare(queue=conf_param['queue'], durable=True, arguments={'x-max-priority': 10})