From 2805783bd02cd8f25de3ba200ccb1ea667b3be14 Mon Sep 17 00:00:00 2001 From: "redex-ai-karthickmj[bot]" <151735602+redex-ai-karthickmj[bot]@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:08:43 +0000 Subject: [PATCH] Add logging to QR code generator with file rotation --- QR_code_generator.py | 59 +++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/QR_code_generator.py b/QR_code_generator.py index 2a71157..5e7c04b 100644 --- a/QR_code_generator.py +++ b/QR_code_generator.py @@ -1,20 +1,39 @@ -import qrcode - -# Enter url of any website here. -input_URL = "https://www.google.com/" - -qr = qrcode.QRCode( - version=1, - error_correction=qrcode.constants.ERROR_CORRECT_L, - box_size=15, - border=4, -) - -qr.add_data(input_URL) -qr.make(fit=True) - -# convert into image -img = qr.make_image(fill_color="red", back_color="white") -img.save("url_qrcode.png") - -print(qr.data_list) +import qrcode +import logging +from logging.handlers import RotatingFileHandler + +# Set up logging +logging.basicConfig(level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[RotatingFileHandler('qr_code_generator.log', maxBytes=5000, backupCount=2), + logging.StreamHandler()]) + +logger = logging.getLogger(__name__) + +try: + logger.info('Starting QR code generation') + + # Enter url of any website here. + input_URL = "https://www.google.com/" + + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=15, + border=4, + ) + + qr.add_data(input_URL) + qr.make(fit=True) + + # convert into image + img = qr.make_image(fill_color="red", back_color="white") + img.save("url_qrcode.png") + + logger.info('QR code generated and saved successfully') +except Exception as e: + logger.error('An error occurred while generating the QR code: %s', e) +finally: + logger.info('QR code generation process completed') + +print(qr.data_list)