Custom error logging

Hi,

although I like the way Yii logs the errors to the file, I’d like to extend it a bit, so that each time an unhandled Exception is thrown (it measn, the user will be notified), instead of providing user with the exception content, I’d like to assign some id (uniqid) to the exception, than log it to the file along with the exception, and display that ID to the user.

Where should I implement this action? The error handling in Yii is quite complex, and I don’t want to interrupt the default behaviors, just to extend it a bit.

Is extending CErrorHandler a proper way of doing this?

I did this by re-defining the CApplication::handleException() method. See:

http://www.yiiframework.com/doc/api/1.1/CApplication#handleException-detail

I also re-defined the CApplication::handleError() method. See:

http://www.yiiframework.com/doc/api/1.1/CApplication#handleError-detail

However, I did run into one gotcha:

  1. The functions handleException() and handleError() are defined in CApplication.

  2. I want my customized error/exception handling to work both for web applications and for console applications.

  3. However, since both CConsoleApplication and CWebApplication extend CApplication, I had to extend BOTH

    of those base classes, and define handleException() and handleError() in each extended class.

So, for some Yii expert reading this, is there another way? A better way? A way to avoid code duplication?

both methods raises events, onException and onError.

in configuration file, you may specify the onError handler,




    'onError' => function($ev) {

        $ev->params['uniqid'] = uniqid();

        $ev->message = $ev->params['uniqid'] . ': '. $ev->message;

    },



however this doesn’t resolve my problem, as the onError seems to be raised after the error is logged to the file, so I can’t append the generated ID to the error message in file.