3 Jan 2015

[CakePHP2]Database logging (with some Cakesession Object)

Logging(CakePHP official)
http://book.cakephp.org/2.0/en/core-libraries/logging.html
Database logging in CakePHP
http://blog.jandorsman.com/blog/database-logging-in-cakephp
Well, Jan's article is totally usable even in CakePHP 2.4 except one location 'app/libs/log/database_logger.php'(for CakePHP 1.*) to 'app/Lib/Log/Engine/DatabaseLog.php' (for CakePHP 2.*).

When you want log some information via Cakesession Object (i.e. user ID), you should check the object first in case the user not logging in.

public function write($type, $message) {
 $log['type'] = ucfirst($type);
 $log['created'] = date('Y-m-d H:i:s');
 $log['content'] = $message;
 if ($login = CakeSession::read('Auth.User')) {
  $log['user_id'] = $login['id'];
 }


 return $this->Log->save($log);
}