Skip to content

Commit

Permalink
[SD-215] Add x-section-io-id request header to log output. (#3)
Browse files Browse the repository at this point in the history
* [SD-215] Add section-io-id request header to log output.
  • Loading branch information
MdNadimHossain authored Oct 8, 2024
1 parent 858af56 commit bc2ea5a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Logger/TideLogsLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function log($level, $message, array $context = []): void {
return;
}

// Retrieve all headers using the getAllHeaders() function.
$headers = $this->getAllHeaders();

// Check if the x-request-id exists.
if (isset($headers['x-request-id'])) {
$requestId = $headers['x-request-id'];
// Include the request ID in the log context.
$context['request_id'] = $requestId;
}

global $base_url;

$logger = new Logger(
Expand Down Expand Up @@ -186,4 +196,29 @@ public function getSumoLogicCategory() {
return $category ?: static::DEFAULT_CATEGORY;
}

/**
* Retrieves all HTTP headers from the current request.
*
* This function manually extracts HTTP headers from the $_SERVER superglobal.
* It processes server variables that begin with 'HTTP_' and converts them
* into a format similar to what would be returned by getallheaders(), making it
* compatible with environments.
*
* Header names are standardized to all lowercase for consistency.
*
* @return array
* An associative array of HTTP headers where the key is the header name
* (e.g., 'user-agent') in all lowercase and the value is the header's content.
*/
protected function getAllHeaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
// Convert the server variable name into a standard header format (all lowercase).
$headers[strtolower(str_replace('_', '-', substr($name, 5)))] = $value;
}
}
return $headers;
}

}

0 comments on commit bc2ea5a

Please sign in to comment.