Logger Module

Viewing 4 reply threads
  • Author
    Posts
  • July 21, 2019 at 10:35 AM #35991

    Sam Moffatt
    Participant

    As I’ve built field scripts, one of the challenges ended up being figuring out which field is running at which time when you have multiple fields and potentially multiple forms. Below is a logger module that I use to help me keep track with timestamps and headers. You can import this script and a few others by using my “Script Manager” form.

    Here’s a sample output showing re-entrant behaviour (the script field triggered itself to run):

    ==================================================================
    Start "Shipping/Delivery Autopropagate (Orders)" script execution at Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)
    ==================================================================
    
    Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)	One shipment detected, validating shipment data
    Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)	shipping date set in shipment but not in order, updating order
    ==================================================================
    Start "Shipping/Delivery Autopropagate (Orders)" script execution at Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)
    ==================================================================
    
    Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)	One shipment detected, validating shipment data
    
    ==================================================================
    End "Shipping/Delivery Autopropagate (Orders)" script execution at Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)
    ==================================================================
    
    ==================================================================
    End "Shipping/Delivery Autopropagate (Orders)" script execution at Sun Jul 21 2019 10:22:46 GMT-0700 (PDT)
    ==================================================================
    

    Screenshot with less wrapping:

    Console screenshot

    There are two parts that I find useful, the first is the separators which look like this:

    logger.consoleHeader('Shipping/Delivery Autopropagate', 'Orders');
    var result = propagateDates();
    logger.consoleFooter('Shipping/Delivery Autopropagate', Orders');
    result;
    

    This creates the header and footer lines in the format you see in the example above. The other part is the logger lines:

    logger.logMessage("One shipment detected, validating shipment data");
    

    This then adds the timestamp and also stores the log messages temporarily.

    Because I don’t want to copy and paste this all of the time, I have a form called “Script Manager” where I create form scripts that I put in like this (creatively called “Logger”). This means that I can import this with a single line when I want to use it:

    document.getFormNamed('Script Manager').runScriptNamed('Logger');
    

    Here’s the module:

    {
    // ========== Logger Start ========== //
    // NAME: Logger
    // VERSION: 1.0.2
    /**
     * Logger module provides some extra utility functions for logging data.
     * This adds some useful functions for integrating with the console and
     *   for building script fields.
     */
    if (logger == undefined)
    
    var logger = (function() {
    	var logdata = "";
    	return {
    		/**
    		 * Log a message
    		 *
    		 * Takes a <code>message</code> and adds it to the internal <code>logdata</code> variable
    		 *   and also logs it to the console with a timestamp.
    		 */
    		logMessage: function(message)
    			{
    				logdata += message + "\r\n";
    				console.log(new Date() + '\t' + message)
    			},
    
    		/**
    		 * Log an error
    		 *
    		 * Takes a <code>message</code> and <code>error</code> object and then formats into an error
    		 *   via <code>logMessage</code>.
    		 */
    		logError: function(error)
    			{
    				this.logMessage(<code>Caught error: &quot;${error}&quot; at ${error.line}, ${error.column}</code>);
    			},
    
    		/**
    		 * Get the internal log buffer
    		 *
    		 * Returns the raw log buffer to output elsewhere.
    		 */
    		getLog: function()
    			{
    				return logdata;
    			},
    
    		/**
    		 * Clear the internal log buffer
    		 *
    		 * Resets the buffer to an empty string.
    		 */
    		clearLog: function()
    			{
    				logdata = "";
    			},
    
    		/**
    		 * Utility function to print a header when the script starts with optional form name
    		 */
    		consoleHeader: function(scriptName, formName = "")
    			{
    				var label = scriptName + (formName ? <code>(${formName})</code> : '');
    				console.log('==================================================================');
    				console.log('Start "' + label + '" script execution at ' + new Date());
    				console.log('==================================================================\n');
    			},
    
    		/**
    		 * Utility function to print a footer when the script ends with optional form name
    		 */
    		consoleFooter: function(scriptName, formName = "")
    			{
    				var label = scriptName + (formName ? <code>(${formName})</code> : '');
    				console.log('\n==================================================================');
    				console.log('End "' + label + '" script execution at ' + new Date());
    				console.log('==================================================================\n');
    			}
    	}
    })();
    }
    // ========== Logger End ========== //
    
    Attachments:
    You must be logged in to view attached files.
    July 22, 2020 at 3:42 PM #41439

    Rocky Machado
    Participant

    Hi Sam – Reallly like your Looger module. I’m getting an error when trying to compile it. It seems it does not like the “code” tag. Am I missing someting? or did the “code” tag get placed when you copied the script to the site? Also, Have you mofified the script since this last post? If so, can you attach the script?

    it failing here.

    this.logMessage( <code>Caught error: &quot;${error}&quot; at ${error.line}, ${error.column} </code>);

    thanks,
    rocky

    July 22, 2020 at 5:50 PM #41446

    Daniel Leu
    Participant

    I would download the latest version of the script from Sam’s github repository: https://raw.githubusercontent.com/pasamio/tftools/master/scripts/js/logger.js

    July 22, 2020 at 6:32 PM #41448

    Sam Moffatt
    Participant

    I must have missed escaping the backticks when I originally posted it, the forum catches them and turns them into the code tags even though it’s inside a code environment already. @daniel_leu is correct though, my scripts are all up on GitHub so you can grab them there.

    July 22, 2020 at 8:15 PM #41452

    Rocky Machado
    Participant

    Thank You!

Viewing 4 reply threads

You must be logged in to reply to this topic.