The CakePHP error log (CakePHP error logging)

CakePHP error log FAQ: Where is the CakePHP error log, and how do I write to it?

CakePHP error log file location

The CakePHP error log file is named error.log, and it is located in the $app/tmp/logs directory of your CakePHP application:

$app/tmp/logs/error.log

(Where $app represents the name of your CakePHP application.)

In my current application, although I haven't intentionally written any messages to my CakePHP error log myself, I just looked at my log file, and was pleasantly surprised to find a lot of valuable information in there. :)

How to write to the CakePHP error log

Writing to the CakePHP error log is very simple. Just use one of two variations of the CakePHP log function.

When you call the CakePHP log function with just one argument, your output will be sent to the CakePHP error.log file. Here's an example of that:

$this->log('This message goes to CakePHP's error.log file.');

Writing to alternate CakePHP log files

If you want to send your message to CakePHP's debug.log file, or you just want to be more explicit about where you're sending your message, you can add a second parameter to your log function call. This example shows how to write to the CakePHP debug.log file:

$this->log('This message goes to debug.log.', LOG_DEBUG);

While this example shows how to write to a log file named foobar.log:

$this->log('This message goes to my_log_file.log.', 'foobar');

As a final CakePHP logging note, if you wanted to be more explicit that you are writing to the CakePHP error log file, you can add the LOG_ERROR parameter to your call to the CakePHP log function, like this:

$this->log('This message goes to error.log.', LOG_ERROR);

However, as mentioned earlier, that is completely optional.

For more CakePHP logging information

For more information on CakePHP logging, check out the log function page of the CakePHP Cookbook.

As an interesting note, the CakePHP log function is actually implemented in the CakePHP Object class. Although I haven't tried it yet, this implies that you should be able to write a message to a log file from any class (model, view, controller, and other classes) without any additional configuration, and if so, that's very cool.