How to add timestamps to log messages

Timestamps in console.log messages simplify troubleshooting. They can tell you when a particular function occurred or how long a certain step took to run, and they let you match log messages from one system with similar messages from other systems. By default, console.log messages from Flow Designer and Integration Builder scripts don't provide timestamp information...but we can change that. 

So, if your scripts are timing out or if you need to know exactly when your script sent a request to another system, read on.

Set the foundation

To add timestamp information to your log messages in the Flow Designer activity panel or the Integration Builder Activity Stream, you need to add a library that contains the moment.js script.

You can find the moment.js script on the moment.js github site, or our article on shared libraries has a link to the minified version.

Add a library in Flow Designer

  1. Go to the Flows tab in your workflow (or any workflow).
  2. Add a new library and paste the moment.js script into the editor.
  3. Click Save.

Add a shared library in the Integration Builder

Integration Builder libraries are not shared between workflows, so you need to add the library to each workflow where you want to use it.

  1. Go to the Integration Builder tab in the workflow where you want to use it.
  2. Add a new shared library and name it moment.js (if you use another name, make sure you change the require statement in your script to match).
  3. Copy and paste the moment.js script into the library editor and click Save.

Make it happen

After you add the library, you can reference it in your custom step scripts or your integration scripts to add a timestamp to your console.log messages:

// Add moment.js functionality to this script
var moment = require('moment.js');
// Write a log message that includes a timestamp
console.log( moment().format() + "\t A moment.js timestamp" );

For integration scripts, use the library name (in this example, moment.js) in the require statement; for custom steps in Flow Designer, use the reference name you set on the script tab.

By default, the moment.js timestamp format isn't that easy to read and doesn't display fractions of a second. You can change the way it displays the timestamp and make it add milliseconds:

//set the timestamp format
var tsFormat = 'YYYY/MM/DD HH:mm:ss.SSS Z';
// output looks like this: 2020/02/25 15:56:01.936 -08:00
console.log( moment().format( tsFormat ) + "\t Timestamp with milliseconds and timezone" );

Make it even better

That's still a clunky way to print out your messages though. There's a better way — at the top of your script, replace the chunk of code we added earlier with this:

// Add moment.js functionality to this script
var moment = require('moment.js');
// Print out log messages with timestamps including milliseconds and timezone
function tsLog( message ) { // output looks like this: 2020/02/25 15:56:01.936 -08:00
var tStamp = 'YYYY/MM/DD HH:mm:ss.SSS Z'; console.log( moment().format( tStamp ) + "\t" + message ); }

After this, instead of using "console.log('Your message here');" each time you want to include a timestamp, simply use:

tsLog( 'Great news!' );

This sample shows one option for displaying the time. You can change it to suit your needs a bit better; the moment.js website provides a great formatting reference.

Now you no longer need to wonder exactly when your messages were written!

Information originally created and supplied by Jeremy Brown.

 

 

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.