Home > PHP Framework > YII > body text

How to manually generate error logs in Yii

王林
Release: 2020-12-10 16:48:16
forward
3513 people have browsed it

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',
        ]
Copy after login

is modified to:

'errorHandler' => [
            'errorAction' => 'error/error',
        ]
Copy after login

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;
   }
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template