【问题标题】:Override write function of CakePHP log覆盖 CakePHP 日志的写入函数
【发布时间】:2013-02-18 17:11:26
【问题描述】:

我正在尝试将 CakePHP 日志保存在数据库中(表:日志),但尝试时返回错误:

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)

详情

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

文件

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
我也在 app/Lib/ 和 app/Lib/log 中尝试过,但不起作用。

<?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);
    }
 
}
?>

[编辑]
我更改了DatabaseLogger.php 文件的位置,并添加了两条重要的行:

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

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

现在,“工作”,但不像我预期的那样,因为我需要更多 2 个值在写入函数中:query, debug,但不允许。我该怎么做?

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

【问题讨论】:

    标签: php apache cakephp


    【解决方案1】:

    我目前正在这样做:

    • 连接所有变量并在 $message 参数中发送。
    • 稍后,我将其分解并使用这些值

    但我认为这是错误的。

    我真正需要的是:“自定义写入功能”,或者:“如何创建自定义日志”

    无论如何,谢谢。

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      相关资源
      最近更新 更多