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

在 Yii2 中的 Debug Log Messages 中,error 日志显示为 json 格式,可读性太差的排查分析

在 Yii2 中报错:Typed property common\\components\\i18n\\PhpMessageSource::$fallbackBasePath must not be accessed before initialization'

1、在 Yii2 中的 Debug Log Messages 中,error 日志显示为 json 格式,可读性太差的排查分析。如图1

在 Yii2 中的 Debug Log Messages 中,error 日志显示为 json 格式,可读性太差的排查分析
图1

2、发现现有的实现大致如下

api/config/main.php


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


api/exception/ApiErrorHandler.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、error 日志是由这一行输出的 ,Yii::error($exception->getTrace(), ‘application’); ,因为 $exception->getTrace() 是一个数组,Yii 默认是把它 json_encode() 后写到日志里的,所以在 debug 模块里看到的是 JSON 格式(数组被转成了字符串)。

4、最终调整如下,error 日志中不再是 json 格式。如图2

最终调整如下,error 日志中不再是 json 格式
图2

api/exception/ApiErrorHandler.php

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

5、不过发现 error application 与 error Error 是大致相似的,感觉没有必要再记录一个 $exception->getTraceAsString() 了。

6、决定调整一下 api/web/index.php 中的配置,然后再次观察生成的日志。查看 app.log ,发现仍然无甚差异,决定删除掉 Yii::error($exception->getTraceAsString()); 如图3

决定调整一下 api/web/index.php 中的配置,然后再次观察生成的日志。查看 app.log ,发现仍然无甚差异,决定删除掉 Yii::error($exception->getTraceAsString());
图3

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');


PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理