2013-01-15 128 views
1

我正试图在cakePHP中实现一个日志系统。现在我有一个问题是“写入”中的方法内容被调用两次。这会导致问题,因为它在我的系统中放置了2个相同类型的日志条目。自定义日志方法

Databaselogger.php在lib /发动机/目录:

App::uses('CakeLogInterface', 'Log'); 

class DatabaseLogger implements CakeLogInterface { 


private $types = array(); 


public function __construct($options = array()) { 
    // Allowed method calls 
    $this->types = $options['types']; 
} 

public function write($type = NULL, $message = NULL) { 

    // Only store to cache types that are permitted, other errors from cake are not reported 
    if(!empty($this->types) && in_array($type, $this->types)) 
    { 
     //make database entry here 
    } 
} 
} 

在我的bootstrap.php:

App::uses('CakeLog', 'Log'); 
CakeLog::config('debug', array(
'engine' => 'FileLog', 
'types' => array('notice', 'info', 'debug'), 
'file' => 'debug', 
)); 
CakeLog::config('error', array(
'engine' => 'FileLog', 
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'), 
'file' => 'error', 
)); 

// Custom configuration 
CakeLog::config('mytest', array(
    'engine' => 'DatabaseLogger', 
'types' => array('mytest'), 
'scope' => array(), 
'file' => '', 
)); 

这是我如何调用该方法:

CakeLog::write('mytest', 'this message!'); 

任何人都可以提供一些提示,为什么这可能会发生?谢谢!

回答

0

为核心文件的命名空间是

/Log/Engine/ 

我认为它应该是

/Lib/Log/Engine/DatabaseLog.php (or Logger if you want to straggle from the core naming). 

相应。

证明你会从LogEngineCollection类,它包含得到:

App::uses($loggerName, $plugin . 'Log/Engine'); 

,包括你的引擎(正确的命名空间,因此重要性)。

请花时间查看核心代码。它的开放源码毕竟:)

+0

我实际上把这个例子从网站本身,他们使用databaselogger.php作为文件名:http://book.cakephp.org/2.0/en/core-libraries/logging.html – Sixthpoint

+0

这也没有解决我遇到的问题。这是另一个奇怪的注释,当我调用debug()时,它实际上解决了这个问题。但没有调用它会导致2日志形式 – Sixthpoint

+0

我认为有一个额外的配置设置,你需要这是不是在文档中。可能在你的配置引导?然而,在文档中没有,你的例子确实遵循cakephp书。 – Brianjs