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?
To follow the Kohana style I would recommend that you use:
as declaration and place the file with name
exception.phpinclasses/martbooks. This follows the style of Kohana.Extending
Kohana_Exceptioninstead ofExceptionallows you to use variable substitution along the lines ofAs for the defining a
__construct()-method, theecho $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 callingKohana::$log->add(), but if you want log everyMartbooks_Exception, your solution is perfectly valid. In that case I would rewrite your code to:with a definition of
__construct()that conforms toKohana_Exception‘s__construct().The only objection that I would have against logging with
Log::ERRORlevel 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.