| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Logger\Monolog; |
| 22 |
|
| 23 |
use LogicException; |
| 24 |
use MediaWiki\Logger\LegacyLogger; |
| 25 |
use Monolog\Handler\AbstractProcessingHandler; |
| 26 |
use Monolog\Logger; |
| 27 |
use Socket; |
| 28 |
use UnexpectedValueException; |
| 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 |
s |
| 59 |
|
| 60 |
class LegacyHandler extends AbstractProcessingHandler { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected $uri; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected $useLegacyFilter; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
protected $sink; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected $error; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected $host; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected $port; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
protected $prefix; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function __construct( |
| 107 |
$stream, |
| 108 |
$useLegacyFilter = false, |
| 109 |
$level = Logger::DEBUG, |
| 110 |
$bubble = true |
| 111 |
) { |
| 112 |
parent::__construct( $level, $bubble ); |
| 113 |
$this->uri = $stream; |
| 114 |
$this->useLegacyFilter = $useLegacyFilter; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
protected function openSink() { |
| 121 |
if ( !$this->uri ) { |
| 122 |
throw new LogicException( |
| 123 |
'Missing stream uri, the stream can not be opened.' ); |
| 124 |
} |
| 125 |
$this->error = null; |
| 126 |
set_error_handler( [ $this, 'errorTrap' ] ); |
| 127 |
|
| 128 |
if ( str_starts_with( $this->uri, 'udp:' ) ) { |
| 129 |
$parsed = parse_url( $this->uri ); |
| 130 |
if ( !isset( $parsed['host'] ) ) { |
| 131 |
throw new UnexpectedValueException( sprintf( |
| 132 |
'Udp transport "%s" must specify a host', $this->uri |
| 133 |
) ); |
| 134 |
} |
| 135 |
if ( !isset( $parsed['port'] ) ) { |
| 136 |
throw new UnexpectedValueException( sprintf( |
| 137 |
'Udp transport "%s" must specify a port', $this->uri |
| 138 |
) ); |
| 139 |
} |
| 140 |
|
| 141 |
$this->host = $parsed['host']; |
| 142 |
$this->port = $parsed['port']; |
| 143 |
$this->prefix = ''; |
| 144 |
|
| 145 |
if ( isset( $parsed['path'] ) ) { |
| 146 |
$this->prefix = ltrim( $parsed['path'], '/' ); |
| 147 |
} |
| 148 |
|
| 149 |
if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) { |
| 150 |
$domain = AF_INET6; |
| 151 |
|
| 152 |
} else { |
| 153 |
$domain = AF_INET; |
| 154 |
} |
| 155 |
|
| 156 |
$this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP ); |
| 157 |
|
| 158 |
} else { |
| 159 |
$this->sink = fopen( $this->uri, 'a' ); |
| 160 |
} |
| 161 |
restore_error_handler(); |
| 162 |
|
| 163 |
if ( !$this->sink ) { |
| 164 |
$this->sink = null; |
| 165 |
throw new UnexpectedValueException( sprintf( |
| 166 |
'The stream or file "%s" could not be opened: %s', |
| 167 |
|
| 168 |
$this->uri, $this->error |
| 169 |
) ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
protected function errorTrap( $code, $msg ) { |
| 179 |
$this->error = $msg; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function useUdp() { |
| 187 |
return $this->host !== null; |
| 188 |
} |
| 189 |
|
| 190 |
protected function write( array $record ): void { |
| 191 |
if ( $this->useLegacyFilter && |
| 192 |
!LegacyLogger::shouldEmit( |
| 193 |
$record['channel'], $record['message'], |
| 194 |
$record['level'], $record |
| 195 |
) ) { |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if ( $this->sink === null ) { |
| 204 |
$this->openSink(); |
| 205 |
} |
| 206 |
|
| 207 |
$text = (string)$record['formatted']; |
| 208 |
if ( $this->useUdp() ) { |
| 209 |
|
| 210 |
if ( $this->prefix !== '' ) { |
| 211 |
$leader = ( $this->prefix === '{channel}' ) ? |
| 212 |
$record['channel'] : $this->prefix; |
| 213 |
$text = preg_replace( '/^/m', "{$leader} ", $text ); |
| 214 |
|
| 215 |
|
| 216 |
if ( strlen( $text ) > 65506 ) { |
| 217 |
$text = substr( $text, 0, 65506 ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( !str_ends_with( $text, "\n" ) ) { |
| 221 |
$text .= "\n"; |
| 222 |
} |
| 223 |
|
| 224 |
} elseif ( strlen( $text ) > 65507 ) { |
| 225 |
$text = substr( $text, 0, 65507 ); |
| 226 |
} |
| 227 |
|
| 228 |
socket_sendto( |
| 229 |
$this->sink, |
| 230 |
$text, |
| 231 |
strlen( $text ), |
| 232 |
0, |
| 233 |
$this->host, |
| 234 |
$this->port |
| 235 |
); |
| 236 |
|
| 237 |
} else { |
| 238 |
fwrite( $this->sink, $text ); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public function close(): void { |
| 243 |
if ( $this->sink ) { |
| 244 |
if ( $this->useUdp() ) { |
| 245 |
socket_close( $this->sink ); |
| 246 |
} else { |
| 247 |
fclose( $this->sink ); |
| 248 |
} |
| 249 |
} |
| 250 |
$this->sink = null; |
| 251 |
} |
| 252 |
} |