2013-10-27

Specify Log File Name

By default, the log file name will be same as the workspace name, except its extension. If we need to change the log file name, can specify a preferable name to a Workspace Parameter named "Log File". For example:
$(FME_MF_DIR)MyLogFile.log

FME_MF_DIR is a macro (system parameter) name, indicates the current directory path. "./MyLogFile.log" can be also specified here.

But the log file name specified with such a way is fixed, so the file will be overwritten when the workspace is executed multiple times repeatedly. When we need to preserve every log file with unique name for each run, how can we do that?

1. Rename in the Shutdown Script
One possible approach is to rename the log file name in the Shutdown Script.
Note:This approach cannot be applied in a certain situation as mentioned later.
The log file has been closed before shutdown process; we can access the saved log file path via a global variable named "FME_LogFileName" in the script.
This Shutdown Python Script appends a timestamp (and sequential number when the timestamp also conflicts) to the log file name.
-----
import os, time, re
src = FME_LogFileName
if src and os.path.exists(src):
    stamp = time.strftime('%Y%m%d_%H%M%S')
    name = '%s_%s' % (re.sub('^(.+)\\.log$', '\\1', src), stamp)
    base = name
    count = 1
    while os.path.exists('%s.log' % name):
        name = '%s_%d' % (base, count)
        count += 1
    os.rename(src, '%s.log' % name)
-----

2. Define a Scripted Parameter
If the translation process is executed always one by one, the renaming approach works well. However, if two or more processes for the same workspace will be executed simultaneously - e.g. when the WorkspaceRunner specified "No" to "Wait for Job Complete" runs the workspace several times, we cannot get the expected result. Because the original log file name conflicts between the processes.
In such a situation, we should create a unique log file name as a scripted parameter, and link the Workspace Parameter "Log File" to it. The script can be like this, for example.
-----
import os, time, re
src = FME_MacroValues['FME_MF_DIR'] + FME_MacroValues['FME_MF_NAME']
stamp = time.strftime('%Y%m%d_%H%M%S')
name = '%s_%s' % (re.sub('^(.+)\\.fmw$', '\\1', src), stamp)
base = name
count = 1
while os.path.exists('%s.log' % name):
    name = '%s_%d' % (base, count)
    count += 1
return '%s.log' % name
-----

Appendix: Tcl Script Examples
The following Tcl scripts work as well as the Python examples above.

Shutdown Tcl Script: Rename the Log File Name
------
set src $FME_LogFileName
if {[string length $src] && [file exists $src]} {
  set stamp [clock format [clock seconds] -format {%Y%m%d_%H%M%S}]
  set name [format "%s_%s" [regsub {^(.+)\.log$} $src {\1}] $stamp]
  set base $name
  for {set count 1} {[file exists [format "%s.log" $name]]} {incr count} {
    set name [format "%s_%d" $base $count]
  }
  file rename $src [format "%s.log" $name]
}
------

Scripted (Tcl) Parameter: Create a Unique Log File Name
------
set src "$FME_MacroValues(FME_MF_DIR)$FME_MacroValues(FME_MF_NAME)"
set stamp [clock format [clock seconds] -format {%Y%m%d_%H%M%S}]
set name [format "%s_%s" [regsub {^(.+)\.fmw$} $src {\1}] $stamp]
set base $name
for {set count 1} {[file exists [format "%s.log" $name]]} {incr count} {
  set name [format "%s_%d" $base $count]
}
return [format "%s.log" $name]
------

See another example here:
> Community: FME -> log file

No comments:

Post a Comment