From 2a2b9563123818969bf9910bf19c826a7493d378 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:46:50 +0200 Subject: [PATCH 1/6] do not log into console during publishing --- .../tools/publisher/models/publish.py | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index a60ef69fac..c22f2d263c 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -1,6 +1,7 @@ import uuid import copy import inspect +import logging import traceback import collections from functools import partial @@ -23,6 +24,14 @@ PUBLISH_EVENT_SOURCE = "publisher.publish.model" PLUGIN_ORDER_OFFSET = 0.5 +class MessageHandler(logging.Handler): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.records = [] + + def emit(self, record): + self.records.append(record) + class PublishReportMaker: """Report for single publishing process. @@ -869,6 +878,9 @@ class PublishModel: # - pop the key after first collector using it would be safest option? self._publish_context.data["create_context"] = create_context publish_plugins = create_context.publish_plugins + for plugin in publish_plugins: + plugin.log.propagate = False + self._publish_plugins = publish_plugins self._publish_plugins_proxy = PublishPluginsProxy( publish_plugins @@ -1219,9 +1231,18 @@ class PublishModel: plugin: pyblish.api.Plugin, instance: pyblish.api.Instance ): - result = pyblish.plugin.process( - plugin, self._publish_context, instance - ) + handler = MessageHandler() + root = logging.getLogger() + plugin.log.addHandler(handler) + root.addHandler(handler) + try: + result = pyblish.plugin.process( + plugin, self._publish_context, instance + ) + result["records"] = handler.records + finally: + plugin.log.removeHandler(handler) + root.removeHandler(handler) exception = result.get("error") if exception: From 6099d4a5ae00b5c7a0ea20672eb61e8fe5b021ca Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:03:44 +0200 Subject: [PATCH 2/6] move propagate to publish iterator --- client/ayon_core/tools/publisher/models/publish.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index c22f2d263c..2041faab54 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -878,9 +878,6 @@ class PublishModel: # - pop the key after first collector using it would be safest option? self._publish_context.data["create_context"] = create_context publish_plugins = create_context.publish_plugins - for plugin in publish_plugins: - plugin.log.propagate = False - self._publish_plugins = publish_plugins self._publish_plugins_proxy = PublishPluginsProxy( publish_plugins @@ -1233,6 +1230,8 @@ class PublishModel: ): handler = MessageHandler() root = logging.getLogger() + + plugin.log.propagate = False plugin.log.addHandler(handler) root.addHandler(handler) try: @@ -1241,6 +1240,7 @@ class PublishModel: ) result["records"] = handler.records finally: + plugin.log.propagate = True plugin.log.removeHandler(handler) root.removeHandler(handler) From 4c809f9278364057252e11a833d5e1f524a13c8a Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:09:47 +0200 Subject: [PATCH 3/6] calculate message at the time of emit --- client/ayon_core/tools/publisher/models/publish.py | 1 + 1 file changed, 1 insertion(+) diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index 2041faab54..4c21a6d5b5 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -30,6 +30,7 @@ class MessageHandler(logging.Handler): self.records = [] def emit(self, record): + record.msg = record.getMessage() self.records.append(record) From f5c26e3d6e54e2941331ccb0cb23361ab3a8edb6 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:18:06 +0200 Subject: [PATCH 4/6] call getMessage in try block --- client/ayon_core/pipeline/context_tools.py | 5 ++++- client/ayon_core/tools/publisher/models/publish.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/pipeline/context_tools.py b/client/ayon_core/pipeline/context_tools.py index 8b72405048..5fb48cd79b 100644 --- a/client/ayon_core/pipeline/context_tools.py +++ b/client/ayon_core/pipeline/context_tools.py @@ -132,7 +132,10 @@ def install_host(host): def modified_emit(obj, record): """Method replacing `emit` in Pyblish's MessageHandler.""" - record.msg = record.getMessage() + try: + record.msg = record.getMessage() + except Exception: + record.msg = str(record.msg) obj.records.append(record) MessageHandler.emit = modified_emit diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index 4c21a6d5b5..e7e765c4d2 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -30,7 +30,10 @@ class MessageHandler(logging.Handler): self.records = [] def emit(self, record): - record.msg = record.getMessage() + try: + record.msg = record.getMessage() + except Exception: + record.msg = str(record.msg) self.records.append(record) From 57266dc7b660226fc6fddddecb4b1b9e89bfde1b Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:59:48 +0200 Subject: [PATCH 5/6] use single object of handler --- .../tools/publisher/models/publish.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index e7e765c4d2..6848e27bc4 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -29,6 +29,9 @@ class MessageHandler(logging.Handler): super().__init__(*args, **kwargs) self.records = [] + def clear_records(self): + self.records = [] + def emit(self, record): try: record.msg = record.getMessage() @@ -858,6 +861,8 @@ class PublishModel: self._default_iterator() ) + self._log_handler: MessageHandler = MessageHandler() + def reset(self): create_context = self._controller.get_create_context() self._publish_up_validation = False @@ -1232,21 +1237,20 @@ class PublishModel: plugin: pyblish.api.Plugin, instance: pyblish.api.Instance ): - handler = MessageHandler() root = logging.getLogger() - + self._log_handler.clear_records() plugin.log.propagate = False - plugin.log.addHandler(handler) - root.addHandler(handler) + plugin.log.addHandler(self._log_handler) + root.addHandler(self._log_handler) try: result = pyblish.plugin.process( plugin, self._publish_context, instance ) - result["records"] = handler.records + result["records"] = self._log_handler.records finally: plugin.log.propagate = True - plugin.log.removeHandler(handler) - root.removeHandler(handler) + plugin.log.removeHandler(self._log_handler) + root.removeHandler(self._log_handler) exception = result.get("error") if exception: From c315c96755c450849dc5af6ce25a36e7664822b6 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Thu, 3 Oct 2024 15:51:46 +0200 Subject: [PATCH 6/6] print logs based on env variable --- .../tools/publisher/models/publish.py | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/client/ayon_core/tools/publisher/models/publish.py b/client/ayon_core/tools/publisher/models/publish.py index ff20d8ec3e..6dfda38885 100644 --- a/client/ayon_core/tools/publisher/models/publish.py +++ b/client/ayon_core/tools/publisher/models/publish.py @@ -4,12 +4,14 @@ import inspect import logging import traceback import collections +from contextlib import contextmanager from functools import partial from typing import Optional, Dict, List, Union, Any, Iterable import arrow import pyblish.plugin +from ayon_core.lib import env_value_to_bool from ayon_core.pipeline import ( PublishValidationError, KnownPublishError, @@ -867,6 +869,10 @@ class PublishModel: def __init__(self, controller: AbstractPublisherBackend): self._controller = controller + self._log_to_console: bool = env_value_to_bool( + "AYON_PUBLISHER_PRINT_LOGS", default=False + ) + # Publishing should stop at validation stage self._publish_up_validation: bool = False self._publish_comment_is_set: bool = False @@ -917,7 +923,13 @@ class PublishModel: self._log_handler: MessageHandler = MessageHandler() def reset(self): + # Allow to change behavior during process lifetime + self._log_to_console = env_value_to_bool( + "AYON_PUBLISHER_PRINT_LOGS", default=False + ) + create_context = self._controller.get_create_context() + self._publish_up_validation = False self._publish_comment_is_set = False self._publish_has_started = False @@ -1285,25 +1297,38 @@ class PublishModel: self._set_progress(self._publish_max_progress) yield partial(self.stop_publish) + @contextmanager + def _log_manager(self, plugin: pyblish.api.Plugin): + root = logging.getLogger() + if not self._log_to_console: + plugin.log.propagate = False + plugin.log.addHandler(self._log_handler) + root.addHandler(self._log_handler) + + try: + if self._log_to_console: + yield None + else: + yield self._log_handler + + finally: + if not self._log_to_console: + plugin.log.propagate = True + plugin.log.removeHandler(self._log_handler) + root.removeHandler(self._log_handler) + self._log_handler.clear_records() + def _process_and_continue( self, plugin: pyblish.api.Plugin, instance: pyblish.api.Instance ): - root = logging.getLogger() - self._log_handler.clear_records() - plugin.log.propagate = False - plugin.log.addHandler(self._log_handler) - root.addHandler(self._log_handler) - try: + with self._log_manager(plugin) as log_handler: result = pyblish.plugin.process( plugin, self._publish_context, instance ) - result["records"] = self._log_handler.records - finally: - plugin.log.propagate = True - plugin.log.removeHandler(self._log_handler) - root.removeHandler(self._log_handler) + if log_handler is not None: + result["records"] = log_handler.records exception = result.get("error") if exception: