登录Zend最简单的方法?

时间:2013-08-19 08:28:47

标签: zend-framework

在YII中,当您想要记录时,只需致电:

YII::log("bla");

在Zend中,我必须创建一个函数:

private function log($message, $priority = Zend_Log::INFO)
{
    $logger = Zend_Registry::get("log");
    if( is_object($message) || is_array($message) )
        $logger->log( print_r( $message,true), $priority);
    else
        $logger->log( $message, $priority);
}

获取全局函数的最简单方法是什么,这样我也可以像使用YII一样轻松记录内容。是否有一种内置的方式,我可以做这样的事情:

 ZEND::log("Bla");

或者我将如何创建此类功能以及将其放在我的代码中?

1 个答案:

答案 0 :(得分:2)

这取决于您的日志记录目的。我看到三个选项:

  1. 标准日志记录。如果你想记录所有内容,那么你可以检查谁应该创建一个插件并让它记录,或者创建数据库适配器日志。

    我记录的一个例子:

    class App_Plugin_Logger extends Zend_Controller_Plugin_Abstract {
    
    protected $fileLoc;
    public function __construct($file = null) {
        if($file ===null)
            $file = APPLICATION_PATH.'/../log/log_'.date('Y-m-d').'.txt';
        $this->fileLoc = $file;
    }
    
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $id = (Zend_Auth::getInstance()->hasIdentity() ? Zend_Auth::getInstance()->getIdentity()->getId() : 0);
    
        $fp = fopen($this->fileLoc,"a");
        $message = date('Y-m-d H:i:s').' '.$request->getServer('REMOTE_ADDR').' USER:'.$id.' '.json_encode($request->getParams()).PHP_EOL;
            //logs all the input data (post/get), controller, action and ip-addres of client
        if(fwrite($fp,$message)===false){
            error_log('MagStream, Logging failed:'.$message);
        }
        fclose($fp);
    }
    }
    
  2. 如果您只想知道调试变量,可以die(var_dump($variable));或使用xdebug扩展名(非常有用)

  3. 如果你想在特定的地方登录你可以创建一个自己的类,它会打开一个文件并在最后添加一行或使用zend_log库:http://framework.zend.com/manual/1.12/en/zend.log.overview.html

  4. 您可以向Zend_Log添加静态函数:

    public static function l($mes){
      $logger = new Zend_Log();
      $writer = new Zend_Log_Writer_Stream('php://output'); 
      $logger->addWriter($writer);
      $logger->log($mes, Zend_Log::DEBUG);
    }
    
相关问题