Difference between revisions of "Using JLog"

From Joomla! Documentation

(Marked for translation)
(Marked this version for translation)
Line 1: Line 1:
 
<noinclude><languages /></noinclude>
 
<noinclude><languages /></noinclude>
<translate>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 [[S:MyLanguage/Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1|Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1]] for more information on this.</translate>
+
<translate><!--T:1-->
 +
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 [[S:MyLanguage/Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1|Exceptions and Logging in Joomla 1.7 and Joomla Platform 11.1]] for more information on this.</translate>
 
__TOC__
 
__TOC__
  
 
<translate>
 
<translate>
== Calling the class ==
+
== Calling the class == <!--T:2-->
 
</translate>
 
</translate>
  
<translate>To use JLog you need to call the JLog class. Done through the following code:</translate>
+
<translate><!--T:3-->
 +
To use JLog you need to call the JLog class. Done through the following code:</translate>
  
 
     jimport('joomla.log.log');
 
     jimport('joomla.log.log');
  
 
<translate>
 
<translate>
== Basic File Logging ==
+
== Basic File Logging == <!--T:4-->
 
</translate>
 
</translate>
  
<translate>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:</translate>
+
<translate><!--T:5-->
 +
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:</translate>
  
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'jerror');
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'jerror');
  
<translate>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:</translate>  
+
<translate><!--T:6-->
 +
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:</translate>  
  
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING);
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING);
  
 
<translate>
 
<translate>
== Logging a specific extension ==
+
== Logging a specific extension == <!--T:7-->
 
</translate>
 
</translate>
<translate>Sometimes it may be useful to log the errors to a specific file. In this case you can.</translate>  
+
<translate><!--T:8-->
 +
Sometimes it may be useful to log the errors to a specific file. In this case you can.</translate>  
  
 
     JLog::addLogger(
 
     JLog::addLogger(
Line 41: Line 46:
 
     );
 
     );
  
<translate>Now remember to change the category when you add a log message. Such as in the example below.</translate>
+
<translate><!--T:9-->
 +
Now remember to change the category when you add a log message. Such as in the example below.</translate>
  
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'com_helloworld');
 
     JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'com_helloworld');
  
<translate>'''Note:''' You may wish to combine this with the [[S:MyLanguage/Display error messages and notices|Display error messages and notices]] section to display visable error notifications to users.</translate>
+
<translate><!--T:10-->
 +
'''Note:''' You may wish to combine this with the [[S:MyLanguage/Display error messages and notices|Display error messages and notices]] section to display visable error notifications to users.</translate>
  
 
<translate>
 
<translate>
== Logging specific priorities ==
+
== Logging specific priorities == <!--T:11-->
 
</translate>
 
</translate>
  
<translate>You can also add an additional logger to capture only critical and emergency log notifications:</translate>
+
<translate><!--T:12-->
 +
You can also add an additional logger to capture only critical and emergency log notifications:</translate>
  
 
     JLog::addLogger(
 
     JLog::addLogger(
Line 64: Line 72:
 
     );
 
     );
  
<translate>You can also exclude a specific category from being included. For example, to log all but DEBUG messages:</translate>
+
<translate><!--T:13-->
 +
You can also exclude a specific category from being included. For example, to log all but DEBUG messages:</translate>
  
 
     JLog::addLogger(
 
     JLog::addLogger(
Line 77: Line 86:
 
     );
 
     );
  
<translate>Notice how bitwise operations (bitwise AND, &; and bitwise NOT, ~)
+
<translate><!--T:14-->
 +
Notice how bitwise operations (bitwise AND, &; and bitwise NOT, ~)
 
are used to calculate the accepted log levels.</translate>
 
are used to calculate the accepted log levels.</translate>
  
 
<translate>
 
<translate>
== Formatting the logfile ==
+
== Formatting the logfile == <!--T:15-->
 
</translate>
 
</translate>
  
<translate>The first parameter to addLogger can have a few optional additional settings in addition to the 'text_file' entry.</translate>
+
<translate><!--T:16-->
 +
The first parameter to addLogger can have a few optional additional settings in addition to the 'text_file' entry.</translate>
  
<translate>There is for example the entry '''text_entry_format''', specifying the format
+
<translate><!--T:17-->
 +
There is for example the entry '''text_entry_format''', specifying the format
 
of each line in your logfile.</translate>
 
of each line in your logfile.</translate>
  
<translate>The default format is:</translate>
+
<translate><!--T:18-->
 +
The default format is:</translate>
  
 
     '{DATETIME} {PRIORITY}      {CATEGORY}      {MESSAGE}'
 
     '{DATETIME} {PRIORITY}      {CATEGORY}      {MESSAGE}'
  
<translate>Here is an example of a different format which shows how to omit the category:</translate>
+
<translate><!--T:19-->
 +
Here is an example of a different format which shows how to omit the category:</translate>
  
 
     JLog::addLogger(
 
     JLog::addLogger(
Line 108: Line 122:
 
     );
 
     );
  
<translate>In addition to the placeholders shown above in the default string, the following values are available:</translate>
+
<translate><!--T:20-->
 +
In addition to the placeholders shown above in the default string, the following values are available:</translate>
  
 
     {CLIENTIP}
 
     {CLIENTIP}
Line 115: Line 130:
  
  
<translate>There is an additional optional boolean parameter '''text_file_no_php''', which specifies whether the log file is prepended with the usual prefix of</translate>
+
<translate><!--T:21-->
 +
There is an additional optional boolean parameter '''text_file_no_php''', which specifies whether the log file is prepended with the usual prefix of</translate>
  
 
     #
 
     #
 
     #<?php die('Forbidden.');?>
 
     #<?php die('Forbidden.');?>
  
<translate>'''Note:''' Usually you should not set this setting to false. Log files should not be readable from the outside, since they can provide valuable information about your system for attackers.
+
<translate><!--T:22-->
 +
'''Note:''' Usually you should not set this setting to false. Log files should not be readable from the outside, since they can provide valuable information about your system for attackers.
 
Only dabble with this if you know what you're doing!</translate>
 
Only dabble with this if you know what you're doing!</translate>
  
<translate>Furthermore, if you want to store the log file somewhere else than the logging path configuredin the Joomla! settings, you can there is the '''text_file_path''' setting.</translate>
+
<translate><!--T:23-->
 +
Furthermore, if you want to store the log file somewhere else than the logging path configuredin the Joomla! settings, you can there is the '''text_file_path''' setting.</translate>
  
 
<noinclude>
 
<noinclude>
 
<translate>
 
<translate>
 +
<!--T:24-->
 
[[Category:Development]]
 
[[Category:Development]]
 
[[category:Joomla! 1.7]]
 
[[category:Joomla! 1.7]]

Revision as of 13:37, 17 October 2015

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎русский

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[edit]

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

   jimport('joomla.log.log');

Basic File Logging[edit]

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[edit]

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 messages of all log levels to be sent to the file
       JLog::ALL,
       // The log category/categories which should be recorded in this file
       // In this case, it's just the one category from our extension, still
       // we need to put it inside an array
       array('com_helloworld')
   );

Now remember to change the category when you add a log message. 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[edit]

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 log level messages to be sent to the file
       JLog::CRITICAL + JLog::EMERGENCY,
       // The log category which should be recorded in this file
       array('com_helloworld')
   );

You can also exclude a specific category from being included. For example, to log all but DEBUG messages:

   JLog::addLogger(
       array(
            // Sets file name
            'text_file' => 'com_helloworld.all_but_debug.php'
       ),
       // Sets all but DEBUG log level messages to be sent to the file
       JLog::ALL & ~JLog::DEBUG,
       // The log category which should be recorded in this file
       array('com_helloworld')
   );

Notice how bitwise operations (bitwise AND, &; and bitwise NOT, ~) are used to calculate the accepted log levels.

Formatting the logfile[edit]

The first parameter to addLogger can have a few optional additional settings in addition to the 'text_file' entry.

There is for example the entry text_entry_format, specifying the format of each line in your logfile.

The default format is:

   '{DATETIME} {PRIORITY}      {CATEGORY}      {MESSAGE}'

Here is an example of a different format which shows how to omit the category:

   JLog::addLogger(
       array(
            // Sets file name
            'text_file' => 'com_helloworld.critical_emergency.php',
            // Sets the format of each line
            'text_entry_format' => '{DATETIME} {PRIORITY} {MESSAGE}'
       ),
       // Sets all but DEBUG log level messages to be sent to the file
       JLog::ALL & ~JLog::DEBUG,
       // The log category which should be recorded in this file
       array('com_helloworld')
   );

In addition to the placeholders shown above in the default string, the following values are available:

   {CLIENTIP}
   {TIME}
   {DATE}


There is an additional optional boolean parameter text_file_no_php, which specifies whether the log file is prepended with the usual prefix of

   #
   #<?php die('Forbidden.');?>

Note: Usually you should not set this setting to false. Log files should not be readable from the outside, since they can provide valuable information about your system for attackers. Only dabble with this if you know what you're doing!

Furthermore, if you want to store the log file somewhere else than the logging path configuredin the Joomla! settings, you can there is the text_file_path setting.