没有不值得去解决的问题,也没有不值得去学习的技术!

Investigating Poor Readability When Yii2 Debug Log Messages Display Errors in JSON

Investigating Poor Readability When Yii2 Debug Log Messages Display Errors in JSON

1. Investigating the poor readability caused by error logs displaying in JSON format within Yii2’s Debug Log Messages. See Figure 1.

Investigating the poor readability caused by error logs displaying in JSON format within Yii2's Debug Log Messages
Figure 1

2. Found that the existing implementation is roughly as follows:

api/config/main.php

PHP
return [
    'components' => [
        'errorHandler' => [
            'errorAction' => 'site/error',
            'class' => api\exception\ApiErrorHandler::class,
        ],
    ]
];

api/exception/ApiErrorHandler.php

PHP
<?php
 
namespace api\exception;
 
use Yii;
use yii\web\ErrorHandler;
use yii\web\Response;
use yii\web\UnauthorizedHttpException;
 
class ApiErrorHandler extends ErrorHandler
{
    protected function renderException($exception)
    {
        if (Yii::$app->has('response')) {
            $response = Yii::$app->getResponse();
            // reset parameters of response to avoid interference with partially created response data
            // in case the error occurred while sending the response.
            $response->isSent = false;
            $response->stream = null;
            $response->data = null;
            $response->content = null;
        } else {
            $response = new Response();
        }
        $response->format === Response::FORMAT_JSON;
 
        $code = 10090;
        $message = '系统错误';
        if ($exception instanceof UnauthorizedHttpException) {
            $message = '登录后才可访问';
            $code = 11000;
        } else {
            Yii::error(['异常捕获', $exception->getMessage(), $exception->getFile(), $exception->getLine()]);
            Yii::error($exception->getTrace(), 'application');
        }
 
        $response->data = [
            'code' => $code,
            'message' => $message,
            'data' => [
                'error_code' => $exception->getCode()
            ]
        ];
//        $response->headers->add('Access-Control-Allow-Origin', '*');
//        $response->headers->add('Access-Control-Allow-Headers', '*');
        $response->send();
    }
}
 

3. The error log is output by this line: Yii::error($exception->getTrace(), ‘application’);. Because $exception->getTrace() is an array, Yii defaults to json_encode()ing it before writing it to the log, so what is seen in the debug module is in JSON format (the array is converted to a string).

4. The final adjustment is as follows, and the error log is no longer in JSON format. See Figure 2.

The final adjustment is as follows, and the error log is no longer in JSON format
Figure 2

api/exception/ApiErrorHandler.php

PHP
<?php
 
Yii::error($exception->getTraceAsString());

5. However, I noticed that the “error application” and “error Error” logs are largely similar, so I felt there was no need to log another $exception->getTraceAsString().

6. I decided to adjust the configuration in api/web/index.php and then observe the generated logs again. Checking app.log, I found there was still little difference, so I decided to remove Yii::error($exception->getTraceAsString());. See Figure 3.

I decided to adjust the configuration in api/web/index.php and then observe the generated logs again. Checking app.log, I found there was still little difference, so I decided to remove Yii::error($exception->getTraceAsString());
Figure 3
PHP
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系