douzhun4124 2011-12-16 08:01
浏览 53
已采纳

为Kohana设置错误日志记录

I have exception class:

class MartbooksException extends Exception 
{
    public function __construct($msg)
    {
        parent::__construct($msg, 0, null);
        echo $msg;
        Kohana::$log->add(Log::ERROR, $msg);
    }
}

Kohana::$log->add(Log::ERROR, $msg);

Is this all I should do to write logs in application/logs files?

Is this good solution?

  • 写回答

2条回答 默认 最新

  • drcvvkx914772 2011-12-16 09:18
    关注

    To follow the Kohana style I would recommend that you use:

    Class Martbooks_Exception Extends Kohana_Exception
    

    as declaration and place the file with name exception.php in classes/martbooks. This follows the style of Kohana.

    Extending Kohana_Exception instead of Exception allows you to use variable substitution along the lines of

    throw new Martbooks_Exception ('this is a :v', array (':v' => 'variable', ));
    

    As for the defining a __construct()-method, the echo $msg; part would not be my preferred way of solving error handling, any echoing should be done in the block that catches the exception. The same could be argued for in the case of calling Kohana::$log->add(), but if you want log every Martbooks_Exception, your solution is perfectly valid. In that case I would rewrite your code to:

    Class Martbooks_Exception Extends Kohana_Exception
    {
       public function __construct($message, array $variables = NULL, $code = 0)
       {
         parent::__construct ($message, $variables, $code);
         Kohana::$log->add (Log::ERROR, __ ($message, $variables));
       }
    }
    

    with a definition of __construct() that conforms to Kohana_Exception's __construct().

    The only objection that I would have against logging with Log::ERROR level in the constructor is that it assumes that every exception is an application level error, which might be true of some exception types, but it could also be used to signal other meanings. The exact meaning of an exception should be left to the exception handling block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大