From b8ae675b458bdd39d20ea4fc3fc4df16986f4e06 Mon Sep 17 00:00:00 2001 From: Elon masai <123534505+elonmasai7@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:16:27 +0300 Subject: [PATCH 1/2] Create logger.py --- logger.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 logger.py diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..f6941f2 --- /dev/null +++ b/logger.py @@ -0,0 +1,41 @@ +import logging +import os +from datetime import datetime + +# Configure logging +log_dir = "logs" +if not os.path.exists(log_dir): + os.makedirs(log_dir) + +logging.basicConfig( + filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"), + level=logging.DEBUG, + format="%(asctime)s - %(levelname)s - %(message)s", +) + +def main_task(): + try: + # Example task + logging.info("Starting the main task...") + + # Simulated error + if some_condition(): # Replace with actual logic + raise ValueError("Simulated error occurred.") + + logging.info("Main task completed successfully.") + + except ValueError as ve: + logging.error(f"ValueError: {ve}") + except Exception as e: + logging.error(f"Unexpected error: {e}") + finally: + logging.info("Task execution finished.") + +def some_condition(): + # Simulate an error condition + return True + +if __name__ == "__main__": + logging.info("Application started.") + main_task() + logging.info("Application exited.") From 87bf6baf6c96357bcc7fb8134422a778f03e3a38 Mon Sep 17 00:00:00 2001 From: Elon masai <123534505+elonmasai7@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:27:59 +0300 Subject: [PATCH 2/2] Update logger.py --- logger.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/logger.py b/logger.py index f6941f2..7fc0d61 100644 --- a/logger.py +++ b/logger.py @@ -14,28 +14,34 @@ logging.basicConfig( ) def main_task(): + """ + Main task execution function. Simulates a workflow and handles errors. + """ try: - # Example task logging.info("Starting the main task...") - - # Simulated error - if some_condition(): # Replace with actual logic + + # Simulated task and error condition + if some_condition(): raise ValueError("Simulated error occurred.") - + logging.info("Main task completed successfully.") - + except ValueError as ve: - logging.error(f"ValueError: {ve}") + logging.error(f"ValueError occurred: {ve}", exc_info=True) except Exception as e: - logging.error(f"Unexpected error: {e}") + logging.error(f"Unexpected error occurred: {e}", exc_info=True) finally: logging.info("Task execution finished.") def some_condition(): - # Simulate an error condition + """ + Simulates an error condition. Returns True to trigger an error. + Replace this logic with actual task conditions. + """ return True if __name__ == "__main__": + # Application workflow logging.info("Application started.") main_task() logging.info("Application exited.")