Skip to main content

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.

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_MacroValueso'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

 


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_MacroValueso'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.


This is the link to the posted idea:


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


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


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.

 


@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


@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.macroValuese'FME_MF_DIR'] # directory path
w = fme.macroValuesc'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 = wp:-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_MacroValueso'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