-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDurableLogger.php
More file actions
153 lines (131 loc) · 5.62 KB
/
DurableLogger.php
File metadata and controls
153 lines (131 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
* Copyright ©2024 Robert Landers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Bottledcode\DurablePhp;
use Ahc\Cli\Output\Color;
use Ahc\Cli\Output\Writer;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Level;
use Monolog\Logger as Monologger;
use Monolog\LogRecord;
use Monolog\Processor\MemoryPeakUsageProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use function Amp\ByteStream\getOutputBufferStream;
class DurableLogger implements LoggerInterface
{
private ?Writer $writer = null;
private LogLevel $level;
public function __construct(private ?LoggerInterface $logger = null, private string|null $name = null, Level|null $level = null)
{
if ($logger === null) {
$handler = new StreamHandler(getOutputBufferStream());
//$handler = new StreamHandler(STDOUT);
//$handler->setFormatter(new ConsoleFormatter(allowInlineLineBreaks: true));
$handler->setFormatter(
new class (new ConsoleFormatter(
format: $this->name ? '%channel%.%level_name%: %message% %context% %extra%' : '[%datetime%] %level_name%: %message% %context% %extra%',
allowInlineLineBreaks: true,
ignoreEmptyContextAndExtra: true,
), $this->name) implements FormatterInterface {
private Color $colorize;
public function __construct(private FormatterInterface $innerFormat, private string|null $name = null)
{
$this->colorize = new Color();
}
#[\Override]
public function format(LogRecord $record): string
{
$color = match ($record->level) {
Level::Debug, Level::Notice => fn(string $text) => $this->colorize->comment($text),
Level::Info => fn(string $text) => $this->colorize->info($text),
Level::Warning, Level::Alert => fn(string $text) => $this->colorize->warn($text),
Level::Critical, Level::Error, Level::Emergency => fn(
string $text,
) => $this->colorize->error($text),
};
return $color($this->innerFormat->format($record->with(channel: $this->name))) . "\033[0m\n";
}
#[\Override]
public function formatBatch(array $records): string
{
return implode('', array_map($this->format(...), $records));
}
},
);
$handler->pushProcessor(new MemoryPeakUsageProcessor(true, true));
$handler->pushProcessor(new ProcessIdProcessor());
$handler->setLevel(getenv('LOG_LEVEL') ?: ($level ?? Level::Warning));
$logger = new Monologger('main');
$logger->pushHandler($handler);
$this->logger = $logger;
}
}
#[\Override]
public function info(string|\Stringable $message, array $context = []): void
{
$this->logger->info($message, $context);
}
#[\Override]
public function error(string|\Stringable $message, array $context = []): void
{
$this->logger->error($message, $context);
}
#[\Override]
public function emergency(string|\Stringable $message, array $context = []): void
{
$this->logger->emergency($message, $context);
}
#[\Override]
public function alert(string|\Stringable $message, array $context = []): void
{
$this->logger->alert($message, $context);
}
#[\Override]
public function critical(string|\Stringable $message, array $context = []): void
{
$this->logger->critical($message, $context);
}
#[\Override]
public function warning(string|\Stringable $message, array $context = []): void
{
$this->logger->warning($message, $context);
}
#[\Override]
public function notice(string|\Stringable $message, array $context = []): void
{
$this->logger->notice($message, $context);
}
#[\Override]
public function debug(string|\Stringable $message, array $context = []): void
{
$this->logger->debug($message, $context);
}
#[\Override]
public function log($level, string|\Stringable $message, array $context = []): void
{
$this->logger->log($level, $message, $context);
}
}