How to debug your code: Difference between revisions
From Joomla! Documentation
Reference newer documentation on logging and debugging features. |
|||
| Line 1: | Line 1: | ||
{{page|needs improvement|Updated Joomla! System-debug plugin features not listed here.}} | {{page|needs improvement|Updated Joomla! System-debug plugin features not listed here.}} | ||
==Update== | |||
Much of the below methods can still be valid for some quick and ''dirty'' debugging, however, there is sufficient functionality gains from the JLog class and the Debug System Plugin added since this page was created. | |||
From [[Using JLog]]: | |||
''Joomla logging [JLog] gives you the ability to log messages to files and to the screen (within the Joomla! Debug Console at the bottom of the web page), and the main Joomla class which underpins this is JLog. The logging can be controlled dynamically to some extent through the Joomla Global Configuration and by configuring the System – Debug plugin (which is shipped with Joomla).'' | |||
==The Easy Way 1 (echo)== | ==The Easy Way 1 (echo)== | ||
Revision as of 03:28, 25 October 2017
This article is tagged because it NEEDS IMPROVEMENT. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.
Reason: Updated Joomla! System-debug plugin features not listed here.
Update
Much of the below methods can still be valid for some quick and dirty debugging, however, there is sufficient functionality gains from the JLog class and the Debug System Plugin added since this page was created.
From Using JLog:
Joomla logging [JLog] gives you the ability to log messages to files and to the screen (within the Joomla! Debug Console at the bottom of the web page), and the main Joomla class which underpins this is JLog. The logging can be controlled dynamically to some extent through the Joomla Global Configuration and by configuring the System – Debug plugin (which is shipped with Joomla).
The Easy Way 1 (echo)
The simplest way to see what is going on inside your code is to temporarily add echo statements for variables to show their values on the screen. For example, say you want to know what the value of some variables are when $i is "5". You could use code like this:
for ( $i = 0; $i < 10; $i++ ) {
if ( $i == 5 ) {
echo '$i=' . $i;
// other echo statements
}
}
This works for simple situations. However, if you are planning on doing a lot of Joomla! development, it is worth the effort to install and learn an integrated development environment (IDE) that includes a real PHP debugger.
The Easy Way 2 (joomla message)
Your code won't always display simple echo statements. In that case you can try this alternative, still easy way:
JFactory::getApplication()->enqueueMessage('Some debug string(s)');
You can choose different message types which corresponds to grouping with different styles (colors mainly).
Using an IDE
Check this 3 minutes video that shows how you can debug your code with a Browser and an IDE
Many Joomla! developers use the Eclipse IDE. This is free and includes a debugger. Instructions for installing it are available at Setting up your workstation for Joomla! development.
Using the PHP Expert editor
Another option is the PHP Expert editor with an installed extension for debugging. Add the following lines to the php.ini file:
extension=php_dbg.dll [Debugger] debugger.enabled=on debugger.profiler_enabled=off
It is best to set profiler_enable to "off". Then you need to set options in the Run/Options menu to use HTTP-server and the directory in which your script is located.
If all options are correct, you may run your script in debug mode by clicking on the Debug button (F8)
J!Dump
An often handy extension that can be found in the JED is the J!Dump extension that will allow you to dump variable, stack traces, and system information into a popup window at run time. This extension works like the PHP command `var_dump` but formats the output in a much more readable fashion.
JDEBUG
To check whether the Website is in the debug mode, test the JDEBUG variable:
if(JDEBUG)
{
//whatever debugging code you want to run
}
The debug mode can be enabled from the Joomla! Global Configuration under the System tab. Once enabled the output of the debug plugin will be displayed at the bottom of each page.
This plugin provides important information that can assist in debugging and improving the performance of your component.
Profile Information
The Profile Information tab from the debug plugin provides information about the time and memory taken to render the page based on each of the application events. This can help to identify areas outside of network speed that are contributing to long page load times high server memory usage.
Database Queries
One of the most useful tabs is the Database Queries tab. This will provide a log of all queries that have been executed while loading the page and identify both the time taken to execute the query and whether duplicate queries have occurred. This is particularly useful when debugging performance problems on larger components as duplicate queries are often a contributing factor.
JFirePHP
Use the Joomla JFirePHP extension.


