import time
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def monitor_time(interval=1):
"""
Monitors the current time and logs it at specified intervals.
Args:
interval (int): The time interval in seconds between log updates. Defaults to 1.
"""
try:
while True:
now = time.time() # Get the current time in seconds since the epoch
logging.info(f"Current time: {now}") # Log the current time
time.sleep(interval) # Wait for the specified interval
except KeyboardInterrupt:
logging.info("Monitoring stopped by user.")
except Exception as e:
logging.error(f"An error occurred: {e}")
if __name__ == "__main__":
monitor_time() # Start monitoring with a 1-second interval
Add your comment