【问题标题】:Laravel 7 - How to disable logging?Laravel 7 - 如何禁用日志记录?
【发布时间】:2020-09-03 16:30:39
【问题描述】:

我正在使用 Laravel 7.x,我想编写自己的日志。我已经配置了 config/logging.php 并实现了写入日志条目,但是 Laravel 也在我的日志文件中写入了自己的日志条目。

如何禁用自动 Laravel 日志条目并保持我自己的日志写入?

提前致谢。

【问题讨论】:

  • 您可以创建单独的日志文件并写入您的日志信息。
  • 要使用loggin.php 配置来写我的日志,我可以开始写我的日志。问题是 Laravel 也使用我的文件来编写他自己的日志。
  • 我建议您创建自己的日志文件并根据需要经常更新。 Laravel 是为错误跟踪创建自己的日志文件。
  • 我该怎么做?因为,Laravel 正在写入我的日志文件。我已经定义了我的日志文件,并且 Laravel 正在上面写。
  • 你能显示你的 logger.php 配置吗?您是否使用logger() 函数在您的日志文件中进行记录?

标签: laravel logging disable


【解决方案1】:

您可以制作自己的日志频道。编辑你的config/logging.php:

    ....
    'channels' => [
        ....

        'your-channel-name' => [
            'driver' => 'daily',
            'path' => storage_path('logs/your-channel-log-file-name.log'),
            'level' => 'debug'
        ],
    ],
    ....

之后,您可以像这样将记录记录到your-channel-log-file-name.log 文件中:

\Log::channel('your-channel-name')->debug('This is my debug message');

这就像使用logger() 函数。

【讨论】:

  • 谢谢彼得。从来没有想过这种方法。顺便说一句,要禁用 Laravel 7.x 日志,在此解决方案中,只需在堆栈选项的通道中留下一个空数组:code 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => [], 'ignore_exceptions' => false, ],
【解决方案2】:

您可以尝试为您的问题提供解决方案。

请在您的控制器或类文件中包含以下命名空间。

use Carbon\Carbon;
use Illuminate\Support\Facades\File;

现在您可以为您的日志写入创建日志目录和文件。

$logPath = storage_path() . '/custom_logs/';
$log = $logPath . 'custom-file-log-'.Carbon::today()->format('d-m-Y') . '.log';        
if (!File::exists($logPath)) {
   File::makeDirectory($logPath, $mode = 0777, true, true);
}

现在将错误或客户信息写入日志文件。

您可以在日志文件中记录文本消息

$message = "It is first custom  log";
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".$message . PHP_EOL);

您可以在日志文件中记录数组。

$message = ['A'=>'B', 'C'=>'D'];
File::append($log, Carbon::now()->format('Y-m-d H:i:s')." -- ".print_r($message,true). PHP_EOL);

谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2020-01-17
    • 2016-07-04
    • 2011-01-08
    • 2014-07-30
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多