Simplify the log grouping logic
This commit is contained in:
1 parent
f57e23d76a
commit
3aa0120e3d
10 files changed
+82
-84
No files matched your search
+11
-5
@@ -57,7 +57,13 @@ class Evaluator():
|
||||
|
||||
self.__no_log_prefix = parser.parse_args('no_log_prefix',False) #as no_log_prefix is used to feedback parser error, it need to be defined first
|
||||
condition_name = parser.parse_args('condition_name',None,['log_prefix'])
|
||||
self.__trace_name = ', '.join(map(str, condition_name)) if isinstance(condition_name, list) else str(condition_name or 'condition')
|
||||
log_group = parser.parse_args('log_group',None)
|
||||
if log_group is not None:
|
||||
self.__log_group = str(log_group)
|
||||
elif isinstance(condition_name, list):
|
||||
self.__log_group = ', '.join(map(str, condition_name))
|
||||
else:
|
||||
self.__log_group = str(condition_name or 'condition')
|
||||
if condition_name:
|
||||
if isinstance(condition_name,list):
|
||||
for name in condition_name:
|
||||
@@ -163,17 +169,17 @@ class Evaluator():
|
||||
raise e
|
||||
|
||||
|
||||
def __log_group(self):
|
||||
def __log_group_scope(self):
|
||||
# SmartObject itself implements LoggerInterface, but many evaluators are
|
||||
# constructed with only the app as their API and no logger_interface kwarg.
|
||||
logger = self.__logger_interface or self.__appdaemon_api
|
||||
group_scope = getattr(logger, 'log_group', None)
|
||||
if callable(group_scope):
|
||||
return group_scope('smartcondition', self.__trace_name)
|
||||
return group_scope(self.__log_group)
|
||||
return nullcontext()
|
||||
|
||||
def evaluate(self,log = True):
|
||||
with self.__log_group():
|
||||
with self.__log_group_scope():
|
||||
try: result = self.__evaluate()
|
||||
except EvaluationException as e:
|
||||
self.__log(f"{e}",level = "ERROR")
|
||||
@@ -503,7 +509,7 @@ class Evaluator():
|
||||
self.__last_evaluation_result = new_result
|
||||
|
||||
def __trigger_callbacks(self,trigger_reason,triggered_from_activation = False):
|
||||
with self.__log_group():
|
||||
with self.__log_group_scope():
|
||||
return self.__trigger_callbacks_in_group(trigger_reason, triggered_from_activation)
|
||||
|
||||
def __trigger_callbacks_in_group(self,trigger_reason,triggered_from_activation = False):
|
||||
|
||||
Reference in new issue
Block a user