How to Log Errors in Database in CakePHP

, , Leave a comment

How to log Errors in Database in CakePHP

In this post we’re going to learn how to log errors in the database in cakephp.Before moving direct on the topic lets clear the default behaviour of cakephp’s error logging.


For any application log files serve as a barometer for how well the app is operating and provide you idea as to how your users are using your app.
In case of cakephp the default CakeLog settings writes log entries to a file in your /tmp/logs/ directory under the type of log that you have defined. 
For Example:
CakeLog::write('error', 'Something did not work');
Will write to /tmp/logs/error.log
Error Logging is easy in CakePHP.There are two ways of writing to the log files.The first one is to use the static CakeLog::write() method.
CakeLog::write('debug', 'Something did not work');
The second one is to use shortcut function : log() which is available on any class that extends Object.Internally call CakeLog::write() will be called on Calling log() function.
$this->log("Something did not work!", 'debug');
Error logging in files have some Disadvantages like: 
– Large size
– Dirty look
– Lower readability
– Searching is tedious task.
To overcome these disadvantages you have to find the way to store the errors in the database instead of the log files.To accomplish this i googled and found one plugin called “CakePHP-DatabaseLogger-Plugin” but unfortunately it has some errors and unclear steps of its setup.I have corrected that and explained all the steps in detail so that anyone can easily understand it.


(1) Download the plugin from here: DatabaseLogger
 (Note: original plugin source : CakePHP-DatabaseLogger-Plugin)
(2) Extract the zip file.Copy the DatabaseLogger folder and paste it in your app/Plugin folder.Now copy the database_logger.php file and paste it to app/Config folder.
(3) Run schema (located at app/Plugin/DatabaseLogger/Config/Schema/DatabaseLogger.sql) into database to create logs table.
(4) Add the lines given below in your app/Config/bootstrap.php file.

CakeLog::config('default', array(
'engine' => 'DatabaseLogger.DatabaseLog'


(5) Comment the below lines which is default file logging option.
CakeLog::config('debug', array(

'engine' => 'File',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',


You can use it anywhere in your app.When you call $this->log() or CakeLog::write the database logger will be used.
$this->log('This message will be logged in the database','error');


CakeLog::write('error', 'This message will be logged in the database');
Now navigate to to view,delete or search your logs.

Download Plugin