我是Laravel和PHP的新手,我正在尝试清除我的错误日志。我正在使用的当前错误日志是 Laravel 的 laravel.log 文件,该文件位于/app/storage/logs 中。
有没有一种简单的方法可以清除 laravel.log 文件?删除它并在需要时重建是否安全?
我使用的是最新版本的 Ubuntu。谢谢!
空字符串回显到它就可以了,如下所示:
echo "" > storage/logs/laravel.log
最有效的方法是将其truncate
为零:
truncate -s 0 /app/storage/logs/laravel.log
这是一个可重用的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.php
然后运行php artisan logs:clear
更新:
- 为根目录中的
npm
、composer
等日志添加了base_path('*.log')
。 - 向
rm
功能添加了-f
以禁止显示No such file or directory
消息。
只需在要清除日志的位置添加此行即可。 file_put_contents(storage_path('logs/laravel.log'),'');
最简单的方法是使用 vim。
$ vim laravel.log然后键入 ggdG然后:wq保存并退出。
笔记:GG - 将光标移动到第一行
d - 删除
G - 到文件末尾
您还可以创建自定义工匠命令。
首先,运行命令php artisan make:command Log/ClearLogFile
以创建自定义命令文件。
然后,在Console/Commands/Log/ClearLogFile.php
上打开文件(取决于您的Laravel版本,目前我使用的是5.5版本)
之后,您需要定义自定义命令代码,看看
// Define command name
protected $signature = 'log:clear';
// Add description to your command
protected $description = 'Clear Laravel log';
// Create your own custom command
public function handle(){
exec('echo "" > ' . storage_path('logs/laravel.log'));
$this->info('Logs have been cleared');
}
然后,你只需要像其他php工匠命令一样运行,
php artisan log:clear
谢谢,@emotality答案
我有用:
echo "" > storage/logs/laravel.log
从你的Laravel目录:
rm storage/logs/laravel-*.log
我发现这个解决方案适用于窗户
Artisan::command('logs:clear', function() {
array_map('unlink', array_filter((array) glob(storage_path('logs/*.log'))));
$this->comment('Logs have been cleared!');
})->describe('Clear log files');
运行php artisan logs:clear
对于 Laravel 5,它将是
truncate -s 0 storage/logs/laravel.log
做: : > storage/logs/laravel.log
当我运行 SQL 查询侦听器时,我通常会在我的控制器方法中粘贴以下行:
exec("truncate -s 0 " . storage_path('/logs/laravel.log'));
如果使用不同的monolog
通道设置,则可能需要调整日志文件的名称。
有非常简单的方法可以使用Laravel More Command Package清除php artisan log:clear
命令。
首先安装拉拉维尔更多命令
composer require theanik/laravel-more-command --dev
然后运行
php artisan log:clear
以上将从/storage/logs/ 目录中删除所有旧的日志数据。
谢谢。
完成@emotality响应。这是Windows的代码。
Artisan::command('logs:clear', function() {
exec('echo "" > ' . storage_path('logs/laravel.log'));
$this->info('Logs have been cleared');
})->describe('Clear log files');
把它放进routes/console.php
并像这样使用它:
php artisan logs:clear
转到修补php artisan tinker
然后运行
use Illuminate'Support'Facades'Redis;
Redis::resolve('horizon')->flushall();
预期响应:
> Redis::resolve('horizon')->flushall();
= true
>
我只是在清除之前添加一点以确保。
Artisan::command('log:clear', function() {
if ($this->confirm('Do you wish to continue?')) {
exec('echo "" > ' . storage_path('logs/laravel.log'));
$this->info('Logs have been cleared');
}
})->describe('Clear Laravel log');