1. import time
  2. import logging
  3. # Configure logging
  4. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  5. def monitor_time(interval=1):
  6. """
  7. Monitors the current time and logs it at specified intervals.
  8. Args:
  9. interval (int): The time interval in seconds between log updates. Defaults to 1.
  10. """
  11. try:
  12. while True:
  13. now = time.time() # Get the current time in seconds since the epoch
  14. logging.info(f"Current time: {now}") # Log the current time
  15. time.sleep(interval) # Wait for the specified interval
  16. except KeyboardInterrupt:
  17. logging.info("Monitoring stopped by user.")
  18. except Exception as e:
  19. logging.error(f"An error occurred: {e}")
  20. if __name__ == "__main__":
  21. monitor_time() # Start monitoring with a 1-second interval

Add your comment