1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace PerfectApp\Logger; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
use Stringable; |
9
|
|
|
|
10
|
|
|
class EmailLogger implements LoggerInterface |
11
|
|
|
{ |
12
|
10 |
|
public function __construct( |
13
|
|
|
private readonly string $toEmail, |
14
|
|
|
private readonly string $fromEmail, |
15
|
|
|
private MailSenderInterface $mailSender |
16
|
|
|
) |
17
|
|
|
{ |
18
|
10 |
|
} |
19
|
|
|
|
20
|
10 |
|
public function log(mixed $level, Stringable|string $message, array $context = []): void |
21
|
|
|
{ |
22
|
10 |
|
if (!in_array($level, [ |
23
|
10 |
|
LogLevel::EMERGENCY, |
24
|
10 |
|
LogLevel::ALERT, |
25
|
10 |
|
LogLevel::CRITICAL, |
26
|
10 |
|
LogLevel::ERROR, |
27
|
10 |
|
LogLevel::WARNING, |
28
|
10 |
|
LogLevel::NOTICE, |
29
|
10 |
|
LogLevel::INFO, |
30
|
10 |
|
LogLevel::DEBUG |
31
|
10 |
|
])) { |
32
|
1 |
|
throw new InvalidArgumentException('Invalid log level.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
$subject = strtoupper($level) . ' log message'; |
36
|
9 |
|
$headers = 'From: ' . $this->fromEmail . "\r\n" . |
37
|
9 |
|
'X-Mailer: PHP/' . phpversion(); |
38
|
|
|
|
39
|
9 |
|
$this->sendMail($this->toEmail, $subject, $message . "\n" . json_encode($context), $headers); |
40
|
|
|
} |
41
|
|
|
|
42
|
9 |
|
protected function sendMail(string $to, string $subject, string $message, string $headers): bool |
43
|
|
|
{ |
44
|
9 |
|
return $this->mailSender->send($to, $subject, $message, $headers); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function emergency(Stringable|string $message, array $context = []): void |
48
|
|
|
{ |
49
|
1 |
|
$this->log(LogLevel::EMERGENCY, $message, $context); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function alert(Stringable|string $message, array $context = []): void |
53
|
|
|
{ |
54
|
1 |
|
$this->log(LogLevel::ALERT, $message, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function critical(Stringable|string $message, array $context = []): void |
58
|
|
|
{ |
59
|
1 |
|
$this->log(LogLevel::CRITICAL, $message, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function error(Stringable|string $message, array $context = []): void |
63
|
|
|
{ |
64
|
1 |
|
$this->log(LogLevel::ERROR, $message, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function warning(Stringable|string $message, array $context = []): void |
68
|
|
|
{ |
69
|
1 |
|
$this->log(LogLevel::WARNING, $message, $context); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function notice(Stringable|string $message, array $context = []): void |
73
|
|
|
{ |
74
|
1 |
|
$this->log(LogLevel::NOTICE, $message, $context); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function info(Stringable|string $message, array $context = []): void |
78
|
|
|
{ |
79
|
1 |
|
$this->log(LogLevel::INFO, $message, $context); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function debug(Stringable|string $message, array $context = []): void |
83
|
|
|
{ |
84
|
1 |
|
$this->log(LogLevel::DEBUG, $message, $context); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|