在 PHP 的 Laravel 中,如何清除 laravel.log?

2022-08-30 09:59:45

我是Laravel和PHP的新手,我正在尝试清除我的错误日志。我当前使用的错误日志是 Laravel 的 laravel.log文件,该文件位于 /app/storage/logs 中。

有没有一种简单的方法来清除laravel.log文件?删除它是否安全,并且在需要时会重新生成?

我使用的是最新版本的 Ubuntu。谢谢!


答案 1

向它回显一个空字符串会起作用,如下所示:

echo "" > storage/logs/laravel.log

最有效的方法是将其大小设置为零:truncate

truncate -s 0 /app/storage/logs/laravel.log

答案 2

这是一个可重用的命令,可以节省您的时间和精力:)artisan

Artisan::command('logs:clear', function() {
    
    exec('rm -f ' . storage_path('logs/*.log'));

    exec('rm -f ' . base_path('*.log'));
    
    $this->comment('Logs have been cleared!');
    
})->describe('Clear log files');

把这个放进去,然后跑routes/console.phpphp artisan logs:clear


更新:

  • 在根目录中添加了 for 等日志。base_path('*.log')npmcomposer
  • 添加到函数以禁止显示消息。-frmNo such file or directory

推荐