首页 > 解决方案 > 创建文件后记录丢失几行

问题描述

我写了一个特征来记录一些东西,但是当第一次使用它来记录时(文件不存在),一些日志内容看起来像是丢失了。

试了几次调试,没有解决。

帮手特质

<?php
namespace App\Helpers;

use Illuminate\Support\Facades\Log;

trait OutToLog
{
    public function outLogTo(string $channel, string $logContents)
    {
        Log::channel($channel)->info($logContents);
    }
}

预期日志

[2019-10-20 19:19:42] INFO: status=200;
request=test_request;
response=test_response;

[2019-10-20 19:19:42] INFO: status=300;
request=test_request;
response=test_response;

[2019-10-20 19:19:42] INFO: Test notify service with exception.  

真正的日志

[2019-10-20 19:19:42] INFO: Test notify service with exception.  
=test_response;

经过第一次测试,第二次测试,之后没有同样的问题。实在不明白为什么。

这被称为如下所示:

    private function resultLog(array $result): void
    {
        if ($result['status'] === 200) {
            doSomething...
        }

        $resultLog = "status=" . $result['status'] . $resultRequest . ";\n" .$resultResponse. "\n";
        $this->outLogTo('channel_name', $resultLog);
    }

这被称为

    try {
        $this->resultLog($result);
    } catch (\Exception $e) {
        $this->exceptionLog($e);
    }

像这样的异常日志

private function exceptionLog($e){
    $this->outLogTo('channel_name', $e);
}

标签: laravellogging

解决方案


推荐阅读