Question

Date and Time in Logfile name

  • 8 February 2016
  • 8 replies
  • 88 views

Userlevel 2
Badge +16

I know this can be done, but can not find the right way:

How can I add the current Date and Time in the name of the Log file?

Preferred format is: YYYYMMDDH24MI

Any help is appreciated.


8 replies

Badge +14

Hi Erik - Whilst I can't claim any credit for this, I can share the solution that @takashi helped me with some months ago. Hope it helps:

scripted-timestamp-parameter-v2.fmw

# Scripted (Python) Parameter Example
# Create log file path with time stamp.
import time, os
d = FME_MacroValues['FME_MF_DIR'] # directory path
w = FME_MacroValues['FME_MF_NAME'] # workspace name
t = time.strftime('%Y%m%dT%H%M%S') # time stamp
 
 
# If the workspace name ends with ".fmw", remove it.
if w.endswith('.fmw'):
    w = w[:-4]
 
 
# If the expected path conflicts with an existing file,
# append sequential number to avoid overwriting it.
base = '%s%s_%s' % (d, w, t)
path = '%s.log' % base
n = 1
while os.path.exists(path):
    path = '%s_%d.log' % (base, n)
    n += 1
return path

 

Userlevel 2
Badge +16

Hi Erik - Whilst I can't claim any credit for this, I can share the solution that @takashi helped me with some months ago. Hope it helps:

scripted-timestamp-parameter-v2.fmw

# Scripted (Python) Parameter Example
# Create log file path with time stamp.
import time, os
d = FME_MacroValues['FME_MF_DIR'] # directory path
w = FME_MacroValues['FME_MF_NAME'] # workspace name
t = time.strftime('%Y%m%dT%H%M%S') # time stamp
 
 
# If the workspace name ends with ".fmw", remove it.
if w.endswith('.fmw'):
    w = w[:-4]
 
 
# If the expected path conflicts with an existing file,
# append sequential number to avoid overwriting it.
base = '%s%s_%s' % (d, w, t)
path = '%s.log' % base
n = 1
while os.path.exists(path):
    path = '%s_%d.log' % (base, n)
    n += 1
return path

 

Thanks @1spatialdave and @takashi.

Exactly what I needed.

I will add an idea to make this a little easier for anybody not into Python programming.

Userlevel 2
Badge +16

This is the link to the posted idea:

https://knowledge.safe.com/content/idea/23151/add-option-for-datetime-in-log-file-name.html

Badge +11

Somewhat related, as I borrowed the Python code pasted here, this workspace has a Shutdown Python Script to rename a log file after the translation finishes - this works with FME Server.

 

 

Note: This was written for a customer with FME 2013 SP4 Build 13547 - your mileage may vary if using a different version...!

customlogname-pythonshutdown.fmw

Userlevel 2
Badge +17

Somewhat related, as I borrowed the Python code pasted here, this workspace has a Shutdown Python Script to rename a log file after the translation finishes - this works with FME Server.

 

 

Note: This was written for a customer with FME 2013 SP4 Build 13547 - your mileage may vary if using a different version...!

customlogname-pythonshutdown.fmw

Hi @RylanAtSafe, thanks for sharing the excellent solution.

 

I think the shutdown script works as expected when a single FME process runs the workspace, but it should be remarked that it could fail if two or more processes run the workspace simultaneously. For example, if the workspace was launched multiple times for different pairs of source/destination datasets through a WorkspaceRunner (Wait for Job Complete: No), all or some log files could conflict.

 

Note for FME 2017+: Since the "print" command used in the script has been changed to a function in Python 3.x, the argument must be surrounded with round brackets when you run it with Python 3.x interpreter.

 

Badge +1

@takashi

I want to create a folder which has the name of the worckbench that I am using and that the log files keep them there, with the same structure they have in the previous example. with the time, date ...... Thank you very much first

Userlevel 2
Badge +17

@takashi 

I want to create a folder which has the name of the worckbench that I am using and that the log files keep them there, with the same structure they have in the previous example. with the time, date ...... Thank you very much first

Try running this example.

 

# Scripted (Python) Parameter Example
# Create log file path with time stamp.
import fme, time, os
d = fme.macroValues['FME_MF_DIR'] # directory path
w = fme.macroValues['FME_MF_NAME'] # workspace name
t = time.strftime('%Y%m%dT%H%M%S') # time stamp

# If the workspace name ends with ".fmw", remove it.
if w.endswith('.fmw'):
    w = w[:-4]
    
# Create a new foloder which has the name of the workspace.
dir = os.path.join(d, w)
if not os.path.exists(dir):
    os.mkdir(dir)

# If the expected path conflicts with an existing file,
# append sequential number to avoid overwriting it.
base = os.path.join(dir, t)
path = '%s.log' % base
n = 1
while os.path.exists(path):
    path = '%s_%d.log' % (base, n)
    n += 1
return path

 

Hi Erik - Whilst I can't claim any credit for this, I can share the solution that @takashi helped me with some months ago. Hope it helps:

scripted-timestamp-parameter-v2.fmw

# Scripted (Python) Parameter Example
# Create log file path with time stamp.
import time, os
d = FME_MacroValues['FME_MF_DIR'] # directory path
w = FME_MacroValues['FME_MF_NAME'] # workspace name
t = time.strftime('%Y%m%dT%H%M%S') # time stamp
 
 
# If the workspace name ends with ".fmw", remove it.
if w.endswith('.fmw'):
    w = w[:-4]
 
 
# If the expected path conflicts with an existing file,
# append sequential number to avoid overwriting it.
base = '%s%s_%s' % (d, w, t)
path = '%s.log' % base
n = 1
while os.path.exists(path):
    path = '%s_%d.log' % (base, n)
    n += 1
return path

 

How to use this returned path in name of log file? I cannot see any option to set parameter while selecting a log file

Reply