Skip to main content
Question

Date and Time in Logfile name

  • February 8, 2016
  • 8 replies
  • 310 views

erik_jan
Contributor
Forum|alt.badge.img+19

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

davideagle
Contributor
Forum|alt.badge.img+21
  • Contributor
  • 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+19
  • Author
  • Contributor
  • February 8, 2016
davideagle wrote:

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+19
  • Author
  • Contributor
  • February 8, 2016

rylanatsafe
Safer
Forum|alt.badge.img+13

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
Evangelist
  • May 2, 2017
rylanatsafe wrote:

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
  • 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
Evangelist
  • October 29, 2018
fer_ruiz wrote:

@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

 


  • December 24, 2021
davideagle wrote:

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings