avoid double logging of celery

This commit is contained in:
Phlogi
2025-08-26 16:59:14 +02:00
parent a6bdf966a4
commit 9f834a67bc
2 changed files with 17 additions and 2 deletions

View File

@@ -73,6 +73,12 @@ class CeleryManager:
logger.debug(f"Generated Celery command: {' '.join(command)}") logger.debug(f"Generated Celery command: {' '.join(command)}")
return command return command
def _get_worker_env(self):
# Inherit current environment, but set NO_CONSOLE_LOG=1 for subprocess
env = os.environ.copy()
env["NO_CONSOLE_LOG"] = "1"
return env
def _process_output_reader(self, stream, log_prefix, error=False): def _process_output_reader(self, stream, log_prefix, error=False):
logger.debug(f"Log reader thread started for {log_prefix}") logger.debug(f"Log reader thread started for {log_prefix}")
try: try:
@@ -141,6 +147,7 @@ class CeleryManager:
text=True, text=True,
bufsize=1, bufsize=1,
universal_newlines=True, universal_newlines=True,
env=self._get_worker_env(),
) )
self.download_log_thread_stdout = threading.Thread( self.download_log_thread_stdout = threading.Thread(
target=self._process_output_reader, target=self._process_output_reader,
@@ -177,6 +184,7 @@ class CeleryManager:
text=True, text=True,
bufsize=1, bufsize=1,
universal_newlines=True, universal_newlines=True,
env=self._get_worker_env(),
) )
self.utility_log_thread_stdout = threading.Thread( self.utility_log_thread_stdout = threading.Thread(
target=self._process_output_reader, target=self._process_output_reader,

View File

@@ -285,9 +285,16 @@ def setup_celery_logging(**kwargs):
""" """
This handler ensures Celery uses our application logging settings This handler ensures Celery uses our application logging settings
instead of its own. Prevents duplicate log configurations. instead of its own. Prevents duplicate log configurations.
Also disables console logging if NO_CONSOLE_LOG=1 is set in the environment.
""" """
# Using the root logger's handlers and level preserves our config root_logger = logging.getLogger()
return logging.getLogger() import os
if os.environ.get("NO_CONSOLE_LOG") == "1":
# Remove all StreamHandlers (console handlers) from the root logger
handlers_to_remove = [h for h in root_logger.handlers if isinstance(h, logging.StreamHandler)]
for h in handlers_to_remove:
root_logger.removeHandler(h)
return root_logger
# The initialization of a worker will log the worker configuration # The initialization of a worker will log the worker configuration