From 9cc4caf878a8cb9f86659de28c50aa919e57bbc5 Mon Sep 17 00:00:00 2001 From: Jorge Fabila Date: Tue, 17 Jun 2025 09:20:36 +0200 Subject: [PATCH] =?UTF-8?q?a=C3=B1adiendo=20variables=20modelos=20lineales?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client_cmd.py | 39 +++++++++++++++++++++++++++++++++------ server_cmd.py | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/client_cmd.py b/client_cmd.py index cad4cd3..98bbc79 100644 --- a/client_cmd.py +++ b/client_cmd.py @@ -54,21 +54,48 @@ # Create sandbox log file path sandbox_log_file = Path(os.path.join(config["sandbox_path"], "log_client.txt")) - # Set up the file handler + # Set up the file handler (writes to file) file_handler = logging.FileHandler(sandbox_log_file) file_handler.setLevel(logging.DEBUG) - - # Set up the console handler + # Set up the console handler (writes to Docker logs via stdout) console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(logging.DEBUG) - # Create a formatter and set it for both handlers + # Create a formatter for consistency formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) - # Add both handlers to the root logger - logging.basicConfig(level=logging.DEBUG, handlers=[file_handler, console_handler]) + # Get the root logger and configure it + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + logger.handlers = [] # Clear any default handlers + logger.addHandler(file_handler) + logger.addHandler(console_handler) + + # Redirect print() and sys.stdout/sys.stderr into logger + class StreamToLogger: + def __init__(self, logger, level): + self.logger = logger + self.level = level + + def write(self, message): + for line in message.rstrip().splitlines(): + self.logger.log(self.level, line.rstrip()) + + def flush(self): + pass + + # Create two sub-loggers + stdout_logger = logging.getLogger("STDOUT") + stderr_logger = logging.getLogger("STDERR") + + # Redirect standard output and error to logging + sys.stdout = StreamToLogger(stdout_logger, logging.INFO) + sys.stderr = StreamToLogger(stderr_logger, logging.ERROR) + + # Now you can use logging in both places + logging.debug("This will be logged to both the console and the file.") # Now you can use logging in both places logging.debug("This will be logged to both the console and the file.") diff --git a/server_cmd.py b/server_cmd.py index c6a63dd..0de88a3 100644 --- a/server_cmd.py +++ b/server_cmd.py @@ -66,25 +66,50 @@ def check_config(config): # Create sandbox log file path sandbox_log_file = Path(os.path.join(config["sandbox_path"], "log_server.txt")) - # Set up the file handler + # Set up the file handler (writes to file) file_handler = logging.FileHandler(sandbox_log_file) file_handler.setLevel(logging.DEBUG) - # Set up the console handler + # Set up the console handler (writes to Docker logs via stdout) console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(logging.DEBUG) - # Create a formatter and set it for both handlers + # Create a formatter for consistency formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) - # Add both handlers to the root logger - logging.basicConfig(level=logging.DEBUG, handlers=[file_handler, console_handler]) + # Get the root logger and configure it + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + logger.handlers = [] # Clear any default handlers + logger.addHandler(file_handler) + logger.addHandler(console_handler) + + # Redirect print() and sys.stdout/sys.stderr into logger + class StreamToLogger: + def __init__(self, logger, level): + self.logger = logger + self.level = level + + def write(self, message): + for line in message.rstrip().splitlines(): + self.logger.log(self.level, line.rstrip()) + + def flush(self): + pass + + # Create two sub-loggers + stdout_logger = logging.getLogger("STDOUT") + stderr_logger = logging.getLogger("STDERR") + + # Redirect standard output and error to logging + sys.stdout = StreamToLogger(stdout_logger, logging.INFO) + sys.stderr = StreamToLogger(stderr_logger, logging.ERROR) # Now you can use logging in both places logging.debug("This will be logged to both the console and the file.") - + # Your existing code continues here... # For example, the following logs will go to both stdout and file: logging.debug("Starting Flower server...")