简体   繁体   中英

Override write function of CakePHP log

I'm try save CakePHP logs in database (table: logs), but return error when I try:

Could not load class Databaselogger
Error: An Internal Error Has Occurred.

Stack Trace
CORE\Cake\Log\LogEngineCollection.php line 42 → LogEngineCollection::_getLogger(string)
CORE\Cake\Log\CakeLog.php line 199 → LogEngineCollection->load(string, array)
APP\Config\bootstrap.php line 31 → CakeLog::config(string, array)
CORE\Cake\Core\Configure.php line 93 → include(string)
CORE\Cake\bootstrap.php line 164 → Configure::bootstrap(boolean)
APP\webroot\index.php line 91 → include(string)

DETAILS

CakePHP: 2.3
Apache: 2.2
PHP: 5.3.9
MySQL: 5.1

FILES

bootstrap.php

CakeLog::config('otherFile', array(
    'engine' => 'Databaselogger'
));

UsersController.php

CakeLog::write('info', $this->alias, null, print_r($this->request->data, true));

app/Log/Databaselogger.php
I try in app/Lib/ too, and app/Lib/log too but doesn't work.

<?php
class Databaselogger extends CakeLog {

    function __construct() {
        App::import('Model', 'Log');
        $this->Log = new Log;
    }

    function write($type, $message, $query, $debug) {
        $this->Log->create();

        $log['type'] = ucfirst($type);
        $log['message'] = $message;
        $log['query'] = $query;
        $log['debug'] = $debug;
        $log['created'] = date('Y-m-d H:i:s');
        $log['modified'] = date('Y-m-d H:i:s');

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

}
?>

[EDIT]
I change the location of DatabaseLogger.php file, and add 2 important lines:

    // app/Lib/Log/Engine/DatabaseLogger.php

    App::uses('CakeLogInterface', 'Log');
    class DatabaseLogger implements CakeLogInterface {

And now, "works", but not as I expected, because I need more 2 values in write function: query, debug , but isn't allowed. How I do that?

    function write($type, $message, $query, $debug) {

I'm currently do that:

  • concatenate all variables and sent in $message param.
  • Later, I explode that and use the values

But I think this is wrong.

What I really need is: "custom write function", or: "how create your custom log" .

Anyway thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM