Request ID
Generate or honor unique request identifiers across requests and responses
Logixlysia can automatically generate or retrieve unique request identifiers (Request IDs) and attach them to the request context, response headers, and log outputs.
Basic Usage
You can enable the request ID feature using the requestId config option. When set to true, it uses the default configuration (X-Request-Id header and UUID v4 generator).
import { Elysia } from 'elysia'
import logixlysia from 'logixlysia'
const app = new Elysia().use(
logixlysia({
config: {
requestId: true
}
})
)
Production Preset
The prod preset enables requestId automatically by default:
const app = new Elysia().use(
logixlysia({
preset: 'prod' // requestId is enabled automatically
})
)
Custom Configuration
For advanced use cases, you can pass a configuration object instead of a boolean:
const app = new Elysia().use(
logixlysia({
config: {
requestId: {
// Custom header name to read from / write to
header: 'X-Correlation-Id',
// Custom request ID generator function
generator: () => `req-${crypto.randomUUID()}`
}
}
})
)
Options
| Option | Type | Default | Description |
|---|---|---|---|
enabled |
boolean |
true |
Turn request ID generation on or off when using object configuration. |
header |
string |
'X-Request-Id' |
The header key to check on incoming requests (honoring upstream proxies) and to set on responses. It must be a valid HTTP header name with no spaces; a value such as 'X Request Id' throws logixlysia: invalid requestId config when the plugin is constructed. An empty or blank value falls back to 'X-Request-Id'. An inbound value is used only when it matches ^[A-Za-z0-9._-]{1,128}$; otherwise a new id is generated. |
generator |
() => string |
crypto.randomUUID |
A function that returns a unique string to use as the request ID. |
The id belongs to the plugin
While requestId is on, the requestId field of a request’s context is set by Logixlysia and stays as it was set. log.mergeContext({ requestId }) and an enricher field of that name are ignored, so the id on a request’s lines is the id in its response header. A requestId passed to a single call, as in log.info('msg', { requestId }), still labels that one line. The same holds inside runJob. To record another service’s id, use a different key:
log.mergeContext({ upstreamRequestId: response.headers.get('x-request-id') })
Generator failures
If generator throws or returns a value that does not match ^[A-Za-z0-9._-]{1,128}$, Logixlysia replaces it with a random UUID for that request. The request continues normally and no error line is logged for it.
Logixlysia reports the failure through onError with sink: 'enricher'. Without onError, it writes the failure to stderr at most once every 5 seconds.
logixlysia({
config: {
requestId: {
generator: () => getTraceId()
},
onError: ({ sink, error }) => {
if (sink === 'enricher')
console.warn('request id generator failed', error)
}
}
})
Log Formatting
When Request ID is enabled, you can output it in your custom log format using the {requestId} token:
const app = new Elysia().use(
logixlysia({
config: {
requestId: true,
customLogFormat: '{method} {pathname} - Request ID: {requestId}'
}
})
)
If Request ID is disabled or not present, the token will render as an empty string.
Error Responses
If a request fails (e.g. throws an exception), the request ID is still propagated in the response headers (like X-Request-Id) and is correctly logged in the HTTP error telemetry context, allowing trace correlation for failed requests.
Logixlysia sets the request ID header as soon as the request starts. Short-circuited responses carry it too, such as an auth resolve that returns status(401), a response returned from an onRequest hook registered after Logixlysia, or an error answered by an app-wide onError registered before Logixlysia. Their log line carries the same ID.
Several Plugin Instances
An app can mount Logixlysia in more than one module, each with its own options. Instances in one request share the id Logixlysia chose for a header name, and names that differ only in case, such as X-Request-Id and x-request-id, count as one header. The first instance to see a request chooses the id and the header key it goes out under, spelled as that instance’s header option spells it. A later instance reuses both, so the response carries one value and the instances log it, with two exceptions. A handler that returns its own Response carrying that header sends its own value, while the instances log the plugin’s id. A request that ends before a later instance’s derive runs, for example a body that fails to parse, is logged by that instance without the request id, even when the response carries the one the first instance chose.
Logixlysia does not adopt a value the app itself put on the response under that header. It uses a valid inbound header or calls the generator. Its id replaces the app’s value when the app wrote the same key. When the app spelled the key in another case, such as x-request-id beside the default X-Request-Id, the response carries both values.