Skip to content

Commit

Permalink
Add Tail and server log commands
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 25, 2024
1 parent dfca3c5 commit 049d277
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 2 deletions.
28 changes: 28 additions & 0 deletions log/debug.log
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,31 @@ Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandle
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"""
Cannot resolve Tempest\Log\Logger because it is not an instantiable class. Maybe it's missing an initializer class?\n
\n
\t LoggerCommand::__construct(Logger $logger)Originally called in /Users/brent/Dev/tempest-console/src/Actions/ExecuteConsoleCommand.php:43\n
"""

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
[exception]
"A message from the exception default"

Called in /Users/brent/Dev/tempest-console/src/Exceptions/ConsoleExceptionHandler.php:51
8 changes: 8 additions & 0 deletions log/tempest.log
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
[2024-05-24T19:09:39.792849+00:00] tempest.DEBUG: [0] 'hi' [] []
[2024-05-24T19:09:40.324998+00:00] tempest.DEBUG: [0] 'hi' [] []
[2024-05-24T19:09:55.203969+00:00] tempest.DEBUG: [0] \Tempest\Console\Commands\LogCommands::__set_state(array( 'console' => \Tempest\Console\GenericConsole::__set_state(array( 'label' => NULL, 'isForced' => false, 'componentRenderer' => \Tempest\Console\Components\InteractiveComponentRenderer::__set_state(array( 'validationErrors' => array ( ), )), 'output' => \Tempest\Console\Output\StdoutOutputBuffer::__set_state(array( )), 'input' => \Tempest\Console\Input\StdinInputBuffer::__set_state(array( )), 'highlighter' => \Tempest\Highlight\Highlighter::__set_state(array( 'languages' => array ( 'blade' => \Tempest\Highlight\Languages\Blade\BladeLanguage::__set_state(array( )), 'css' => \Tempest\Highlight\Languages\Css\CssLanguage::__set_state(array( )), 'diff' => \Tempest\Highlight\Languages\Diff\DiffLanguage::__set_state(array( )), 'doc' => \Tempest\Highlight\Languages\DocComment\DocCommentLanguage::__set_state(array( )), 'ellison' => \Tempest\Highlight\Languages\Ellison\EllisonLanguage::__set_state(array( )), 'gdscript' => \Tempest\Highlight\Languages\Gdscript\GdscriptLanguage::__set_state(array( )), 'html' => \Tempest\Highlight\Languages\Html\HtmlLanguage::__set_state(array( )), 'js' => \Tempest\Highlight\Languages\JavaScript\JavaScriptLanguage::__set_state(array( )), 'javascript' => \Tempest\Highlight\Languages\JavaScript\JavaScriptLanguage::__set_state(array( )), 'node' => \Tempest\Highlight\Languages\JavaScript\JavaScriptLanguage::__set_state(array( )), 'json' => \Tempest\Highlight\Languages\Json\JsonLanguage::__set_state(array( )), 'php' => \Tempest\Highlight\Languages\Php\PhpLanguage::__set_state(array( )), 'python' => \Tempest\Highlight\Languages\Python\PythonLanguage::__set_state(array( )), 'py' => \Tempest\Highlight\Languages\Python\PythonLanguage::__set_state(array( )), 'sql' => \Tempest\Highlight\Languages\Sql\SqlLanguage::__set_state(array( )), 'xml' => \Tempest\Highlight\Languages\Xml\XmlLanguage::__set_state(array( )), 'yaml' => \Tempest\Highlight\Languages\Yaml\YamlLanguage::__set_state(array( )), 'yml' => \Tempest\Highlight\Languages\Yaml\YamlLanguage::__set_state(array( )), 'twig' => \Tempest\Highlight\Languages\Twig\TwigLanguage::__set_state(array( )), ), 'gutterInjection' => NULL, 'currentLanguage' => NULL, 'isNested' => false, 'theme' => \Tempest\Console\Highlight\TempestTerminalTheme::__set_state(array( )), )), )), 'logConfig' => \Tempest\Log\LogConfig::__set_state(array( 'channels' => array ( 0 => \Tempest\Log\Channels\AppendLogChannel::__set_state(array( 'path' => '/Users/brent/Dev/tempest-console/log/tempest.log', 'bubble' => true, 'filePermission' => NULL, 'useLocking' => false, )), ), 'prefix' => 'tempest', )), )) [] []
[2024-05-25T10:34:57.471122+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:34:58.775535+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:34:59.418017+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:35:00.017464+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:35:00.562681+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:35:01.203978+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:35:02.036984+00:00] tempest.INFO: From the log [] []
[2024-05-25T10:37:41.346138+00:00] tempest.INFO: From the log [] []
2 changes: 1 addition & 1 deletion src/Commands/LogDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __invoke(): void
touch($debugLogPath);
}

$this->console->writeln("<h2>Debug</h2> Listening for logs, use <em><strong>ll()</strong></em>, <em><strong>lw()</strong></em>, or <em><strong>ld()</strong></em>");
$this->console->writeln("<h1>Debug</h1> Listening for logs, use <em><strong>ll()</strong></em>, <em><strong>lw()</strong></em>, or <em><strong>ld()</strong></em>");

(new TailReader())->tail(
$debugLogPath,
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/LogProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\Output\TailReader;
use Tempest\Log\Channels\AppendLogChannel;
use Tempest\Log\LogConfig;

Expand All @@ -22,7 +23,8 @@ public function __invoke(): void
{
foreach ($this->logConfig->channels as $channel) {
if ($channel instanceof AppendLogChannel) {
passthru("tail -f {$channel->getPath()}");
$this->console->writeln("<h1>Log</h1> Listening for logs…");
(new TailReader())->tail($channel->getPath());
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/Commands/LogServerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Commands;

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\Highlight\VarExportLanguage\VarExportLanguage;
use Tempest\Console\Output\TailReader;
use Tempest\Log\LogConfig;

final readonly class LogServerCommand
{
public function __construct(
private Console $console,
private LogConfig $logConfig,
) {
}

#[ConsoleCommand('log:server', aliases: ['ls'])]
public function __invoke(): void
{
$serverLogPath = $this->logConfig->serverLogPath;

if (! $serverLogPath) {
$this->console->error("No server log configured in LogConfig");

return;
}

if (! file_exists($serverLogPath)) {
$this->console->error("No valid server log at <em>{$serverLogPath}</em>");

return;
}

$this->console->writeln("<h1>Server</h1> Listening for logs…");

(new TailReader())->tail($serverLogPath);
}
}
66 changes: 66 additions & 0 deletions src/Commands/TailCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Commands;

use Fiber;
use Tempest\Console\ConsoleArgument;
use Tempest\Console\ConsoleCommand;

final readonly class TailCommand
{
public function __construct(
private LogDebugCommand $logDebugCommand,
private LogProjectCommand $logProjectCommand,
private LogServerCommand $logServerCommand,
) {}

#[ConsoleCommand(
name: 'log:tail',
description: 'Tail multiple logs',
aliases: ['tail'],
)]
public function __invoke(
#[ConsoleArgument(description: 'Include the debug log', aliases: ['-d'])]
?bool $debug = null,

#[ConsoleArgument(description: 'Include the project log', aliases: ['-p'])]
?bool $project = null,

#[ConsoleArgument(description: 'Include the server log', aliases: ['-s'])]
?bool $server = null
): void {
$shouldFilter = $debug !== null
|| $project !== null
|| $server !== null;

/** @var array<array-key, \Tempest\Console\Commands\LogDebugCommand|\Tempest\Console\Commands\LogProjectCommand> $loggers */
$loggers = array_filter([
($shouldFilter === false || $debug) ? $this->logDebugCommand : null,
($shouldFilter === false || $project) ? $this->logProjectCommand : null,
($shouldFilter === false || $server) ? $this->logServerCommand : null,
]);

/** @var Fiber[] $fibers */
$fibers = [];

foreach ($loggers as $key => $logger) {
$fiber = new Fiber(fn () => ($logger)());
$fibers[$key] = $fiber;
$fiber->start();
}

while ($fibers !== []) {
foreach ($fibers as $key => $fiber) {
if ($fiber->isSuspended()) {
$fiber->resume();
}

if ($fiber->isTerminated()) {
unset($fibers[$key]);
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/Output/TailReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tempest\Console\Output;

use Closure;
use Fiber;

final readonly class TailReader
{
Expand All @@ -16,6 +17,10 @@ public function tail(string $path, ?Closure $format = null): void

/** @phpstan-ignore-next-line */
while (true) {
if (Fiber::getCurrent()) {
Fiber::suspend();
}

fseek($handle, -1, SEEK_END);
$newOffset = ftell($handle);

Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/LoggerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Console\Fixtures;

use Tempest\Console\ConsoleCommand;
use Tempest\Log\Logger;

final readonly class LoggerCommand
{
public function __construct(private Logger $logger) {}

#[ConsoleCommand('logger')]
public function __invoke(): void
{
$this->logger->info('From the log');
}
}

0 comments on commit 049d277

Please # to comment.