Skip to content

Commit 7557036

Browse files
Merge pull request #181 from fair-workflows/fix-retrospective-logging
Fix retrospective logging
2 parents 0efbe14 + b9eaf02 commit 7557036

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

fairworkflows/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from pathlib import Path
23

34
PACKAGE_DIR = Path(__file__).parent.absolute()
@@ -11,3 +12,5 @@
1112

1213
DUMMY_FAIRWORKFLOWS_URI = 'http://fairworkflows.org'
1314
IS_FAIRSTEP_RETURN_VALUE_PARAMETER_NAME='returns'
15+
16+
LOGGER = logging.getLogger('fairworkflows')

fairworkflows/fairstep.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import sys
23
import inspect
34
import typing
@@ -10,9 +11,11 @@
1011
from rdflib import RDF, RDFS, DCTERMS
1112

1213
from fairworkflows import namespaces, LinguisticSystem, LINGSYS_ENGLISH, LINGSYS_PYTHON
13-
from fairworkflows.config import DUMMY_FAIRWORKFLOWS_URI, IS_FAIRSTEP_RETURN_VALUE_PARAMETER_NAME
14+
from fairworkflows.config import DUMMY_FAIRWORKFLOWS_URI, IS_FAIRSTEP_RETURN_VALUE_PARAMETER_NAME, \
15+
LOGGER
1416
from fairworkflows.rdf_wrapper import RdfWrapper, replace_in_rdf
1517

18+
1619
class FairVariable:
1720
"""Represents a variable.
1821
@@ -425,16 +428,24 @@ def _modify_function(func):
425428
inputs = _extract_inputs_from_function(func, kwargs)
426429
outputs = _extract_outputs_from_function(func, kwargs)
427430

428-
func._fairstep = FairStep(uri='http://www.example.org/unpublished-'+func.__name__,
429-
label=label,
430-
description=description,
431-
is_pplan_step=is_pplan_step,
432-
is_manual_task=is_manual_task,
433-
is_script_task=is_script_task,
434-
language=LINGSYS_PYTHON,
435-
inputs=inputs,
436-
outputs=outputs)
437-
431+
fairstep = FairStep(uri='http://www.example.org/unpublished-'+func.__name__,
432+
label=label,
433+
description=description,
434+
is_pplan_step=is_pplan_step,
435+
is_manual_task=is_manual_task,
436+
is_script_task=is_script_task,
437+
language=LINGSYS_PYTHON,
438+
inputs=inputs,
439+
outputs=outputs)
440+
441+
@functools.wraps
442+
def _add_logging(func):
443+
def _wrapper(*func_args, **func_kwargs):
444+
LOGGER.info(f'Running step: {func.__name__}')
445+
return func(*func_args, **func_kwargs)
446+
return _wrapper
447+
func = _add_logging(func)
448+
func._fairstep = fairstep
438449
return noodles.schedule(func)
439450

440451
return _modify_function

fairworkflows/fairworkflow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from rdflib.tools.rdf2dot import rdf2dot
1717
from requests import HTTPError
1818

19+
from fairworkflows.config import LOGGER
1920
from fairworkflows import namespaces, LinguisticSystem, LINGSYS_ENGLISH, LINGSYS_PYTHON
2021
from fairworkflows.fairstep import FairStep
2122
from fairworkflows.rdf_wrapper import RdfWrapper
@@ -376,9 +377,8 @@ def execute(self, *args, **kwargs):
376377
formatter = logging.Formatter('%(asctime)s - %(message)s')
377378
log_handler.setFormatter(formatter)
378379

379-
logger = logging.getLogger('noodles')
380-
logger.setLevel(logging.INFO)
381-
logger.handlers = [log_handler]
380+
LOGGER.setLevel(logging.INFO)
381+
LOGGER.handlers = [log_handler]
382382
self.workflow_level_promise = noodles.workflow.from_call(
383383
noodles.get_workflow(self.workflow_level_promise).root_node.foo, args, kwargs, {})
384384
result = noodles.run_single(self.workflow_level_promise)

tests/test_fairworkflow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import rdflib
7+
from nanopub.definitions import DUMMY_NANOPUB_URI
78
from requests import HTTPError
89

910
from conftest import skip_if_nanopub_server_unavailable, read_rdf_test_resource
@@ -351,8 +352,13 @@ def my_workflow(in1, in2, in3):
351352

352353
result, prov = fw.execute(1, 4, 3)
353354
assert result == -66
355+
354356
assert isinstance(prov, Publication)
355357

358+
prov_log = str(list(prov.assertion.objects(rdflib.URIRef(f'{DUMMY_NANOPUB_URI}#retroprov'),
359+
rdflib.RDFS.label))[0])
360+
assert 'Running step: add' in prov_log
361+
356362
def test_workflow_complex_serialization(self):
357363
class OtherType:
358364
def __init__(self, message):

0 commit comments

Comments
 (0)