Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(options): optionally allow custom logger to be provided #1851

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ class Redis extends Commander implements DataHandledable {
return this.emit.apply(this, arguments);
}
if (error && error instanceof Error) {
console.error("[ioredis] Unhandled error event:", error.stack);
this.options.logger.error("[ioredis] Unhandled error event:", error.stack);
}
return false;
}
Expand Down Expand Up @@ -721,6 +721,10 @@ class Redis extends Commander implements DataHandledable {
options.db = parseInt(options.db, 10);
}

if (!options.logger) {
options.logger = console;
}

// @ts-expect-error
this.options = resolveTLSProfile(options);
}
Expand Down Expand Up @@ -796,7 +800,7 @@ class Redis extends Commander implements DataHandledable {
this.info(function (err, res) {
if (err) {
if (err.message && err.message.includes("NOPERM")) {
console.warn(
_this.options.logger.warn(
`Skipping the ready check because INFO command fails: "${err.message}". You can disable ready check with "enableReadyCheck". More: https://github.com/luin/ioredis/wiki/Disable-ready-check.`
);
return callback(null, {});
Expand Down
13 changes: 13 additions & 0 deletions lib/cluster/ClusterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface NatMap {
[key: string]: { host: string; port: number };
}

type Logger = {
log(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
};

/**
* Options for Cluster constructor
*/
Expand Down Expand Up @@ -196,6 +202,12 @@ export interface ClusterOptions extends CommanderOptions {
string,
{ lua: string; numberOfKeys?: number; readOnly?: boolean }
>;

/**
* The logging mechanism to use. If you want to use your own logger, pass an object implementing the `Logger` interface.
* @default console
*/
logger?: Logger;
}

export const DEFAULT_CLUSTER_OPTIONS: ClusterOptions = {
Expand All @@ -214,4 +226,5 @@ export const DEFAULT_CLUSTER_OPTIONS: ClusterOptions = {
dnsLookup: lookup,
enableAutoPipelining: false,
autoPipeliningIgnoredCommands: [],
logger: console,
};
13 changes: 13 additions & 0 deletions lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { StandaloneConnectionOptions } from "../connectors/StandaloneConnector";

export type ReconnectOnError = (err: Error) => boolean | 1 | 2;

type Logger = {
log(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
};

export interface CommonRedisOptions extends CommanderOptions {
Connector?: ConnectorConstructor;
retryStrategy?: (times: number) => number | void | null;
Expand Down Expand Up @@ -179,6 +185,12 @@ export interface CommonRedisOptions extends CommanderOptions {
string,
{ lua: string; numberOfKeys?: number; readOnly?: boolean }
>;

/**
* The logging mechanism to use. If you want to use your own logger, pass an object implementing the `Logger` interface.
* @default console
*/
logger: Logger;
}

export type RedisOptions = CommonRedisOptions &
Expand Down Expand Up @@ -236,4 +248,5 @@ export const DEFAULT_REDIS_OPTIONS: RedisOptions = {
enableAutoPipelining: false,
autoPipeliningIgnoredCommands: [],
sentinelMaxConnections: 10,
logger: console,
};
6 changes: 3 additions & 3 deletions lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ export function connectHandler(self) {
}
if (err) {
if (err.message.indexOf("no password is set") !== -1) {
console.warn(
self.options.logger.warn(
"[WARN] Redis server does not require a password, but a password was supplied."
);
} else if (
err.message.indexOf(
"without any password configured for the default user"
) !== -1
) {
console.warn(
self.options.logger.warn(
"[WARN] This Redis server's `default` user does not require a password, but a password was supplied"
);
} else if (
err.message.indexOf(
"wrong number of arguments for 'auth' command"
) !== -1
) {
console.warn(
self.options.logger.warn(
`[ERROR] The server returned "wrong number of arguments for 'auth' command". You are probably passing both username and password to Redis version 5 or below. You should only pass the 'password' option for Redis version 5 and under.`
);
} else {
Expand Down