Logging with Python scripts

Liz Sanderson
Liz Sanderson

FME Version

Introduction

Writing messages to the FME logfile in a Python script can be tricky. If you use a simple print "message", the line will be printed in the log window, but NOT saved in the logfile.

The procedure for writing to the logfile differs based on where you are running the script.
 

PythonCaller Transformer

In a PythonCaller (or PythonCreator) transformer, messages are added to the logfile with the FMELogFile object, which is part of the fmeobjects. You need to create a logger object:

logger = fmeobjects.FMELogFile() 

and then you will need to send messages to the logger:

logger.logMessageString("Hello I am Logging Now")


Shutdown Script

Logging in a shutdown script is a bit trickier. At this point in the lifecycle of the workspace, the workspace has been disconnected from the fme process, so the FMELogfile object no longer works. Instead you will need to get the location of the logfile on disk with FME_LogFileName parameter and use Python's built-in file functionality to add lines to the logfile, for example:

logger = open(FME_LogFileName,'a')
logger.write("wow - this message will actually be written to log file")
logger.close()

Note: Lines logged in this way will NOT appear in the log window in workbench, but will be appended to the end of the logfile on disk. If you want something to appear in both places you will need to write it to the logfile, as well like this:

print "wow - this message will only go to log window"


Examples of logging in a PythonCaller and in a shutdown script can be found in the attached workspace.

Note: If you are running Python scripts on FME Server, any error messages produced by the interpreter will be written out to the Process Monitor Engine log file rather than the Workspace log. This file can be found in <FMEServerSystemShare>\Logs\Engine\Current\.

For a complete overview of using Python with FME please see the article Tutorial: Python and FME Basics

Was this article helpful?

Comments

1 comment

  • Comment author
    tcrossman

    Very helpful article.  What about writing messages to the log file from a Scripted Parameter, though?  I have tried both log file objects and print statements in a scripted parameter.  Both write to the log window, but neither writes to the log file.

    0

Please sign in to leave a comment.