| 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; |
| 22 |
|
| 23 |
use DateTimeZone; |
| 24 |
use Error; |
| 25 |
use LogicException; |
| 26 |
use MediaWiki\Debug\MWDebug; |
| 27 |
use MediaWiki\WikiMap\WikiMap; |
| 28 |
use MWExceptionHandler; |
| 29 |
use Psr\Log\AbstractLogger; |
| 30 |
use Psr\Log\LogLevel; |
| 31 |
use Throwable; |
| 32 |
use UDPTransport; |
| 33 |
use Wikimedia\AtEase\AtEase; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
s |
| 51 |
|
| 52 |
class LegacyLogger extends AbstractLogger { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected $channel; |
| 58 |
|
| 59 |
private const LEVEL_DEBUG = 100; |
| 60 |
private const LEVEL_INFO = 200; |
| 61 |
private const LEVEL_NOTICE = 250; |
| 62 |
private const LEVEL_WARNING = 300; |
| 63 |
private const LEVEL_ERROR = 400; |
| 64 |
private const LEVEL_CRITICAL = 500; |
| 65 |
private const LEVEL_ALERT = 550; |
| 66 |
private const LEVEL_EMERGENCY = 600; |
| 67 |
private const LEVEL_INFINITY = 999; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected static $levelMapping = [ |
| 76 |
LogLevel::DEBUG => self::LEVEL_DEBUG, |
| 77 |
LogLevel::INFO => self::LEVEL_INFO, |
| 78 |
LogLevel::NOTICE => self::LEVEL_NOTICE, |
| 79 |
LogLevel::WARNING => self::LEVEL_WARNING, |
| 80 |
LogLevel::ERROR => self::LEVEL_ERROR, |
| 81 |
LogLevel::CRITICAL => self::LEVEL_CRITICAL, |
| 82 |
LogLevel::ALERT => self::LEVEL_ALERT, |
| 83 |
LogLevel::EMERGENCY => self::LEVEL_EMERGENCY, |
| 84 |
]; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private $minimumLevel; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private $isDB; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function __construct( $channel ) { |
| 105 |
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups, $wgDebugToolbar, $wgDebugRawPage, $wgShowDebug; |
| 106 |
|
| 107 |
$this->channel = $channel; |
| 108 |
$this->isDB = ( $channel === 'rdbms' ); |
| 109 |
|
| 110 |
|
| 111 |
if ( !$wgDebugRawPage && wfIsDebugRawPage() ) { |
| 112 |
$this->minimumLevel = self::LEVEL_WARNING; |
| 113 |
} elseif ( $wgDebugLogFile != '' || $wgShowDebug || $wgDebugToolbar ) { |
| 114 |
|
| 115 |
$this->minimumLevel = self::LEVEL_DEBUG; |
| 116 |
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) { |
| 117 |
$logConfig = $wgDebugLogGroups[$channel]; |
| 118 |
|
| 119 |
if ( is_array( $logConfig ) && isset( $logConfig['level'] ) ) { |
| 120 |
$this->minimumLevel = self::$levelMapping[$logConfig['level']]; |
| 121 |
} else { |
| 122 |
$this->minimumLevel = self::LEVEL_DEBUG; |
| 123 |
} |
| 124 |
} else { |
| 125 |
|
| 126 |
$this->minimumLevel = self::LEVEL_INFINITY; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $this->isDB && $wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) { |
| 130 |
|
| 131 |
$this->minimumLevel = self::LEVEL_ERROR; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function setMinimumForTest( ?int $level ) { |
| 143 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 144 |
throw new LogicException( 'Not allowed outside tests' ); |
| 145 |
} |
| 146 |
|
| 147 |
$original = $this->minimumLevel; |
| 148 |
$this->minimumLevel = $level ?? self::LEVEL_INFINITY; |
| 149 |
return $original; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function log( $level, $message, array $context = [] ): void { |
| 160 |
if ( is_string( $level ) ) { |
| 161 |
$level = self::$levelMapping[$level]; |
| 162 |
} |
| 163 |
if ( $level < $this->minimumLevel ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if ( $this->isDB |
| 168 |
&& $level === self::LEVEL_DEBUG |
| 169 |
&& isset( $context['sql'] ) |
| 170 |
) { |
| 171 |
|
| 172 |
MWDebug::query( |
| 173 |
$context['sql'], |
| 174 |
$context['method'], |
| 175 |
$context['runtime_ms'] / 1000, |
| 176 |
$context['db_server'] |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
global $wgDBerrorLog; |
| 186 |
if ( $this->isDB && $level >= self::LEVEL_ERROR && $wgDBerrorLog ) { |
| 187 |
|
| 188 |
$effectiveChannel = 'wfLogDBError'; |
| 189 |
} else { |
| 190 |
$effectiveChannel = $this->channel; |
| 191 |
} |
| 192 |
|
| 193 |
if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) { |
| 194 |
$text = self::format( $effectiveChannel, $message, $context ); |
| 195 |
$destination = self::destination( $effectiveChannel, $message, $context ); |
| 196 |
$this->maybeLogToStderr( $text ); |
| 197 |
self::emit( $text, $destination ); |
| 198 |
} |
| 199 |
if ( !isset( $context['private'] ) || !$context['private'] ) { |
| 200 |
|
| 201 |
MWDebug::debugMsg( $message, [ 'channel' => $this->channel ] + $context ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static function shouldEmit( $channel, $message, $level, $context ) { |
| 216 |
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups; |
| 217 |
|
| 218 |
if ( is_string( $level ) ) { |
| 219 |
$level = self::$levelMapping[$level]; |
| 220 |
} |
| 221 |
|
| 222 |
if ( $channel === 'wfLogDBError' ) { |
| 223 |
|
| 224 |
|
| 225 |
$shouldEmit = (bool)$wgDBerrorLog; |
| 226 |
|
| 227 |
} elseif ( $channel === 'wfDebug' ) { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
$shouldEmit = $wgDebugLogFile != ''; |
| 233 |
|
| 234 |
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) { |
| 235 |
$logConfig = $wgDebugLogGroups[$channel]; |
| 236 |
|
| 237 |
if ( is_array( $logConfig ) ) { |
| 238 |
$shouldEmit = true; |
| 239 |
if ( isset( $logConfig['sample'] ) ) { |
| 240 |
|
| 241 |
$shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1; |
| 242 |
} |
| 243 |
|
| 244 |
if ( isset( $logConfig['level'] ) ) { |
| 245 |
$shouldEmit = $level >= self::$levelMapping[$logConfig['level']]; |
| 246 |
} |
| 247 |
} else { |
| 248 |
|
| 249 |
$shouldEmit = $logConfig !== false; |
| 250 |
} |
| 251 |
|
| 252 |
} elseif ( isset( $context['private'] ) && $context['private'] ) { |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
$shouldEmit = false; |
| 258 |
} else { |
| 259 |
|
| 260 |
|
| 261 |
$shouldEmit = $wgDebugLogFile != ''; |
| 262 |
} |
| 263 |
|
| 264 |
return $shouldEmit; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static function format( $channel, $message, $context ) { |
| 280 |
global $wgDebugLogGroups, $wgLogExceptionBacktrace; |
| 281 |
|
| 282 |
if ( $channel === 'wfDebug' ) { |
| 283 |
$text = self::formatAsWfDebug( $channel, $message, $context ); |
| 284 |
|
| 285 |
} elseif ( $channel === 'wfLogDBError' ) { |
| 286 |
$text = self::formatAsWfLogDBError( $channel, $message, $context ); |
| 287 |
|
| 288 |
} elseif ( !isset( $wgDebugLogGroups[$channel] ) ) { |
| 289 |
$text = self::formatAsWfDebug( |
| 290 |
$channel, "[{$channel}] {$message}", $context ); |
| 291 |
|
| 292 |
} else { |
| 293 |
|
| 294 |
$text = self::formatAsWfDebugLog( $channel, $message, $context ); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
if ( $wgLogExceptionBacktrace && isset( $context['exception'] ) ) { |
| 299 |
$e = $context['exception']; |
| 300 |
$backtrace = false; |
| 301 |
|
| 302 |
if ( $e instanceof Throwable ) { |
| 303 |
$backtrace = MWExceptionHandler::getRedactedTrace( $e ); |
| 304 |
|
| 305 |
} elseif ( is_array( $e ) && isset( $e['trace'] ) ) { |
| 306 |
|
| 307 |
$backtrace = $e['trace']; |
| 308 |
} |
| 309 |
|
| 310 |
if ( $backtrace ) { |
| 311 |
$text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) . |
| 312 |
"\n"; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return self::interpolate( $text, $context ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
protected static function formatAsWfDebug( $channel, $message, $context ) { |
| 328 |
$text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $message ); |
| 329 |
if ( isset( $context['seconds_elapsed'] ) ) { |
| 330 |
|
| 331 |
|
| 332 |
$text = "{$context['seconds_elapsed']} {$context['memory_used']} {$text}"; |
| 333 |
} |
| 334 |
if ( isset( $context['prefix'] ) ) { |
| 335 |
$text = "{$context['prefix']}{$text}"; |
| 336 |
} |
| 337 |
return "{$text}\n"; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
protected static function formatAsWfLogDBError( $channel, $message, $context ) { |
| 349 |
global $wgDBerrorLogTZ; |
| 350 |
static $cachedTimezone = null; |
| 351 |
|
| 352 |
if ( !$cachedTimezone ) { |
| 353 |
$cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ ); |
| 354 |
} |
| 355 |
|
| 356 |
$d = date_create( 'now', $cachedTimezone ); |
| 357 |
$date = $d->format( 'D M j G:i:s T Y' ); |
| 358 |
|
| 359 |
$host = wfHostname(); |
| 360 |
$wiki = WikiMap::getCurrentWikiId(); |
| 361 |
|
| 362 |
$text = "{$date}\t{$host}\t{$wiki}\t{$message}\n"; |
| 363 |
return $text; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
protected static function formatAsWfDebugLog( $channel, $message, $context ) { |
| 375 |
$time = wfTimestamp( TS_DB ); |
| 376 |
$wiki = WikiMap::getCurrentWikiId(); |
| 377 |
$host = wfHostname(); |
| 378 |
$text = "{$time} {$host} {$wiki}: {$message}\n"; |
| 379 |
return $text; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public static function interpolate( $message, array $context ) { |
| 390 |
if ( str_contains( $message, '{' ) ) { |
| 391 |
$replace = []; |
| 392 |
foreach ( $context as $key => $val ) { |
| 393 |
$replace['{' . $key . '}'] = self::flatten( $val ); |
| 394 |
} |
| 395 |
$message = strtr( $message, $replace ); |
| 396 |
} |
| 397 |
return $message; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
protected static function flatten( $item ) { |
| 408 |
if ( $item === null ) { |
| 409 |
return '[Null]'; |
| 410 |
} |
| 411 |
|
| 412 |
if ( is_bool( $item ) ) { |
| 413 |
return $item ? 'true' : 'false'; |
| 414 |
} |
| 415 |
|
| 416 |
if ( is_float( $item ) ) { |
| 417 |
if ( is_infinite( $item ) ) { |
| 418 |
return ( $item > 0 ? '' : '-' ) . 'INF'; |
| 419 |
} |
| 420 |
if ( is_nan( $item ) ) { |
| 421 |
return 'NaN'; |
| 422 |
} |
| 423 |
return (string)$item; |
| 424 |
} |
| 425 |
|
| 426 |
if ( is_scalar( $item ) ) { |
| 427 |
return (string)$item; |
| 428 |
} |
| 429 |
|
| 430 |
if ( is_array( $item ) ) { |
| 431 |
return '[Array(' . count( $item ) . ')]'; |
| 432 |
} |
| 433 |
|
| 434 |
if ( $item instanceof \DateTime ) { |
| 435 |
return $item->format( 'c' ); |
| 436 |
} |
| 437 |
|
| 438 |
if ( $item instanceof Throwable ) { |
| 439 |
$which = $item instanceof Error ? 'Error' : 'Exception'; |
| 440 |
return '[' . $which . ' ' . get_class( $item ) . '( ' . |
| 441 |
$item->getFile() . ':' . $item->getLine() . ') ' . |
| 442 |
$item->getMessage() . ']'; |
| 443 |
} |
| 444 |
|
| 445 |
if ( is_object( $item ) ) { |
| 446 |
if ( method_exists( $item, '__toString' ) ) { |
| 447 |
return (string)$item; |
| 448 |
} |
| 449 |
|
| 450 |
return '[Object ' . get_class( $item ) . ']'; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
if ( is_resource( $item ) ) { |
| 455 |
return '[Resource ' . get_resource_type( $item ) . ']'; |
| 456 |
} |
| 457 |
|
| 458 |
return '[Unknown ' . get_debug_type( $item ) . ']'; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
protected static function destination( $channel, $message, $context ) { |
| 472 |
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
$destination = $wgDebugLogFile; |
| 477 |
|
| 478 |
if ( isset( $context['destination'] ) ) { |
| 479 |
|
| 480 |
$destination = $context['destination']; |
| 481 |
|
| 482 |
} elseif ( $channel === 'wfDebug' ) { |
| 483 |
$destination = $wgDebugLogFile; |
| 484 |
|
| 485 |
} elseif ( $channel === 'wfLogDBError' ) { |
| 486 |
$destination = $wgDBerrorLog; |
| 487 |
|
| 488 |
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) { |
| 489 |
$logConfig = $wgDebugLogGroups[$channel]; |
| 490 |
|
| 491 |
if ( is_array( $logConfig ) ) { |
| 492 |
$destination = $logConfig['destination']; |
| 493 |
} else { |
| 494 |
$destination = strval( $logConfig ); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
return $destination; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public static function emit( $text, $file ) { |
| 511 |
if ( str_starts_with( $file, 'udp:' ) ) { |
| 512 |
$transport = UDPTransport::newFromString( $file ); |
| 513 |
$transport->emit( $text ); |
| 514 |
} else { |
| 515 |
AtEase::suppressWarnings(); |
| 516 |
$exists = file_exists( $file ); |
| 517 |
$size = $exists ? filesize( $file ) : false; |
| 518 |
if ( !$exists || |
| 519 |
( $size !== false && $size + strlen( $text ) < 0x7fffffff ) |
| 520 |
) { |
| 521 |
file_put_contents( $file, $text, FILE_APPEND ); |
| 522 |
} |
| 523 |
AtEase::restoreWarnings(); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
private function maybeLogToStderr( string $text ): void { |
| 535 |
if ( getenv( 'MW_LOG_STDERR' ) ) { |
| 536 |
error_log( trim( $text ) ); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
} |