diff --git a/camera-client/camera_monitor.py b/camera-client/camera_monitor.py index 69beb37..0f5e586 100644 --- a/camera-client/camera_monitor.py +++ b/camera-client/camera_monitor.py @@ -84,9 +84,29 @@ def load_config(config_path): except json.JSONDecodeError: raise Exception(f"Invalid JSON format in config file: {config_path}") -def setup_logging(): - logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') - return logging.getLogger('CameraMonitor') +def setup_logging(log_file='logs/camera_monitor.log'): + # logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') + # return logging.getLogger('CameraMonitor') + # Configure the logger + logger = logging.getLogger('CameraMonitor') + logger.setLevel(logging.INFO) + + # Create a TimedRotatingFileHandler for log rotation + file_handler = logging.handlers.TimedRotatingFileHandler( + log_file, when='midnight', backupCount=7 + ) # Keeps logs for 7 days + file_handler.suffix = '%Y-%m-%d' # Append date to log file names + + # Define the log message format + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + ) + file_handler.setFormatter(formatter) + + # Add the file handler to the logger + logger.addHandler(file_handler) + + return logger def main(): args = parse_command_line_args()