1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Use util.debuglog instead of using different indidivudal styles for debugging

This commit is contained in:
Ruben Bridgewater
2015-09-02 18:11:19 +02:00
parent 954f1f30df
commit 1eb30add66
10 changed files with 52 additions and 161 deletions

View File

@@ -539,36 +539,6 @@ This will print:
Note that this program will not exit cleanly because the client is still connected. Note that this program will not exit cleanly because the client is still connected.
## redis.debug_mode
Boolean to enable debug mode and protocol tracing.
```js
var redis = require("redis"),
client = redis.createClient();
redis.debug_mode = true;
client.on("connect", function () {
client.set("foo_rand000000000000", "some fantastic value");
});
```
This will display:
mjr:~/work/node_redis (master)$ node ~/example.js
send command: *3
$3
SET
$20
foo_rand000000000000
$20
some fantastic value
on_data: +OK
`send command` is data sent into Redis and `on_data` is data received from Redis.
## Multi-word commands ## Multi-word commands
To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass

View File

@@ -3,8 +3,6 @@
var redis = require("../index"), var redis = require("../index"),
client = redis.createClient(); client = redis.createClient();
redis.debug_mode = true;
client.eval("return 100.5", 0, function (err, res) { client.eval("return 100.5", 0, function (err, res) {
console.dir(err); console.dir(err);
console.dir(res); console.dir(res);

View File

@@ -7,8 +7,6 @@ var redis = require("redis"),
client4 = redis.createClient(), client4 = redis.createClient(),
msg_count = 0; msg_count = 0;
redis.debug_mode = false;
client1.on("psubscribe", function (pattern, count) { client1.on("psubscribe", function (pattern, count) {
console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions"); console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions");
client2.publish("channeltwo", "Me!"); client2.publish("channeltwo", "Me!");

View File

@@ -4,8 +4,6 @@ var redis = require("redis"),
client1 = redis.createClient(), msg_count = 0, client1 = redis.createClient(), msg_count = 0,
client2 = redis.createClient(); client2 = redis.createClient();
redis.debug_mode = false;
// Most clients probably don't do much on "subscribe". This example uses it to coordinate things within one program. // Most clients probably don't do much on "subscribe". This example uses it to coordinate things within one program.
client1.on("subscribe", function (channel, count) { client1.on("subscribe", function (channel, count) {
console.log("client1 subscribed to " + channel + ", " + count + " total subscriptions"); console.log("client1 subscribed to " + channel + ", " + count + " total subscriptions");

143
index.js
View File

@@ -12,25 +12,15 @@ var net = require("net"),
parsers = [], commands, parsers = [], commands,
connection_id = 0, connection_id = 0,
default_port = 6379, default_port = 6379,
default_host = "127.0.0.1"; default_host = "127.0.0.1",
debug = util.debuglog('redis');
// can set this to true to enable for all connections
exports.debug_mode = false;
var arraySlice = Array.prototype.slice;
function trace() {
if (!exports.debug_mode) return;
console.log.apply(null, arraySlice.call(arguments));
}
// hiredis might not be installed // hiredis might not be installed
try { try {
require("./lib/parser/hiredis"); require("./lib/parser/hiredis");
parsers.push(require("./lib/parser/hiredis")); parsers.push(require("./lib/parser/hiredis"));
} catch (err) { } catch (err) {
if (exports.debug_mode) { debug("hiredis parser not installed.");
console.warn("hiredis parser not installed.");
}
} }
parsers.push(require("./lib/parser/javascript")); parsers.push(require("./lib/parser/javascript"));
@@ -132,13 +122,13 @@ RedisClient.prototype.initialize_retry_vars = function () {
}; };
RedisClient.prototype.unref = function () { RedisClient.prototype.unref = function () {
trace("User requesting to unref the connection"); debug("User requesting to unref the connection");
if (this.connected) { if (this.connected) {
trace("unref'ing the socket connection"); debug("unref'ing the socket connection");
this.stream.unref(); this.stream.unref();
} }
else { else {
trace("Not connected yet, will unref later"); debug("Not connected yet, will unref later");
this.once("connect", function () { this.once("connect", function () {
this.unref(); this.unref();
}); });
@@ -187,9 +177,7 @@ RedisClient.prototype.on_error = function (msg) {
var message = "Redis connection to " + this.address + " failed - " + msg; var message = "Redis connection to " + this.address + " failed - " + msg;
if (exports.debug_mode) { debug(message);
console.warn(message);
}
this.flush_and_error(message); this.flush_and_error(message);
@@ -205,9 +193,8 @@ RedisClient.prototype.on_error = function (msg) {
RedisClient.prototype.do_auth = function () { RedisClient.prototype.do_auth = function () {
var self = this; var self = this;
if (exports.debug_mode) { debug("Sending auth to " + self.address + " id " + self.connection_id);
console.log("Sending auth to " + self.address + " id " + self.connection_id);
}
self.send_anyway = true; self.send_anyway = true;
self.send_command("auth", [this.auth_pass], function (err, res) { self.send_command("auth", [this.auth_pass], function (err, res) {
if (err) { if (err) {
@@ -229,9 +216,9 @@ RedisClient.prototype.do_auth = function () {
if (res.toString() !== "OK") { if (res.toString() !== "OK") {
return self.emit("error", new Error("Auth failed: " + res.toString())); return self.emit("error", new Error("Auth failed: " + res.toString()));
} }
if (exports.debug_mode) {
console.log("Auth succeeded " + self.address + " id " + self.connection_id); debug("Auth succeeded " + self.address + " id " + self.connection_id);
}
if (self.auth_callback) { if (self.auth_callback) {
self.auth_callback(err, res); self.auth_callback(err, res);
self.auth_callback = null; self.auth_callback = null;
@@ -251,9 +238,7 @@ RedisClient.prototype.do_auth = function () {
}; };
RedisClient.prototype.on_connect = function () { RedisClient.prototype.on_connect = function () {
if (exports.debug_mode) { debug("Stream connected " + this.address + " id " + this.connection_id);
console.log("Stream connected " + this.address + " id " + this.connection_id);
}
this.connected = true; this.connected = true;
this.ready = false; this.ready = false;
@@ -289,23 +274,17 @@ RedisClient.prototype.init_parser = function () {
if (! parsers.some(function (parser) { if (! parsers.some(function (parser) {
if (parser.name === self.options.parser) { if (parser.name === self.options.parser) {
self.parser_module = parser; self.parser_module = parser;
if (exports.debug_mode) { debug("Using parser module: " + self.parser_module.name);
console.log("Using parser module: " + self.parser_module.name);
}
return true; return true;
} }
})) { })) {
throw new Error("Couldn't find named parser " + self.options.parser + " on this system"); throw new Error("Couldn't find named parser " + self.options.parser + " on this system");
} }
} else { } else {
if (exports.debug_mode) { debug("Using default parser module: " + parsers[0].name);
console.log("Using default parser module: " + parsers[0].name);
}
this.parser_module = parsers[0]; this.parser_module = parsers[0];
} }
this.parser_module.debug_mode = exports.debug_mode;
// return_buffers sends back Buffers from parser to callback. detect_buffers sends back Buffers from parser, but // return_buffers sends back Buffers from parser to callback. detect_buffers sends back Buffers from parser, but
// converts to Strings if the input arguments are not Buffers. // converts to Strings if the input arguments are not Buffers.
this.reply_parser = new this.parser_module.Parser({ this.reply_parser = new this.parser_module.Parser({
@@ -361,9 +340,7 @@ RedisClient.prototype.on_ready = function () {
}; };
Object.keys(this.subscription_set).forEach(function (key) { Object.keys(this.subscription_set).forEach(function (key) {
var parts = key.split(" "); var parts = key.split(" ");
if (exports.debug_mode) { debug("sending pub/sub on_ready " + parts[0] + ", " + parts[1]);
console.warn("sending pub/sub on_ready " + parts[0] + ", " + parts[1]);
}
callback_count++; callback_count++;
self.send_command(parts[0] + "scribe", [parts[1]], callback); self.send_command(parts[0] + "scribe", [parts[1]], callback);
}); });
@@ -403,18 +380,14 @@ RedisClient.prototype.on_info_cmd = function (err, res) {
this.server_info = obj; this.server_info = obj;
if (!obj.loading || obj.loading === "0") { if (!obj.loading || obj.loading === "0") {
if (exports.debug_mode) { debug("Redis server ready.");
console.log("Redis server ready.");
}
this.on_ready(); this.on_ready();
} else { } else {
retry_time = obj.loading_eta_seconds * 1000; retry_time = obj.loading_eta_seconds * 1000;
if (retry_time > 1000) { if (retry_time > 1000) {
retry_time = 1000; retry_time = 1000;
} }
if (exports.debug_mode) { debug("Redis server still loading, trying again in " + retry_time);
console.log("Redis server still loading, trying again in " + retry_time);
}
setTimeout(function () { setTimeout(function () {
self.ready_check(); self.ready_check();
}, retry_time); }, retry_time);
@@ -424,9 +397,7 @@ RedisClient.prototype.on_info_cmd = function (err, res) {
RedisClient.prototype.ready_check = function () { RedisClient.prototype.ready_check = function () {
var self = this; var self = this;
if (exports.debug_mode) { debug("checking server ready state...");
console.log("checking server ready state...");
}
this.send_anyway = true; // secret flag to send_command to send something even if not "ready" this.send_anyway = true; // secret flag to send_command to send something even if not "ready"
this.info(function (err, res) { this.info(function (err, res) {
@@ -440,9 +411,7 @@ RedisClient.prototype.send_offline_queue = function () {
while (this.offline_queue.length > 0) { while (this.offline_queue.length > 0) {
command_obj = this.offline_queue.shift(); command_obj = this.offline_queue.shift();
if (exports.debug_mode) { debug("Sending offline command: " + command_obj.command);
console.log("Sending offline command: " + command_obj.command);
}
buffered_writes += !this.send_command(command_obj.command, command_obj.args, command_obj.callback); buffered_writes += !this.send_command(command_obj.command, command_obj.args, command_obj.callback);
} }
this.offline_queue = new Queue(); this.offline_queue = new Queue();
@@ -462,9 +431,7 @@ RedisClient.prototype.connection_gone = function (why) {
return; return;
} }
if (exports.debug_mode) { debug("Redis connection is gone from " + why + " event.");
console.warn("Redis connection is gone from " + why + " event.");
}
this.connected = false; this.connected = false;
this.ready = false; this.ready = false;
@@ -491,9 +458,7 @@ RedisClient.prototype.connection_gone = function (why) {
// If this is a requested shutdown, then don't retry // If this is a requested shutdown, then don't retry
if (this.closing) { if (this.closing) {
this.retry_timer = null; this.retry_timer = null;
if (exports.debug_mode) { debug("connection ended from quit command, not retrying.");
console.warn("connection ended from quit command, not retrying.");
}
return; return;
} }
@@ -504,9 +469,7 @@ RedisClient.prototype.connection_gone = function (why) {
this.retry_delay = nextDelay; this.retry_delay = nextDelay;
} }
if (exports.debug_mode) { debug("Retry connection in " + this.retry_delay + " ms");
console.log("Retry connection in " + this.retry_delay + " ms");
}
if (this.max_attempts && this.attempts >= this.max_attempts) { if (this.max_attempts && this.attempts >= this.max_attempts) {
this.retry_timer = null; this.retry_timer = null;
@@ -522,9 +485,7 @@ RedisClient.prototype.connection_gone = function (why) {
attempt: self.attempts attempt: self.attempts
}); });
this.retry_timer = setTimeout(function () { this.retry_timer = setTimeout(function () {
if (exports.debug_mode) { debug("Retrying connection...");
console.log("Retrying connection...");
}
self.retry_totaltime += self.retry_delay; self.retry_totaltime += self.retry_delay;
@@ -542,9 +503,7 @@ RedisClient.prototype.connection_gone = function (why) {
}; };
RedisClient.prototype.on_data = function (data) { RedisClient.prototype.on_data = function (data) {
if (exports.debug_mode) { debug("net read " + this.address + " id " + this.connection_id + ": " + data.toString());
console.log("net read " + this.address + " id " + this.connection_id + ": " + data.toString());
}
try { try {
this.reply_parser.execute(data); this.reply_parser.execute(data);
@@ -655,7 +614,7 @@ RedisClient.prototype.return_reply = function (reply) {
} }
if (this.pub_sub_mode && (type == 'message' || type == 'pmessage')) { if (this.pub_sub_mode && (type == 'message' || type == 'pmessage')) {
trace("received pubsub message"); debug("received pubsub message");
} }
else { else {
command_obj = this.command_queue.shift(); command_obj = this.command_queue.shift();
@@ -686,9 +645,7 @@ RedisClient.prototype.return_reply = function (reply) {
} }
try_callback(command_obj.callback, reply); try_callback(command_obj.callback, reply);
} else if (exports.debug_mode) { } else debug("no callback for reply: " + (reply && reply.toString && reply.toString()));
console.log("no callback for reply: " + (reply && reply.toString && reply.toString()));
}
} else if (this.pub_sub_mode || (command_obj && command_obj.sub_command)) { } else if (this.pub_sub_mode || (command_obj && command_obj.sub_command)) {
if (Array.isArray(reply)) { if (Array.isArray(reply)) {
type = reply[0].toString(); type = reply[0].toString();
@@ -700,9 +657,7 @@ RedisClient.prototype.return_reply = function (reply) {
} else if (type === "subscribe" || type === "unsubscribe" || type === "psubscribe" || type === "punsubscribe") { } else if (type === "subscribe" || type === "unsubscribe" || type === "psubscribe" || type === "punsubscribe") {
if (reply[2] === 0) { if (reply[2] === 0) {
this.pub_sub_mode = false; this.pub_sub_mode = false;
if (this.debug_mode) { debug("All subscriptions removed, exiting pub/sub mode");
console.log("All subscriptions removed, exiting pub/sub mode");
}
} else { } else {
this.pub_sub_mode = true; this.pub_sub_mode = true;
} }
@@ -803,16 +758,12 @@ RedisClient.prototype.send_command = function (command, args, callback) {
command_obj = new Command(command, args, false, buffer_args, callback); command_obj = new Command(command, args, false, buffer_args, callback);
if ((!this.ready && !this.send_anyway) || !stream.writable) { if ((!this.ready && !this.send_anyway) || !stream.writable) {
if (exports.debug_mode) { if (!stream.writable) {
if (!stream.writable) { debug("send command: stream is not writeable.");
console.log("send command: stream is not writeable.");
}
} }
if (this.enable_offline_queue) { if (this.enable_offline_queue) {
if (exports.debug_mode) { debug("Queueing " + command + " for next server connection.");
console.log("Queueing " + command + " for next server connection.");
}
this.offline_queue.push(command_obj); this.offline_queue.push(command_obj);
this.should_buffer = true; this.should_buffer = true;
} else { } else {
@@ -854,14 +805,10 @@ RedisClient.prototype.send_command = function (command, args, callback) {
} }
command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n"; command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n";
} }
if (exports.debug_mode) { debug("send " + this.address + " id " + this.connection_id + ": " + command_str);
console.log("send " + this.address + " id " + this.connection_id + ": " + command_str);
}
buffered_writes += !stream.write(command_str); buffered_writes += !stream.write(command_str);
} else { } else {
if (exports.debug_mode) { debug("send command (" + command_str + ") has Buffer arguments");
console.log("send command (" + command_str + ") has Buffer arguments");
}
buffered_writes += !stream.write(command_str); buffered_writes += !stream.write(command_str);
for (i = 0, il = args.length, arg; i < il; i += 1) { for (i = 0, il = args.length, arg; i < il; i += 1) {
@@ -872,29 +819,21 @@ RedisClient.prototype.send_command = function (command, args, callback) {
if (Buffer.isBuffer(arg)) { if (Buffer.isBuffer(arg)) {
if (arg.length === 0) { if (arg.length === 0) {
if (exports.debug_mode) { debug("send_command: using empty string for 0 length buffer");
console.log("send_command: using empty string for 0 length buffer");
}
buffered_writes += !stream.write("$0\r\n\r\n"); buffered_writes += !stream.write("$0\r\n\r\n");
} else { } else {
buffered_writes += !stream.write("$" + arg.length + "\r\n"); buffered_writes += !stream.write("$" + arg.length + "\r\n");
buffered_writes += !stream.write(arg); buffered_writes += !stream.write(arg);
buffered_writes += !stream.write("\r\n"); buffered_writes += !stream.write("\r\n");
if (exports.debug_mode) { debug("send_command: buffer send " + arg.length + " bytes");
console.log("send_command: buffer send " + arg.length + " bytes");
}
} }
} else { } else {
if (exports.debug_mode) { debug("send_command: string send " + Buffer.byteLength(arg) + " bytes: " + arg);
console.log("send_command: string send " + Buffer.byteLength(arg) + " bytes: " + arg);
}
buffered_writes += !stream.write("$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n"); buffered_writes += !stream.write("$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n");
} }
} }
} }
if (exports.debug_mode) { debug("send_command buffered_writes: " + buffered_writes, " should_buffer: " + this.should_buffer);
console.log("send_command buffered_writes: " + buffered_writes, " should_buffer: " + this.should_buffer);
}
if (buffered_writes || this.command_queue.getLength() >= this.command_queue_high_water) { if (buffered_writes || this.command_queue.getLength() >= this.command_queue_high_water) {
this.should_buffer = true; this.should_buffer = true;
} }
@@ -904,8 +843,8 @@ RedisClient.prototype.send_command = function (command, args, callback) {
RedisClient.prototype.pub_sub_command = function (command_obj) { RedisClient.prototype.pub_sub_command = function (command_obj) {
var i, key, command, args; var i, key, command, args;
if (this.pub_sub_mode === false && exports.debug_mode) { if (this.pub_sub_mode === false) {
console.log("Entering pub/sub mode from " + command_obj.command); debug("Entering pub/sub mode from " + command_obj.command);
} }
this.pub_sub_mode = true; this.pub_sub_mode = true;
command_obj.sub_command = true; command_obj.sub_command = true;
@@ -1025,9 +964,7 @@ RedisClient.prototype.auth = function () {
var args = to_array(arguments); var args = to_array(arguments);
this.auth_pass = args[0]; this.auth_pass = args[0];
this.auth_callback = args[1]; this.auth_callback = args[1];
if (exports.debug_mode) { debug("Saving auth as " + this.auth_pass);
console.log("Saving auth as " + this.auth_pass);
}
if (this.connected) { if (this.connected) {
this.send_command("auth", args); this.send_command("auth", args);

View File

@@ -4,7 +4,6 @@ var events = require("events"),
util = require("../util"), util = require("../util"),
hiredis = require("hiredis"); hiredis = require("hiredis");
exports.debug_mode = false;
exports.name = "hiredis"; exports.name = "hiredis";
function HiredisReplyParser(options) { function HiredisReplyParser(options) {

View File

@@ -9,7 +9,6 @@ function Packet(type, size) {
} }
exports.name = "javascript"; exports.name = "javascript";
exports.debug_mode = false;
function ReplyParser(options) { function ReplyParser(options) {
this.name = exports.name; this.name = exports.name;
@@ -18,7 +17,6 @@ function ReplyParser(options) {
this._buffer = null; this._buffer = null;
this._offset = 0; this._offset = 0;
this._encoding = "utf-8"; this._encoding = "utf-8";
this._debug_mode = options.debug_mode;
this._reply_type = null; this._reply_type = null;
} }

View File

@@ -11,8 +11,6 @@ var redis = require("./index"),
}, },
small_str, large_str, small_buf, large_buf, very_large_str, very_large_buf; small_str, large_str, small_buf, large_buf, very_large_str, very_large_buf;
redis.debug_mode = false;
function lpad(input, len, chr) { function lpad(input, len, chr) {
var str = input.toString(); var str = input.toString();
chr = chr || " "; chr = chr || " ";

View File

@@ -1,19 +1,15 @@
// helpers for configuring a redis client in // helpers for configuring a redis client in
// its various modes, ipV6, ipV4, socket. // its various modes, ipV6, ipV4, socket.
module.exports = (function () { var redis = require('../../index');
var redis = require('../../index');
redis.debug_mode = process.env.DEBUG ? JSON.parse(process.env.DEBUG) : false;
var config = { var config = {
redis: redis, redis: redis,
PORT: 6378, PORT: 6378,
HOST: { HOST: {
IPv4: "127.0.0.1", IPv4: "127.0.0.1",
IPv6: "::1" IPv6: "::1"
} },
}; configureClient: function (parser, ip, opts) {
config.configureClient = function (parser, ip, opts) {
var args = []; var args = [];
opts = opts || {}; opts = opts || {};
@@ -29,7 +25,7 @@ module.exports = (function () {
args.push(opts); args.push(opts);
return args; return args;
}; }
};
return config; module.exports = config;
})();

View File

@@ -4,7 +4,6 @@
'use strict'; 'use strict';
var redis = require("../../"); var redis = require("../../");
//redis.debug_mode = true
var HOST = process.argv[2] || '127.0.0.1'; var HOST = process.argv[2] || '127.0.0.1';
var PORT = process.argv[3] var PORT = process.argv[3]
var args = PORT ? [PORT, HOST] : [HOST] var args = PORT ? [PORT, HOST] : [HOST]