Question

How can I create a log in PythonCaller and expose the contents in an attribute so that additional transformers can work off of that information?


I have a Python script that cleanses a specified document and then creates a logfile in a local directory. I am trying to embed this Python script to run out of FME via the PythonCaller, so instead of creating an independent log file, it will print out as an attribute and I can create additional functionality via FME based on the content. I was able to successfully run the data cleansing side, but I'm getting stuck with the logging side. That is, I was able to print characters inside the FME Validation Log, but I don't want that as I can't use additional transformers on that log content.

 

Can someone provide an example file or structure of how I can add this feature to my existing PythonCaller? I'm currently running FME(R) 2021.1.1.0 (20210730 - Build 21619 - WIN64) and Python 3.8.


3 replies

Userlevel 3
Badge +26

You can create an attribute using feature.setAttribute(). Documentation can be found here: fmeobjects.FMEFeature.setAttribute — Python FME API FME 2021.2 b21812 documentation (safe.com)

You can create an attribute using feature.setAttribute(). Documentation can be found here: fmeobjects.FMEFeature.setAttribute — Python FME API FME 2021.2 b21812 documentation (safe.com)

I am able to setAttributes on features that are entering the PythonCaller, but I'm specifically looking to run something like this and have any errors print out in the attribute from the logging module. Is that possible?

if State_List.size > 0:
    logging.info('Invalid State Value(s):')
    logging.info(State_List)
    print('Incorrect States')
    sys.exit(2)
if Pay_List.size > 0:
    logging.info('Invalid PayType Value(s):')
    logging.info(Pay_List)
    print('Incorrect Paytype')
    sys.exit(3)
if countexp > 0:
    logging.info('Negative EXP Checks:')
    logging.info(countexp)
    print('Negative Exp and Paytype Check')
    sys.exit(4)

 

Userlevel 4

I am able to setAttributes on features that are entering the PythonCaller, but I'm specifically looking to run something like this and have any errors print out in the attribute from the logging module. Is that possible?

if State_List.size > 0:
    logging.info('Invalid State Value(s):')
    logging.info(State_List)
    print('Incorrect States')
    sys.exit(2)
if Pay_List.size > 0:
    logging.info('Invalid PayType Value(s):')
    logging.info(Pay_List)
    print('Incorrect Paytype')
    sys.exit(3)
if countexp > 0:
    logging.info('Negative EXP Checks:')
    logging.info(countexp)
    print('Negative Exp and Paytype Check')
    sys.exit(4)

 

I've never done anything like it, but it might be possible to write a custom log handler that does it for you.

Generally speaking, the use of sys.exit() in the code above will complicate your goal, you may want to rewrite the logic to avoid it.

 

Reply