Showing posts with label debugging. Show all posts
Showing posts with label debugging. Show all posts

Quick Tip: are you using pr()?

Tuesday, 1 September 2009 12:43 , In , ,

pr() is a version of print_r() with <pre> tags wrapped around it. It will echo out your array in a human-readable format - without having to view source.

Why is this useful? Well, in the world of CakePHP, everything is an array and eventually you're going to have to do some debugging. Not only is pr() easier to type, it's

Why doesn't pr() work?



Have you set debug mode to zero?

Check your /app/config/core.php for this line:

Configure::write('debug', 0);


Change it to:

Configure::write('debug', 2);


And now you should get some nice debug output at the bottom of every page too.

Debugging with CakePHP

Friday, 5 June 2009 02:41 , In , ,

CakePHP comes with some handy debug functions to make your coding life easier.

The first thing you should do is make sure Configure::read('debug') is set to 1 or 2. This will then allow use of debugger functions and also show database queries at the bottom of the page (very handy for profiling and finding bottlenecks).

print_r convenience function pr()

This function puts [pre] tags around the standard print_r to show the contents of an array in a well-formatted manner, making it easier to read and thus quicker to debug.
 function pr($var) {
if (Configure::read() > 0) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
}
To find out information about your database table use pr($this->ModelName->schema())

Why doesn't the pr() function work?

What is your debug level set to? If it's less than 1, it won't display.

debug($var, $showHTML = false, $showFrom = true)

The debug() function is a globally available function that works similarly to the PHP function print_r(). The debug() function allows you to show the contents of a variable in a number of different ways. First, if you'd like data to be shown in an HTML-friendly way, set the second parameter to true. The function also prints out the line and file it is originating from by default.

Output from this function is only shown if the core debug variable has been set to a value greater than 0.

Logging with cakephp

Logging your output can be useful in a number of scenarios - especially in production servers where the debug level must be set to 0. To start logging output, use the log() function.

The log() function takes two parameters. The first is the message you'd like written to the log file and the second is used to define the log type you wish to write the message to. If not supplied, it defaults to LOG_ERROR, which writes to the error log previously mentioned. You can set this second parameter to LOG_DEBUG to write your messages to an alternate debug log found at app/tmp/logs/debug.log: