From 2c1520ae5b6f6d465af641b4cdd171a065da0736 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 5 May 2019 21:17:52 +0200 Subject: [PATCH] Support adding a request ID to logged / printed exceptions This can be used to correlate requests in logfiles across the whole stack from reverse proxy / CDN through web server to PHP and WoltLab Suite. --- wcfsetup/install/files/lib/core.functions.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/core.functions.php b/wcfsetup/install/files/lib/core.functions.php index a287a9b374..8a888d0a9e 100644 --- a/wcfsetup/install/files/lib/core.functions.php +++ b/wcfsetup/install/files/lib/core.functions.php @@ -97,6 +97,14 @@ namespace { } // @codingStandardsIgnoreStart +namespace wcf { + function getRequestId() { + if (!defined('WCF_REQUEST_ID_HEADER') || !WCF_REQUEST_ID_HEADER) return ''; + + return $_SERVER[WCF_REQUEST_ID_HEADER] ?? ''; + } +} + namespace wcf\functions\exception { use wcf\system\WCF; use wcf\system\exception\IExtraInformationException; @@ -124,7 +132,7 @@ namespace wcf\functions\exception { 'Message: '.$stripNewlines($e->getMessage())."\n". 'PHP version: '.phpversion()."\n". 'WoltLab Suite version: '.WCF_VERSION."\n". - 'Request URI: '.$stripNewlines($_SERVER['REQUEST_URI'] ?? '')."\n". + 'Request URI: '.$stripNewlines($_SERVER['REQUEST_URI'] ?? '').(\wcf\getRequestId() ? ' ('.\wcf\getRequestId().')' : '')."\n". 'Referrer: '.$stripNewlines($_SERVER['HTTP_REFERER'] ?? '')."\n". 'User Agent: '.$stripNewlines($_SERVER['HTTP_USER_AGENT'] ?? '')."\n". 'Peak Memory Usage: '.memory_get_peak_usage().'/'.FileUtil::getMemoryLimit()."\n"; @@ -178,6 +186,7 @@ namespace wcf\functions\exception { */ function printThrowable($e) { $exceptionID = logThrowable($e, $logFile); + if (\wcf\getRequestId()) $exceptionID .= '/'.\wcf\getRequestId(); $exceptionTitle = $exceptionSubtitle = $exceptionExplanation = ''; $logFile = sanitizePath($logFile); -- 2.20.1