Created
July 9, 2021 19:27
-
-
Save spidgorny/ccbfe54634210e205ab648a73112648c to your computer and use it in GitHub Desktop.
Display only the last error from the Laravel log file and simplify output so that it fits the single screen output. Like "tail -f storage/logs/laravel.log", but smart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Storage; | |
class TailLog extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'tail:log'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Will tail -f storage/logs/laravel.log but show only one error. This prevents heavy scrolling to find the error.'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
// system('cls'); | |
// echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; | |
// $vLines = exec('tput lines'); // git bash only, gives 3000 | |
$vLines = exec('stty size'); // git bash only, gives 48 | |
list($vLines, $hCols) = trimExplode(' ', $vLines); | |
$vLines -= 5; | |
echo 'vLines: ', $vLines, 'x', $hCols, PHP_EOL; | |
$prevLastError = -1; | |
while (true) { | |
$filename = Storage::path('../logs/laravel.log'); | |
$output = $this->tailCustom($filename, 100); | |
$lines = trimExplode("\n", $output); | |
$lastError = 0; | |
foreach ($lines as $i => $line) { | |
if (str_contains($line, 'production.ERROR')) { | |
$lastError = $i; | |
// continue to find the last error | |
} | |
} | |
// avoid flicker when nothing changed | |
if ($lastError === $prevLastError) { | |
sleep(1); | |
continue; | |
} | |
echo 'Filename: ', $filename, PHP_EOL; | |
echo 'LastError: ', $lastError, PHP_EOL; | |
$lines = array_slice($lines, $lastError, $vLines); | |
$cwd = getcwd(); | |
foreach ($lines as $line) { | |
$line = str_replace('\\\\', '\\', $line); | |
$line = str_replace($cwd, '', $line); | |
if (strlen($line) > $hCols) { | |
$line = substr($line, 0, $hCols/2-3).'...'. | |
substr($line, -$hCols/2); | |
} | |
echo $line, PHP_EOL; | |
} | |
foreach (range(count($lines), $vLines) as $i) { | |
echo PHP_EOL; // cls | |
} | |
$prevLastError = $lastError; | |
sleep(5); | |
} | |
} | |
/** | |
* Slightly modified version of http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/ | |
* @return string[] | |
* @link http://stackoverflow.com/a/15025877/995958 | |
* @license http://creativecommons.org/licenses/by/3.0/ | |
* @author Torleif Berger, Lorenzo Stanco | |
*/ | |
function tailCustom($filepath, $lines = 1, $adaptive = true) | |
{ | |
// Open file | |
$f = @fopen($filepath, "rb"); | |
if ($f === false) return false; | |
// Sets buffer size, according to the number of lines to retrieve. | |
// This gives a performance boost when reading a few lines from the file. | |
if (!$adaptive) $buffer = 4096; | |
else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096)); | |
// Jump to last character | |
fseek($f, -1, SEEK_END); | |
// Read it and adjust line number if necessary | |
// (Otherwise the result would be wrong if file doesn't end with a blank line) | |
if (fread($f, 1) != "\n") $lines -= 1; | |
// Start reading | |
$output = ''; | |
$chunk = ''; | |
// While we would like more | |
while (ftell($f) > 0 && $lines >= 0) { | |
// Figure out how far back we should jump | |
$seek = min(ftell($f), $buffer); | |
// Do the jump (backwards, relative to where we are) | |
fseek($f, -$seek, SEEK_CUR); | |
// Read a chunk and prepend it to our output | |
$output = ($chunk = fread($f, $seek)) . $output; | |
// Jump back to where we started reading | |
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); | |
// Decrease our line counter | |
$lines -= substr_count($chunk, "\n"); | |
} | |
// While we have too many lines | |
// (Because of buffer size we might have read too many) | |
while ($lines++ < 0) { | |
// Find first newline and remove all text before that | |
$output = substr($output, strpos($output, "\n") + 1); | |
} | |
// Close file and return | |
fclose($f); | |
return trim($output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment