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

2. Found that the existing implementation is roughly as follows:
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. 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.

api/exception/ApiErrorHandler.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.

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

