From 483b9d47c2ced34481da1666f0679d93a6b07166 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Mon, 11 Sep 2023 11:05:18 +0200 Subject: [PATCH] move tf_disable_interactive_logs to utils/logging and remove calls from ocrd_network, fix #1090 --- ocrd_network/ocrd_network/processing_worker.py | 4 ---- ocrd_network/ocrd_network/processor_server.py | 5 ----- ocrd_network/ocrd_network/utils.py | 12 ------------ ocrd_utils/ocrd_utils/__init__.py | 1 + ocrd_utils/ocrd_utils/logging.py | 14 ++++++++++++++ 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/ocrd_network/ocrd_network/processing_worker.py b/ocrd_network/ocrd_network/processing_worker.py index d8a7f01f7c..ce756b1412 100644 --- a/ocrd_network/ocrd_network/processing_worker.py +++ b/ocrd_network/ocrd_network/processing_worker.py @@ -36,14 +36,10 @@ from .utils import ( calculate_execution_time, post_to_callback_url, - tf_disable_interactive_logs, verify_database_uri, verify_and_parse_mq_uri ) -# TODO: Check this again when the logging is refactored -tf_disable_interactive_logs() - class ProcessingWorker: def __init__(self, rabbitmq_addr, mongodb_addr, processor_name, ocrd_tool: dict, processor_class=None) -> None: diff --git a/ocrd_network/ocrd_network/processor_server.py b/ocrd_network/ocrd_network/processor_server.py index b7abc6f148..eafd94eade 100644 --- a/ocrd_network/ocrd_network/processor_server.py +++ b/ocrd_network/ocrd_network/processor_server.py @@ -33,13 +33,8 @@ calculate_execution_time, post_to_callback_url, generate_id, - tf_disable_interactive_logs ) -# TODO: Check this again when the logging is refactored -tf_disable_interactive_logs() - - class ProcessorServer(FastAPI): def __init__(self, mongodb_addr: str, processor_name: str = "", processor_class=None): if not (processor_name or processor_class): diff --git a/ocrd_network/ocrd_network/utils.py b/ocrd_network/ocrd_network/utils.py index 4098ef0fa0..1dd69efdd0 100644 --- a/ocrd_network/ocrd_network/utils.py +++ b/ocrd_network/ocrd_network/utils.py @@ -34,18 +34,6 @@ def calculate_execution_time(start: datetime, end: datetime) -> int: return int((end - start).total_seconds() * 1000) -def tf_disable_interactive_logs(): - try: - # This env variable must be set before importing from Keras - environ['TF_CPP_MIN_LOG_LEVEL'] = '3' - from tensorflow.keras.utils import disable_interactive_logging - # Enabled interactive logging throws an exception - # due to a call of sys.stdout.flush() - disable_interactive_logging() - except Exception: - # Nothing should be handled here if TF is not available - pass - def generate_created_time() -> int: return int(datetime.utcnow().timestamp()) diff --git a/ocrd_utils/ocrd_utils/__init__.py b/ocrd_utils/ocrd_utils/__init__.py index 319bc852b3..f4bfd3ed69 100644 --- a/ocrd_utils/ocrd_utils/__init__.py +++ b/ocrd_utils/ocrd_utils/__init__.py @@ -157,6 +157,7 @@ ) from .logging import ( + tf_disable_interactive_logs, disableLogging, getLevelName, getLogger, diff --git a/ocrd_utils/ocrd_utils/logging.py b/ocrd_utils/ocrd_utils/logging.py index 421c05a781..4d8ddae722 100644 --- a/ocrd_utils/ocrd_utils/logging.py +++ b/ocrd_utils/ocrd_utils/logging.py @@ -41,6 +41,20 @@ 'FATAL': 'ERROR', } +def tf_disable_interactive_logs(): + try: + from os import environ + # This env variable must be set before importing from Keras + environ['TF_CPP_MIN_LOG_LEVEL'] = '3' + from tensorflow.keras.utils import disable_interactive_logging + # Enabled interactive logging throws an exception + # due to a call of sys.stdout.flush() + disable_interactive_logging() + except ImportError: + # Nothing should be handled here if TF is not available + pass + + class PropagationShyLogger(logging.Logger): def addHandler(self, hdlr):