Adding Additional Custom Logs to Sage CRM

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Adding Additional Custom Logs to Sage CRM

  • Comments 1
  • Likes
If you are working on an integration of Sage CRM with another system you may have a requirement to Log errors that are specific to the integration itself.

For example integrating Sage CRM with an ERP system you may wish to report errors fully so that possibly several lines worth of info) is logged to a Log File.

There is currently no mechanism for writing your own messages into the existing log files. There is some difficulty with writing into the existing log and that is because several mechanisms may demand simultaneous access and so it would be safer to log the integration messages separately. This is an approach that is used to keep solo synchronization information, SQL and escalation logging separate.

I would always advise keeping your own log information separate as you then are in complete control. Writing your own log files is not difficult and you would be free to structure this in the manner best suited for your project.

We can use the Validation Script and Table Level scripts to allow us easily to write out errors and log messages. A very simple example of the type of code can be seen here:

var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
var mySystemObject = new ActiveXObject('Scripting.FileSystemObject');
var myFile = mySystemObject.GetFile("e:/TEST.TXT");
//Assumes TEST.TXT exists
var myFileTextStream = myFile.OpenAsTextStream(ForAppending, TristateUseDefault);
myFileTextStream.WriteLine('Sage CRM Log');

Now that we can create our own log file this can be integrated into the existing Log Management screen. It is possible to extend the category of log files supported (e.g. System, Outlook, SQL etc) so that we can use the standard CRM logging screen (i.e. Administration --> System --> Logging)

If you add a translation into the custom_captions table (Administration --> Customization --> Translation)

capt_family = selLogFiles
capt_code = CustomLog
capt_us = Custom Logs

This will appear as an option in the available logs to list.

There is something to watch out for though! The name of the custom log file must not include numeric characters other than in the date element.


capt_family = selLogFiles
capt_code = CustomLog
capt_us = Custom Logs

will work but not the following example

capt_family = selLogFiles
capt_code = ERP50
capt_us = Custom Logs

The files must be stamped with the date in order to be found as it looks for files of the form “????????customlog.log”

Or I suppose if you don't want to stamp with the date, just give it a name that has any 8 characters in front of the “customlog.log”

e.g. aaaaaaaacustomlog.log

This means that it is immediately simple to integrate your own logging into the management screen.

The next problem you might face is where to get the path to the folder in which the log files should be created. This is especially true when you are creating code to be used in a component and you may not know the details of the final system.

The server name (if needed) can be got by

var WSNet=new ActiveXObject("WScript.Network"); ErrorStr=WSNet.computername;

and the install name can be got by:

var Reg = GetObject("winmgmts:{impersonationLevel=impersonate}!root/default:StdRegProv");
var Method = Reg.Methods_.Item("EnumKey");
var Params = Method.InParameters.SpawnInstance_();
Params.hDefKey = 0x80000002;
Params.sSubKeyName = "SOFTWARE\\eWare\\Config";
var SubKeys = Reg.ExecMethod_(Method.Name, Params);

This lastly assumes that there is a single CRM install.

The logs folder is then in a constant location.

C:\Program Files\Sage\CRM\installname\Logs\customname.log
  • Hi Jeff,

    I've been using something similar to this for a long time which is great and it works. One thing that has always been an annoyance is that when the first custom log entry is written to the file, obviously the file doesn't exist but it gets created but that first entry isn't recorded. This results in the end user getting an error message on the screen. Any subsequent output to that log file during that day goes in just fine. I can live with this when I use the logging facility for development and testing but it means I can't leave the logging on over time as the customer gets one error each day. Any ideas? My code doesn't include the log folder location code as I just hard code it.