🧑‍💻 Log the request path to give more context

This commit is contained in:
Philipp Stracker 2025-04-03 13:15:34 +02:00
parent e9450f0100
commit 6ed6b8c3ba
No known key found for this signature in database

View file

@ -36,6 +36,14 @@ class WooCommerceLogger implements LoggerInterface {
*/
private string $source;
/**
* Details that are output before the first real log message, to help
* identify the request.
*
* @var string
*/
private string $request_info;
/**
* A random prefix which is visible in every log message, to better
* understand which messages belong to the same request.
@ -54,6 +62,14 @@ class WooCommerceLogger implements LoggerInterface {
$this->wc_logger = $wc_logger;
$this->source = $source;
$this->prefix = sprintf( '#%s - ', wp_rand( 1000, 9999 ) );
// phpcs:disable -- Intentionally not sanitized, for logging purposes.
$method = wp_unslash( $_SERVER['REQUEST_METHOD'] ?? 'CLI' );
$request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ?? '-' );
// phpcs:enable
$request_path = wp_parse_url( $request_uri, PHP_URL_PATH );
$this->request_info = "$method $request_path";
}
/**
@ -68,6 +84,15 @@ class WooCommerceLogger implements LoggerInterface {
$context['source'] = $this->source;
}
if ( $this->request_info ) {
$this->wc_logger->log(
'debug',
"{$this->prefix}[New Request] $this->request_info",
array( 'source' => $context['source'] )
);
$this->request_info = '';
}
$this->wc_logger->log( $level, "{$this->prefix}$message", $context );
}
}