Function: logWithTrace()
@arolariu/website / instrumentation.server / logWithTrace
Function: logWithTrace()
logWithTrace(
level,message,attributes?,renderContext?):void
Defined in: instrumentation.server.ts:1321
Emit structured logs correlated with distributed traces.
Provides JSON-formatted logging with automatic trace context injection. This enables correlation between logs and traces in observability platforms.
Parameters
level
Log level (debug, info, warn, error)
message
string
Human-readable log message
attributes?
Record<string, unknown>
Additional structured data to include in the log entry
renderContext?
Returns
void
Remarks
- Automatically includes trace ID and span ID when within an active span
- Outputs JSON to stdout/stderr for structured log ingestion
- Timestamps are ISO 8601 formatted
- Use attributes for structured data instead of string interpolation
- Logs are written to appropriate console stream based on level
- Debug logs are only emitted in development mode
Examples
// Basic logging with context
logWithTrace('info', 'User logged in', {
'user.authenticated': true,
'auth.method': 'clerk',
}, 'api');
// API route logging
await withSpan('api.user.create', async (span) => {
logWithTrace('info', 'Creating user', {
'http.method': 'POST',
'http.route': '/api/users',
}, 'api');
try {
const user = await createUser(data);
logWithTrace('info', 'User created successfully', {
userId: user.id,
}, 'api');
} catch (error) {
logWithTrace('error', 'Failed to create user', {
'error.type': error.constructor.name,
'error.message': error.message,
'error.handled': true,
}, 'api');
}
});
// Server component logging
logWithTrace('debug', 'Rendering user profile', {
'next.render_context': 'server',
'next.server_components': true,
}, 'server');
// Output format
{
"timestamp": "2025-10-11T14:30:00.123Z",
"level": "info",
"message": "User logged in",
"traceId": "1234567890abcdef",
"spanId": "fedcba0987654321",
"context": "api",
"user.authenticated": true,
"auth.method": "clerk"
}
See
// was this page useful?