Home >PHP Framework >YII >How to manually generate error logs in Yii

How to manually generate error logs in Yii

王林
王林forward
2020-12-10 16:48:163537browse

How to manually generate error logs in Yii

The specific method is as follows:

First configure the error method and modify the errorHandler parameters

(Related recommendations: yii framework)

Under config/web, the default is the error method under the site controller

'errorHandler' => [
            'errorAction' => 'site/error',
        ]

is modified to:

'errorHandler' => [
            'errorAction' => 'error/error',
        ]

I am used to recreating an error The method depends on personal habits.

Create actionError in the error controller, as follows:

public function actionError(){
       $error = \Yii::$app->errorHandler->exception;
       $error_msg = '';
       if($error){
           $filed = $error->getFile();  //获取错误文件
           $line = $error->getLine();   //获取错误行数
           $message = $error->getMessage(); //获取错误消息
           $code = $error->getCode();   //获取错误码

           $log = new FileTarget();
           $log->logFile = \Yii::$app->getRuntimePath() . "/log/error.log"; //生成文件到log目录下

           $error_msg = $message ." [file:{$filed}][line:{$line}][message:{$message}][code:{$code}][url:{$_SERVER['REQUEST_URI']}][POST_DATA:".http_build_query($_POST)."]";

           $log->messages[] = [
               $error_msg,
               1,
               'applicition',
               microtime( true )
           ];
            $log->export();
       }
       return $error_msg;
   }

The error log will be generated in the runtime/log directory.

The above is the detailed content of How to manually generate error logs in Yii. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete