|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource\SemanticLog; |
|
6
|
|
|
|
|
7
|
|
|
use Koriym\SemanticLogger\LogJson; |
|
8
|
|
|
use Koriym\SemanticLogger\SemanticLoggerInterface; |
|
9
|
|
|
use Ray\Di\Di\Named; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
|
|
12
|
|
|
use function date; |
|
13
|
|
|
use function file_put_contents; |
|
14
|
|
|
use function getmypid; |
|
15
|
|
|
use function json_encode; |
|
16
|
|
|
use function sprintf; |
|
17
|
|
|
use function sys_get_temp_dir; |
|
18
|
|
|
use function uniqid; |
|
19
|
|
|
|
|
20
|
|
|
use const JSON_PRETTY_PRINT; |
|
21
|
|
|
use const LOCK_EX; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Development log persister for MCP server integration |
|
25
|
|
|
* |
|
26
|
|
|
* Handles file persistence of semantic logs with Profile data for AI-assisted debugging. |
|
27
|
|
|
* Uses single responsibility principle - only concerned with log persistence. |
|
28
|
|
|
*/ |
|
29
|
|
|
final class DevLogPersister |
|
30
|
|
|
{ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
#[Named('dev_log_directory')] |
|
33
|
|
|
private string $logDirectory = '', |
|
34
|
|
|
) { |
|
35
|
|
|
// Use system temp directory if no directory specified |
|
36
|
|
|
if ($this->logDirectory !== '') { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->logDirectory = sys_get_temp_dir(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Persist semantic logs to file for development/debugging purposes |
|
45
|
|
|
*/ |
|
46
|
|
|
public function persistLogs(SemanticLoggerInterface $logger): void |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$logData = $logger->flush(); |
|
50
|
|
|
$this->saveToFile($logData); |
|
51
|
|
|
} catch (Throwable) { |
|
52
|
|
|
// Silent failure - don't break main processing |
|
53
|
|
|
// In development, log persistence should never affect application flow |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function saveToFile(LogJson $logData): void |
|
58
|
|
|
{ |
|
59
|
|
|
$jsonContent = json_encode($logData, JSON_PRETTY_PRINT); |
|
60
|
|
|
if ($jsonContent === false) { |
|
61
|
|
|
return; // Skip if JSON encoding fails |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$filename = $this->generateFilename(); |
|
65
|
|
|
file_put_contents($filename, $jsonContent, LOCK_EX); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Generate unique filename for concurrent request safety |
|
70
|
|
|
*/ |
|
71
|
|
|
private function generateFilename(): string |
|
72
|
|
|
{ |
|
73
|
|
|
$timestamp = date('Y-m-d_H-i-s-u'); // Microseconds for concurrency |
|
74
|
|
|
$processId = getmypid(); // Process ID for true uniqueness |
|
75
|
|
|
$uniqueId = uniqid(); |
|
76
|
|
|
|
|
77
|
|
|
return sprintf( |
|
78
|
|
|
'%s/semantic-dev-%s-%s-%s.json', |
|
79
|
|
|
$this->logDirectory, |
|
80
|
|
|
$timestamp, |
|
81
|
|
|
$processId !== false ? $processId : 'unknown', |
|
82
|
|
|
$uniqueId, |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|