在 PHP 的 Laravel 中,如何清除 laravel.log?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28127495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PHP's Laravel, how do I clear laravel.log?
提问by Obelisk47
I'm new to Laravel and PHP in general, and I'm trying to clear my errorLog. The current errorlog that I'm using is Laravel's laravel.log file that is in /app/storage/logs.
我一般是 Laravel 和 PHP 的新手,我正在尝试清除我的错误日志。我当前使用的错误日志是 Laravel 的 laravel.log 文件,它位于 /app/storage/logs 中。
Is there an easy way to clear the laravel.log file? Is it safe to delete it and it'll rebuild when needed?
有没有简单的方法来清除 laravel.log 文件?删除它是否安全,它会在需要时重建?
I'm using the latest version of Ubuntu. Thanks!
我正在使用最新版本的 Ubuntu。谢谢!
回答by
Echoing an empty string to it would work, like this :
向它回显一个空字符串会起作用,如下所示:
echo "" > storage/logs/laravel.log
The most efficientwould be to truncate
it to a size of zero :
在最有效的将是truncate
它的大小为零:
truncate -s 0 /app/storage/logs/laravel.log
回答by emotality
Here's a reusable artisan
command that will save you time and effort :)
这是一个可重用的artisan
命令,可以节省您的时间和精力:)
Artisan::command('logs:clear', function() {
exec('rm ' . storage_path('logs/*.log'));
$this->comment('Logs have been cleared!');
})->describe('Clear log files');
Drop this in routes/console.php
then just run php artisan logs:clear
将其放入routes/console.php
然后运行php artisan logs:clear
回答by Yulin
Easiest way for me is using vim.
对我来说最简单的方法是使用 vim。
$ vim laravel.log Then type ggdG Then :wq save and quit.
Notes: gg - moves cursor to first line
注意: gg - 将光标移动到第一行
d - delete
d - 删除
G - to the end of the file
G - 到文件末尾
回答by Adnan Rasheed
Simply just add this line where you want to clear the log.
file_put_contents(storage_path('logs/laravel.log'),'');
只需在要清除日志的位置添加此行即可。
file_put_contents(storage_path('logs/laravel.log'),'');
回答by Fendi Setiawan
You can also create custom artisan command.
您还可以创建自定义工匠命令。
First, run command php artisan make:command Log/ClearLogFile
to create custom command file.
首先,运行命令php artisan make:command Log/ClearLogFile
来创建自定义命令文件。
Then, open file on Console/Commands/Log/ClearLogFile.php
(depends on you Laravel version, currently I'm using 5.5 version)
然后,打开文件Console/Commands/Log/ClearLogFile.php
(取决于您的 Laravel 版本,目前我使用的是 5.5 版本)
After that, you need to define the custom command code, take a look
之后,你需要定义自定义命令代码,看看
// 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');
}
Then, you only need to run just like other php artisan command,
然后,你只需要像其他 php artisan 命令一样运行,
php artisan log:clear
Thanks, for @emotality answer
谢谢,@emotality 的回答
回答by Kévin Ferradj
This worked for me :
这对我有用:
echo "" > storage/logs/laravel.log
回答by Faridul Khan
For Laravel 5, it would be
对于 Laravel 5,它将是
truncate -s 0 storage/logs/laravel.log
回答by Josh LaMar
From your Laravel directory:
从你的 Laravel 目录:
rm storage/logs/laravel-*.log
回答by Patrick.SE
I usually stick the following line in my controller method when I am running the SQL query listener:
当我运行 SQL 查询侦听器时,我通常会在我的控制器方法中添加以下行:
exec("truncate -s 0 " . storage_path('/logs/laravel.log'));
exec("truncate -s 0 " . storage_path('/logs/laravel.log'));
If you use a different monolog
channel setup you might want to tweak the name of your log file.
如果您使用不同的monolog
频道设置,您可能需要调整日志文件的名称。
回答by Pranta Saha
I found this solution works well for windows
我发现这个解决方案适用于 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 php artisan logs:clear
跑 php artisan logs:clear