Actions

Difference between revisions of "Using JLog"

From Joomla! Documentation

(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{version|1.7,2.5,3.0|platform=11.1,11.4,12.1}}{{RightTOC}}
+
{{version|2.5,3.1|platform=11.1}}{{RightTOC}}
 
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. See [[Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1]] for more information on this
 
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. See [[Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1]] for more information on this
  
Line 16: Line 16:
 
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  
 
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');
+
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING);
  
 
== Logging a specific extension ==
 
== Logging a specific extension ==
Line 37: Line 37:
  
 
Note you may wish to combine this with the [[Display error messages and notices]] section to display visable error notifications to users.
 
Note you may wish to combine this with the [[Display error messages and notices]] section to display visable error notifications to users.
 +
 +
== Logging specific priorities ==
 +
 +
You can also add an additional logger to capture only critical and emergency log notifications.
 +
 +
    JLog::addLogger(
 +
        array(
 +
            //Sets file name
 +
            'text_file' => 'com_helloworld.critical_emergency.php'
 +
        ),
 +
        //Sets critical and emergency JLog messages to be set to the file
 +
        JLog::CRITICAL + JLog::EMERGENCY,
 +
        //Chooses a category name
 +
        'com_helloworld'
 +
    );
 +
 +
  
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 05:38, 4 September 2013

Contents

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. See Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1 for more information on this

Calling the class

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

   jimport('joomla.log.log');

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);

Logging a specific extension

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

   JLog::addLogger(
       array(
            //Sets file name
            'text_file' => 'com_helloworld.errors.php'
       ),
       //Sets all JLog messages to be set to the file
       JLog::ALL,
       //Chooses a category name
       'com_helloworld'
   );

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.

Logging specific priorities

You can also add an additional logger to capture only critical and emergency log notifications.

   JLog::addLogger(
       array(
            //Sets file name
            'text_file' => 'com_helloworld.critical_emergency.php'
       ),
       //Sets critical and emergency JLog messages to be set to the file
       JLog::CRITICAL + JLog::EMERGENCY,
       //Chooses a category name
       'com_helloworld'
   );