|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Network\Server; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Network\Server; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class LogWriter implements OnCloseListener, OnConnectListener, OnShutdownListener, OnStartListener, LoggerInterface |
|
9
|
|
|
{ |
|
10
|
|
|
private $logger; |
|
11
|
|
|
|
|
12
|
21 |
|
public function __construct(LoggerInterface $logger = null) |
|
13
|
|
|
{ |
|
14
|
21 |
|
$this->logger = $logger ?? new Logger(); |
|
15
|
21 |
|
} |
|
16
|
|
|
|
|
17
|
5 |
|
public function onClose(Server $server, Client $client): void |
|
18
|
|
|
{ |
|
19
|
5 |
|
$this->info( |
|
20
|
5 |
|
'Client {client} closed connection', |
|
21
|
5 |
|
['client' => $client] |
|
22
|
|
|
); |
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
7 |
|
public function onConnect(Server $server, Client $client): void |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->info( |
|
28
|
7 |
|
'Client {client} connected', |
|
29
|
7 |
|
['client' => $client] |
|
30
|
|
|
); |
|
31
|
7 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function onShutdown(Server $server): void |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->alert('Server shutdown'); |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function onStart(Server $server): void |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->info( |
|
41
|
2 |
|
'Server is listening {address}:{port}', |
|
42
|
2 |
|
['port' => $server->getConfiguration()->getPort(), 'address' => $server->getConfiguration()->getAddress()] |
|
43
|
|
|
); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getLogger(): LoggerInterface |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->logger; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function emergency($message, array $context = []) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->logger->emergency($message, $context); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function alert($message, array $context = []) |
|
57
|
|
|
{ |
|
58
|
3 |
|
$this->logger->alert($message, $context); |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function critical($message, array $context = []) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->logger->critical($message, $context); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function error($message, array $context = []) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->logger->error($message, $context); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function warning($message, array $context = []) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->logger->warning($message, $context); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function notice($message, array $context = []) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->logger->notice($message, $context); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
11 |
|
public function info($message, array $context = []) |
|
82
|
|
|
{ |
|
83
|
11 |
|
$this->logger->info($message, $context); |
|
84
|
11 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function debug($message, array $context = []) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->logger->debug($message, $context); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function log($level, $message, array $context = []) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->logger->log($level, $message, $context); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|