Skip to main content
Question

Date and Time in Logfile name

  • February 8, 2016
  • 8 replies
  • 348 views

erik_jan
Contributor
Forum|alt.badge.img+22

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.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

8 replies

davideagle
Contributor
Forum|alt.badge.img+22
  • Contributor
  • 578 replies
  • February 8, 2016

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

 


erik_jan
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • 2179 replies
  • February 8, 2016

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.


erik_jan
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • 2179 replies
  • February 8, 2016

rylanatsafe
Safer
Forum|alt.badge.img+14
  • Safer
  • 671 replies
  • May 1, 2017

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


takashi
Celebrity
  • 7843 replies
  • May 2, 2017

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.

 


fer_ruiz
Contributor
Forum|alt.badge.img+2
  • Contributor
  • 23 replies
  • October 29, 2018

@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
Celebrity
  • 7843 replies
  • October 29, 2018

@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

 


  • 1 reply
  • December 24, 2021

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