Implementation:Openai Openai node Logger
| Knowledge Sources | |
|---|---|
| Domains | SDK, Logging |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Logger module provides the SDK's internal logging system with configurable log levels and request detail formatting.
Description
The logging system defines a Logger interface with four severity methods (error, warn, info, debug) and a LogLevel type that controls which messages are emitted. Log levels are ranked numerically: off (0), error (200), warn (300), info (400), and debug (500). When a log function's level exceeds the configured threshold, it is replaced with a no-op function, ensuring zero overhead for suppressed messages.
The loggerFor function retrieves or constructs a level-filtered logger for a given OpenAI client instance. It uses a WeakMap cache keyed on the underlying logger object to avoid repeatedly constructing filtered loggers. If no logger is configured, a no-op logger is returned.
The module also exports formatRequestDetails, which sanitizes request metadata for logging by redacting sensitive headers (Authorization, Cookie, Set-Cookie) and cleaning up redundant fields. The parseLogLevel function validates string-based log level inputs, warning if an unrecognized value is provided.
Usage
Use the logging utilities when you need to add debug or diagnostic output to SDK operations. Configure a logger on the OpenAI client instance and set the desired logLevel. The formatRequestDetails function should be used when logging HTTP request and response details to ensure sensitive information is not leaked.
Code Reference
Source Location
- Repository: openai-node
- File: src/internal/utils/log.ts
Signature
type LogFn = (message: string, ...rest: unknown[]) => void;
export type Logger = {
error: LogFn;
warn: LogFn;
info: LogFn;
debug: LogFn;
};
export type LogLevel = 'off' | 'error' | 'warn' | 'info' | 'debug';
export const parseLogLevel: (
maybeLevel: string | undefined,
sourceName: string,
client: OpenAI,
) => LogLevel | undefined;
export function loggerFor(client: OpenAI): Logger;
export const formatRequestDetails: (details: {
options?: RequestOptions | undefined;
headers?: Headers | Record<string, string> | undefined;
retryOfRequestLogID?: string | undefined;
retryOf?: string | undefined;
url?: string | undefined;
status?: number | undefined;
method?: string | undefined;
durationMs?: number | undefined;
message?: unknown;
body?: unknown;
}) => object;
Import
import { loggerFor, formatRequestDetails, parseLogLevel } from 'openai/internal/utils/log';
import type { Logger, LogLevel } from 'openai/internal/utils/log';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | OpenAI |
Yes | The OpenAI client instance whose logger and logLevel properties are read to construct a filtered logger.
|
| details | object |
Yes (for formatRequestDetails) | Request metadata object containing headers, URL, status, method, duration, and other fields to be sanitized for logging. |
| maybeLevel | undefined | Yes (for parseLogLevel) | A candidate log level string to validate against known levels. |
Outputs
| Name | Type | Description |
|---|---|---|
| Logger | Logger |
A logger object with methods filtered by the configured log level. Methods below the threshold are no-ops. |
| sanitized details | object |
Request details with sensitive headers redacted and redundant fields cleaned up. |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI({
logger: console,
logLevel: 'debug',
});
// The SDK internally uses loggerFor to get a filtered logger:
// import { loggerFor } from 'openai/internal/utils/log';
// const log = loggerFor(client);
// log.debug('Request started', formatRequestDetails({ url, method, headers }));
import { formatRequestDetails } from 'openai/internal/utils/log';
const sanitized = formatRequestDetails({
url: 'https://api.openai.com/v1/chat/completions',
method: 'POST',
status: 200,
durationMs: 342,
headers: {
'Authorization': 'Bearer sk-...',
'Content-Type': 'application/json',
},
});
// sanitized.headers['Authorization'] === '***'
// sanitized.headers['Content-Type'] === 'application/json'