Google Ad

CakePHP Database Logger Plugin

Date: Sat, Aug 13th 2011, 15:02 Author: nick Views: 32445 Comments share

CakePHP Database Logger

Get it:
  • Download Now
  • git clone git:// database_logger
It's been a while since I've posted, I've been doing a lot of work on various fun projects and just haven't had the time to formally post about all the cool things I've been doing. I figured with a new plugin deserves a new blog post. So, here I am -- lets talk log files and how they suck.

Log files.. more like lame files

Log files can be handy, they can serve as a barometer for how well your app is operating, and give you some clue as to how your users are using your app (or more importantly -- breaking your app). The default CakeLog settings writes log entries to a file in your /tmp/logs/ directory under the type of log you're specifying.

For example:
  1. CakeLog::write('error', 'This will be a new entry in the /tmp/logs/error.log file');

Will write to /tmp/logs/error.log
This is all well and good if you're running your application on a single server, and you're a masochist who likes to copy over large log files, and open them up in an editor to actually search through it.

However, even though I'm a web developer, I'm not a masochist -- despite popular opinion. So I've developed a super simple plugin to take all those handy dandy log messages scattered around an application and save them to a database logs table instead. The benefit of this strategy is three fold.

1) Scale-ability -- as you add more app servers to handle traffic you're logs will still all be consolidated in one place.

2) Easier searches, with the built in admin interface searching through logs can be done directly in the app.

3) Free extra information with each log entry. With each log entry the requesting url, hostname, ip, and referrer is automatically logged along side it.


Setup is simple. Like any other plugin you'll need to clone the repository into your app/plugins/database_logger directory, and run the schema into the database to create the required logs table.

  1. # clone plugin
  2. git clone git:// app/plugins/database_logger
  4. # run schema into database to create logs table.
  5. cake schema create database_logger -plugin database_logger

Next, you'll need to add two lines to your app/config/bootstrap.php file to configure your CakeLog to log to the new database logger instead of the default FileLog.

  1. //app/config/bootstrap.php
  2. App::import('Core','CakeLog');
  3. CakeLog::config('default', array('engine' => 'DatabaseLogger.DatabaseLogger'));


Anywhere in your app where you call $this->log() or CakeLog::write the database logger will be used.

  1. $this->log('This is a detailed message logged to the database','error');
  2. CakeLog::write('error', 'This is a detailed message logged to the database');

Navigate to to view/search/delete your logs.