Custom log file script problem

Hi,

Creating a custom log file that appears in CRM is relatively easy. Add a new translation with the caption code of something like MyCustomLogs (the caption code is the actual log file name referenced in the script), the family is selLogFiles and the family type is Tags. In each of the language boxes put how you want to see it appear Admin -> System -> Logging. Personally I put something like 100 in the caption order so my custom log appears at the bottom of the list.

In my tablescripts where I need to log information out I will have this:

function InsertRecord()
{
  LogOut("Custom Log Entry");
}

function PostInsertRecord()
{
}

function UpdateRecord()
{
}

function DeleteRecord()
{
}

var bDebug = true;

function LogOut(psData)
{
  if(bDebug)
  {
    var ForReading = 1;
    var ForWriting = 2;
    var ForAppending = 8;
    var TristateUseDefault = -2;
    var TristateTrue = -1;
    var TristateFalse = 0;
    var oFSO = new ActiveXObject("Scripting.FileSystemObject");

    // The following variable contains a physical path that needs modifying depending on the install and location
    var sFileName = "C:\\Program Files (x86)\\Sage\\CRM\\Logs\\" + GetTimeStamp(false) + "MyCustomLogs.log";
    var oLogFile;

    if(!oFSO.FileExists(sFileName)) {oFSO.CreateTextFile(sFileName, true);}
    oLogFile = oFSO.GetFile(sFileName);

    var oTS = oLogFile.OpenAsTextStream(ForAppending, TristateUseDefault);
    oTS.WriteLine(GetTimeStamp(true) + " - " + psData);
    oTS.Close();
  }
}

function GetTimeStamp(pbDateAndTime)
{
  var dtCurrentDate = new Date();
  var iYear = dtCurrentDate.getYear();
  var iMonth = dtCurrentDate.getMonth() + 1;
  var iDay = dtCurrentDate.getDate();
  var iHours = dtCurrentDate.getHours();
  var iMinutes = dtCurrentDate.getMinutes();
  var iSeconds = dtCurrentDate.getSeconds();
  var sDateTimeStamp = "";

  if(iYear < 1000) {iYear += 1900;}
  if(iMonth < 10) {iMonth = "0" + iMonth;}
  if(iDay < 10) {iDay = "0" + iDay;}
  if(iHours < 10) {iHours = "0" + iHours;}
  if(iMinutes < 10) {iMinutes = "0" + iMinutes;}
  if(iSeconds < 10) {iSeconds = "0" + iSeconds;}

  sDateTimeStamp = iYear.toString() + iMonth.toString() + iDay.toString();

  if(pbDateAndTime) {sDateTimeStamp += ": " + iHours.toString() + ":" + iMinutes.toString() + ":" + iSeconds.toString();}

  return sDateTimeStamp;
}

This logging works perfectly and resembles the CRM logs a treat. However, when the log is first written to on a new day, the script errors because the file doesn't exist. The script creates one but for some reason it errors when creating to it and then writing to it. I've used this script for years for debugging and it is incredibly useful but I have never got to the bottom of the error on first write to a new log. I could also write a function that finds the install location rather than hard code it but I couldn't be bothered.

I'll offer this up to anyone that wants to use it but please can someone offer some improvements? It probably could be put into a code library so I don't have to put it in every tablescript, but I wrote this back in the days of 5.x and it hasn't really changed much since. I did put it into a code library using the scriptcommonfunctions registry entry but on a Sage 300 integrated system, the 300 integration hijacks that for its own use.

So... clever people... Can you help improve my old code?

  • Here's my logging code:

    //COMMON FUNCTIONS
    
    //** Switch to enable/disable file logging **
    function loggingOn() {
      return true;
    }
    
    //***** SET THIS SPECIFIC TO THE CRM INSTANCE *****
    function logFileName() {
      return "C:\\CRM\\CRM\\Logs\\tablescriptlog.txt";
    }
    
    
    
    function writeToFile(message) {
     if (loggingOn()) {
      try {
       var fso = new ActiveXObject("Scripting.FileSystemObject");
       var s = fso.OpenTextFile(logFileName(), 8, true);
       var fName = arguments.callee.caller.toString().match(/function ([^\(]+)/)[1]
       s.writeline(new Date().toTimeString() + " - " + fName + ":");
       s.writeline("---- " + message);
       s.writeline("");
       s.Close();
       fso = null;
      } catch (error) {
       ErrorStr += message;
      }
     }
    }

    The third boolean parameter that is passed to OpenTextFile is the 'create the file if it doesn't already exist' flag. 

    I use the ScriptCommonFunctions registry key to define a common script library - and all of the stuff shown above goes in there (https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2009/10/31/function-library-for-tablescripts.aspx)

  • in reply to Chris Burke

    Ha, I should have known you would have an answer Chris! I should just tag you in the questions for CRM to be honest Slight smile

    You were the trainer when I did the developer course for CRM in Winnersh. That's a long time ago!