Skip to content

Commit

Permalink
(in Flow) added support for controlling logging and refactored a bit …
Browse files Browse the repository at this point in the history
…the log methods
  • Loading branch information
DinisCruz committed Oct 8, 2024
1 parent 900e2a0 commit 330fb5b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
30 changes: 17 additions & 13 deletions osbot_utils/helpers/flows/Flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ def config_logger(self):
if self.flow_config.log_to_console:
_.add_console_logger()


def debug(self, message):
self.logger.debug(message)

def create_flow(self):
self.set_flow_name()
self.debug(f"Created flow run '{self.f__flow_id()}' for flow '{self.f__flow_name()}'")
self.log_debug(f"Created flow run '{self.f__flow_id()}' for flow '{self.f__flow_name()}'")

def execute(self):
return self.execute_flow()
Expand All @@ -59,13 +55,13 @@ def execute_flow(self):
if self.flow_config.log_to_memory:
self.logger.add_memory_logger() # todo: move to method that does pre-execute tasks

self.debug(f"Executing flow run '{self.f__flow_id()}''")
self.log_debug(f"Executing flow run '{self.f__flow_id()}''")
try:
with Stdout() as stdout:
self.invoke_flow_target()
except Exception as error:
self.flow_error = error
self.logger.error(self.cformat.red(f"Error executing flow: {error}"))
self.log_error(self.cformat.red(f"Error executing flow: {error}"))

self.log_captured_stdout (stdout)
self.print_flow_return_value ()
Expand All @@ -86,9 +82,6 @@ def f__flow_name(self):
def captured_logs(self):
return ansis_to_texts(self.captured_exec_logs)

def info(self, message):
self.logger.info(message)


async def invoke_flow_target__thread(self, flow): # this is a REALLY important method which is used to pin the flow object to the call stack
return await flow.flow_target(*flow.flow_args, **flow.flow_kwargs) # which is then used by the Task.find_flow method to find it
Expand All @@ -103,12 +96,23 @@ def invoke_flow_target(self):
def log_captured_stdout(self, stdout):
for line in stdout.value().splitlines():
if line:
self.info(f_magenta(line))
self.log_info(f_magenta(line))
if self.flow_config.print_logs:
print()
print()
self.print_log_messages()

def log_debug(self, message):
if self.flow_config.logging_enabled:
self.logger.debug(message)

def log_error(self, message):
if self.flow_config.logging_enabled:
self.logger.error(message)

def log_info(self, message):
if self.flow_config.logging_enabled:
self.logger.info(message)

def log_messages(self):
return ansis_to_texts(self.log_messages_with_colors())
Expand All @@ -131,12 +135,12 @@ def print_log_messages(self, use_colors=True):

def print_flow_finished_message(self):
if self.flow_config.print_finished_message:
self.debug(f"Finished flow run '{self.f__flow_id()}''")
self.log_debug(f"Finished flow run '{self.f__flow_id()}''")

def print_flow_return_value(self):
if self.flow_config.print_none_return_value is False and self.flow_return_value is None:
return
self.debug(f"{f_dark_grey('Flow return value')}: {f_bold(self.flow_return_value)}")
self.log_debug(f"{f_dark_grey('Flow return value')}: {f_bold(self.flow_return_value)}")



Expand Down
19 changes: 11 additions & 8 deletions osbot_utils/helpers/flows/Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import inspect
import typing

from osbot_utils.utils.Misc import random_id, lower

from osbot_utils.utils.Misc import random_id, lower
from osbot_utils.helpers.Dependency_Manager import Dependency_Manager
from osbot_utils.helpers.flows.Flow__Events import flow_events
from osbot_utils.utils.Dev import pprint

from osbot_utils.testing.Stdout import Stdout
from osbot_utils.helpers.CFormat import CFormat, f_dark_grey, f_red, f_blue, f_bold
from osbot_utils.base_classes.Type_Safe import Type_Safe
Expand All @@ -30,6 +27,12 @@ class Task(Type_Safe):
task_error : Exception = None
raise_on_error : bool = True

def log_debug(self, message):
self.task_flow.log_debug(message)

def log_error(self, message):
self.task_flow.log_error(message)

def execute__sync(self):
self.execute__before()
self.execute__task_target__sync()
Expand All @@ -52,7 +55,7 @@ def execute__before(self):
self.task_id = self.random_task_id()

self.task_flow.executed_tasks.append(self)
self.task_flow.logger.debug(f"Executing task '{f_blue(self.task_name)}'")
self.log_debug(f"Executing task '{f_blue(self.task_name)}'")
dependency_manager = Dependency_Manager()
dependency_manager.add_dependency('this_task', self )
dependency_manager.add_dependency('this_flow', self.task_flow )
Expand Down Expand Up @@ -81,7 +84,7 @@ def execute__after(self):
self.print_task_return_value()

if self.task_error:
self.task_flow.logger.error(f_red(f"Error executing '{self.task_name}' task: {self.task_error}"))
self.log_error(f_red(f"Error executing '{self.task_name}' task: {self.task_error}"))
if self.raise_on_error:
raise Exception(f"'{self.task_name}' failed and task raise_on_error was set to True. Stopping flow execution")

Expand All @@ -102,13 +105,13 @@ def find_flow(self):

def print_task_finished_message(self):
if self.task_flow.flow_config.print_finished_message:
self.task_flow.logger.debug(f"Finished task '{f_blue(self.task_name)}'")
self.log_debug(f"Finished task '{f_blue(self.task_name)}'")

def print_task_return_value(self):
flow_config = self.task_flow.flow_config
if flow_config.print_none_return_value is False and self.task_return_value is None:
return
self.task_flow.logger.debug(f"{f_dark_grey('Task return value')}: {f_bold(self.task_return_value)}")
self.log_debug(f"{f_dark_grey('Task return value')}: {f_bold(self.task_return_value)}")


def random_task_id(self):
Expand Down
1 change: 1 addition & 0 deletions osbot_utils/helpers/flows/models/Flow__Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Flow__Config(Type_Safe):
add_task_to_self : bool = True
log_to_console : bool = False
log_to_memory : bool = True
logging_enabled : bool = True
print_logs : bool = False
print_none_return_value: bool = False
print_finished_message : bool = False
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/helpers/flows/test_decorator__flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def an_method_with_flow(name):
print('this is inside the flow!')

with Task().find_flow() as _:
_.info(f'hello {name}')
_.info('this is from an TASK that found the flow')
_.log_info(f'hello {name}')
_.log_info('this is from an TASK that found the flow')

return _.log_messages()

Expand Down

0 comments on commit 330fb5b

Please sign in to comment.