|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Tail; |
|
4
|
|
|
|
|
5
|
|
|
use SplFileInfo; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
|
|
10
|
|
|
class TailCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
protected $signature = 'tail |
|
13
|
|
|
{--lines=0 : Output the last number of lines} |
|
14
|
|
|
{--H|hide-stack-traces : Filter out the stack traces} |
|
15
|
|
|
{--clear : Clear the terminal screen}'; |
|
16
|
|
|
|
|
17
|
|
|
protected $description = 'Tail the latest logfile'; |
|
18
|
|
|
|
|
19
|
|
|
public function handle() |
|
20
|
|
|
{ |
|
21
|
|
|
$logDirectory = storage_path('logs'); |
|
22
|
|
|
|
|
23
|
|
|
if (! $path = $this->findLatestLogFile($logDirectory)) { |
|
24
|
|
|
$this->warn("Could not find a log file in `{$logDirectory}`."); |
|
25
|
|
|
|
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$lines = $this->option('lines'); |
|
30
|
|
|
|
|
31
|
|
|
$filters = $this->getFilters(); |
|
32
|
|
|
|
|
33
|
|
|
$tailCommand = "tail -f -n {$lines} {$filters} ".escapeshellarg($path); |
|
34
|
|
|
|
|
35
|
|
|
$this->handleClearOption(); |
|
36
|
|
|
|
|
37
|
|
|
(new Process($tailCommand)) |
|
38
|
|
|
->setTimeout(null) |
|
39
|
|
|
->run(function ($type, $line) { |
|
40
|
|
|
$this->handleClearOption(); |
|
41
|
|
|
|
|
42
|
|
|
$this->output->write($line); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function findLatestLogFile(string $directory) |
|
47
|
|
|
{ |
|
48
|
|
|
$logFile = collect(File::allFiles($directory)) |
|
49
|
|
|
->sortByDesc(function (SplFileInfo $file) { |
|
50
|
|
|
return $file->getMTime(); |
|
51
|
|
|
}) |
|
52
|
|
|
->first(); |
|
53
|
|
|
|
|
54
|
|
|
return $logFile |
|
55
|
|
|
? $logFile->getPathname() |
|
56
|
|
|
: false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function handleClearOption() |
|
60
|
|
|
{ |
|
61
|
|
|
if (! $this->option('clear')) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->output->write(sprintf("\033\143\e[3J")); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getFilters() |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->option('hide-stack-traces')) { |
|
71
|
|
|
return '| grep -i -E "^\[\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}\]|Next [\w\W]+?\:" --color'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function executeCommand($command) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$output = $this->output; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.