Pages

Wednesday, January 26, 2011

SugarCRM Troubleshooting 101: Quick and Easy Custom Logs

Here is an easy, yet rather helpful, tip.


Suppose one needs to add some log messages to SugarCRM in order to trace a problem -- not uncommon when writing custom code such as that found in logic hooks.


How do we go about adding custom log messages so we can see what is going on?


It is actually fairly easy and only requires one line of code:


$GLOBALS['log']->fatal('My custom debug message');


There are two important items to note about this bit of code.  


First, the word fatal actually refers to the logging level.  SugarCRM allows you to select from various levels, including info, fatal and debug (the most verbose).  The level can be defined by an admin level user by simply selecting Admin > System Settings and modifying the logging level at the bottom of the screen.  Your custom message will print out only if the selected level within System Settings screen matches the level you use within this line of code.


Next, in this example, the text My custom debug message is all that would be printed by the logging engine.  It is basically a string of your liking, which can include references to variables -- either your own or those created by SugarCRM (such as $bean->id).


Consequently, log messages (custom or otherwise) are written to a file named sugarcrm.log which you can find in the root install directory of your SugarCRM instance.  If you prefer a different name for your log file, change it via the earlier referenced Systems Settings screen.


Lastly, if you are using a hosted instance, you may not have access to the root directory corresponding to your install.  To circumvent that and access your log file, select Admin > Diagnostics.

2 comments:

  1. Hello to everybody.

    I can't understand where to put this line.
    /config.php is it?

    Thanks to you

    ReplyDelete
  2. @Daniel:

    You would put it in whichever custom code file requires it. It is for the purposes of outputting custom debug messages, in addition to what is already provided by Sugar.

    ReplyDelete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.