我是Laravel和PHP的新手,我正在尝试清除我的errorLog.我正在使用的当前错误日志是/ app/storage/logs中的Laravel的laravel.log文件.
有没有简单的方法来清除laravel.log文件?删除它是否安全,并在需要时重建?
我正在使用最新版本的Ubuntu.谢谢!
小智 57
回应一个空字符串会起作用,如下所示:
echo "" > storage/logs/laravel.log
Run Code Online (Sandbox Code Playgroud)
在最有效的将是truncate它的大小为零:
truncate -s 0 /app/storage/logs/laravel.log
Run Code Online (Sandbox Code Playgroud)
emo*_*ity 19
这是一个可重复使用的artisan命令,可以节省您的时间和精力:)
Artisan::command('logs:clear', function() {
exec('rm ' . storage_path('logs/*.log'));
$this->comment('Logs have been cleared!');
})->describe('Clear log files');
Run Code Online (Sandbox Code Playgroud)
放下它routes/console.php然后运行php artisan logs:clear
对我来说最简单的方法就是使用vim.
$ vim laravel.log Then type ggdG Then :wq save and quit.
注意: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');
}
Run Code Online (Sandbox Code Playgroud)
然后,你只需要像其他 php artisan 命令一样运行,
php artisan log:clear
Run Code Online (Sandbox Code Playgroud)
谢谢,@emotality 的回答
我发现这个解决方案适用于 Windows
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');
Run Code Online (Sandbox Code Playgroud)
跑步php artisan logs:clear