Skip to content

Commit 0757d78

Browse files
committed
refactor(logging)!: dissociate tempest/debug from tempest/log
1 parent 4965d21 commit 0757d78

22 files changed

+258
-324
lines changed

packages/console/src/Commands/TailCommand.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/console/src/Commands/TailServerLogCommand.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

packages/debug/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"minimum-stability": "dev",
66
"require": {
77
"php": "^8.4",
8+
"tempest/console": "dev-main",
89
"tempest/highlight": "^2.11.4",
910
"symfony/var-dumper": "^7.1"
1011
},

packages/debug/src/Debug.php

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,34 @@
1111
use Tempest\Container\GenericContainer;
1212
use Tempest\EventBus\EventBus;
1313
use Tempest\Highlight\Themes\TerminalStyle;
14-
use Tempest\Log\LogConfig;
14+
use Tempest\Support\Filesystem;
15+
use Tempest\Support\Filesystem\Exceptions\RuntimeException;
1516

1617
final readonly class Debug
1718
{
1819
private function __construct(
19-
private ?LogConfig $logConfig = null,
20+
private ?DebugConfig $config = null,
2021
private ?EventBus $eventBus = null,
2122
) {}
2223

2324
public static function resolve(): self
2425
{
2526
try {
26-
$container = GenericContainer::instance();
27-
2827
return new self(
29-
logConfig: $container?->get(LogConfig::class),
30-
eventBus: $container?->get(EventBus::class),
28+
config: GenericContainer::instance()->get(DebugConfig::class),
29+
eventBus: GenericContainer::instance()->get(EventBus::class),
3130
);
3231
} catch (Exception) {
3332
return new self();
3433
}
3534
}
3635

36+
/**
37+
* Logs and/or dumps the given items.
38+
*
39+
* @param bool $writeToLog Whether to write the items to the log file.
40+
* @param bool $writeToOut Whether to dump the items to the standard output.
41+
*/
3742
public function log(array $items, bool $writeToLog = true, bool $writeToOut = true): void
3843
{
3944
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@@ -52,30 +57,23 @@ public function log(array $items, bool $writeToLog = true, bool $writeToOut = tr
5257

5358
private function writeToLog(array $items, string $callPath): void
5459
{
55-
if ($this->logConfig === null) {
60+
if ($this->config === null) {
5661
return;
5762
}
5863

59-
if (! $this->logConfig->debugLogPath) {
64+
if (! $this->config->logPath) {
6065
return;
6166
}
6267

63-
$directory = dirname($this->logConfig->debugLogPath);
64-
65-
if (! is_dir($directory)) {
66-
mkdir(directory: $directory, recursive: true);
67-
}
68+
Filesystem\create_directory_for_file($this->config->logPath);
6869

69-
$handle = @fopen($this->logConfig->debugLogPath, 'a');
70-
71-
if (! $handle) {
70+
if (! ($handle = @fopen($this->config->logPath, 'a'))) {
7271
return;
7372
}
7473

7574
foreach ($items as $key => $item) {
76-
$output = $this->createDump($item) . $callPath;
77-
78-
fwrite($handle, "{$key} " . $output . PHP_EOL);
75+
fwrite($handle, TerminalStyle::BG_BLUE(" {$key} ") . TerminalStyle::FG_GRAY('' . TerminalStyle::ITALIC($callPath)));
76+
fwrite($handle, $this->createCliDump($item) . PHP_EOL);
7977
}
8078

8179
fclose($handle);
@@ -86,27 +84,27 @@ private function writeToOut(array $items, string $callPath): void
8684
foreach ($items as $key => $item) {
8785
if (defined('STDOUT')) {
8886
fwrite(STDOUT, TerminalStyle::BG_BLUE(" {$key} ") . ' ');
89-
90-
$output = $this->createDump($item);
91-
92-
fwrite(STDOUT, $output);
93-
94-
fwrite(STDOUT, $callPath . PHP_EOL);
87+
fwrite(STDOUT, $this->createCliDump($item));
88+
fwrite(STDOUT, TerminalStyle::DIM('' . TerminalStyle::ITALIC($callPath)) . PHP_EOL . PHP_EOL);
9589
} else {
9690
echo
97-
sprintf(
98-
'<span style="
99-
display:inline-block;
100-
color: #fff;
101-
font-family: %s;
102-
padding: 2px 4px;
103-
font-size: 0.8rem;
104-
margin-bottom: -12px;
105-
background: #0071BC;"
106-
>%s (%s)</span>',
107-
'Source Code Pro, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
108-
$key,
109-
$callPath,
91+
vsprintf(
92+
<<<HTML
93+
<span style="
94+
display:inline-block;
95+
color: #fff;
96+
font-family: %s;
97+
padding: 2px 4px;
98+
font-size: 0.8rem;
99+
margin-bottom: -12px;
100+
background: #0071BC;"
101+
>%s (%s)</span>
102+
HTML,
103+
[
104+
'Source Code Pro, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
105+
$key,
106+
$callPath,
107+
],
110108
)
111109
;
112110

@@ -115,10 +113,9 @@ private function writeToOut(array $items, string $callPath): void
115113
}
116114
}
117115

118-
private function createDump(mixed $input): string
116+
private function createCliDump(mixed $input): string
119117
{
120118
$cloner = new VarCloner();
121-
122119
$output = '';
123120

124121
$dumper = new CliDumper(function ($line, $depth) use (&$output): void {
@@ -130,7 +127,6 @@ private function createDump(mixed $input): string
130127
});
131128

132129
$dumper->setColors(true);
133-
134130
$dumper->dump($cloner->cloneVar($input));
135131

136132
return preg_replace(

packages/debug/src/DebugConfig.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tempest\Debug;
4+
5+
final class DebugConfig
6+
{
7+
/**
8+
* @param string $logPath The file path where debug logs will be written.
9+
*/
10+
public function __construct(
11+
public readonly string $logPath,
12+
) {}
13+
}

packages/console/src/Commands/TailDebugLogCommand.php renamed to packages/debug/src/TailDebugCommand.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,44 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Console\Commands;
5+
namespace Tempest\Debug;
66

77
use Tempest\Console\Console;
88
use Tempest\Console\ConsoleCommand;
99
use Tempest\Console\Highlight\VarExportLanguage\VarExportLanguage;
1010
use Tempest\Console\Output\TailReader;
1111
use Tempest\Container\Tag;
1212
use Tempest\Highlight\Highlighter;
13-
use Tempest\Log\LogConfig;
13+
use Tempest\Support\Filesystem;
1414

15-
final readonly class TailDebugLogCommand
15+
final readonly class TailDebugCommand
1616
{
1717
public function __construct(
1818
private Console $console,
19-
private LogConfig $logConfig,
19+
private DebugConfig $debugConfig,
2020
#[Tag('console')]
2121
private Highlighter $highlighter,
2222
) {}
2323

24-
#[ConsoleCommand('tail:debug', description: 'Tails the debug log')]
25-
public function __invoke(): void
24+
#[ConsoleCommand('tail:debug', description: 'Tails the debug log', aliases: ['debug:tail'])]
25+
public function __invoke(bool $clear = false): void
2626
{
27-
$debugLogPath = $this->logConfig->debugLogPath;
27+
$debugLogPath = $this->debugConfig->logPath;
2828

2929
if (! $debugLogPath) {
30-
$this->console->error('No debug log configured in <code>LogConfig</code>.');
30+
$this->console->error('No debug log configured in <code>DebugConfig</code>.');
3131

3232
return;
3333
}
3434

35-
$dir = pathinfo($debugLogPath, PATHINFO_DIRNAME);
36-
37-
if (! is_dir($dir)) {
38-
mkdir($dir);
35+
if ($clear && Filesystem\is_file($debugLogPath)) {
36+
Filesystem\delete_file($debugLogPath);
3937
}
4038

41-
if (! file_exists($debugLogPath)) {
42-
touch($debugLogPath);
43-
}
39+
Filesystem\create_file($debugLogPath);
4440

4541
$this->console->header('Tailing debug logs', "Reading <file='{$debugLogPath}'/>…");
4642

47-
new TailReader()->tail(
48-
path: $debugLogPath,
49-
format: fn (string $text) => $this->highlighter->parse(
50-
$text,
51-
new VarExportLanguage(),
52-
),
53-
);
43+
new TailReader()->tail($debugLogPath);
5444
}
5545
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tempest\Debug\DebugConfig;
4+
5+
return new DebugConfig(
6+
logPath: Tempest\internal_storage_path('logs', 'debug.log'),
7+
);

packages/log/src/Channels/AppendLogChannel.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,31 @@
88
use Monolog\Level;
99
use Monolog\Processor\PsrLogMessageProcessor;
1010
use Tempest\Log\LogChannel;
11+
use Tempest\Log\LogLevel;
1112

1213
final readonly class AppendLogChannel implements LogChannel
1314
{
15+
/**
16+
* @param string $path The log file path.
17+
* @param bool $useLocking Whether to try to lock log file before doing any writes.
18+
* @param LogLevel $minimumLogLevel The minimum log level to record.
19+
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
20+
* @param null|int $filePermission Optional file permissions (default (0644) are only for owner read/write).
21+
*/
1422
public function __construct(
1523
private string $path,
24+
private bool $useLocking = false,
25+
private LogLevel $minimumLogLevel = LogLevel::DEBUG,
1626
private bool $bubble = true,
1727
private ?int $filePermission = null,
18-
private bool $useLocking = false,
1928
) {}
2029

2130
public function getHandlers(Level $level): array
2231
{
32+
if (! $this->minimumLogLevel->includes(LogLevel::fromMonolog($level))) {
33+
return [];
34+
}
35+
2336
return [
2437
new StreamHandler(
2538
stream: $this->path,

0 commit comments

Comments
 (0)