logger.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. import os
  3. from datetime import datetime
  4. # Configure logging
  5. log_dir = "logs"
  6. if not os.path.exists(log_dir):
  7. os.makedirs(log_dir)
  8. logging.basicConfig(
  9. filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"),
  10. level=logging.DEBUG,
  11. format="%(asctime)s - %(levelname)s - %(message)s",
  12. )
  13. def main_task():
  14. try:
  15. # Example task
  16. logging.info("Starting the main task...")
  17. # Simulated error
  18. if some_condition(): # Replace with actual logic
  19. raise ValueError("Simulated error occurred.")
  20. logging.info("Main task completed successfully.")
  21. except ValueError as ve:
  22. logging.error(f"ValueError: {ve}")
  23. except Exception as e:
  24. logging.error(f"Unexpected error: {e}")
  25. finally:
  26. logging.info("Task execution finished.")
  27. def some_condition():
  28. # Simulate an error condition
  29. return True
  30. if __name__ == "__main__":
  31. logging.info("Application started.")
  32. main_task()
  33. logging.info("Application exited.")