Using JLog

From Joomla! Documentation

Revision as of 14:56, 28 January 2013 by Wilsonge (Talk | contribs)

Using JLog can be very useful in components when analysing the performance of custom extensions - or analysing where extensions are giving issues. Note this should be used in tandem with php exceptions - not as a replacement!

Call the class

To use JLog you need to call the JLog class. Done through the following code:


Basic File Logging

Often you may wish to display an error log message and log to an error file. Joomla allows this natively through the JLog::add function. For example:

   JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'jerror');

Adding the category of jerror means that this message will also be displayed to users. To only write to file you can easily drop that parameter and simply use

   JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'jerror');

More Complex Examples

Sometimes it may be useful to log the errors to a specific file. In this case you can

            //Sets file name
            'text_file' => 'com_helloworld.errors.php'
       //Sets all JLog messages to be set to the file
       //Chooses a category name

Now remember to change the category when you add a log. Such as in the example below.

   JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'com_helloworld');

Note you may wish to combine this with the Display error messages and notices section to display visable error notifications to users.