replaced my debug lib with standard one

This commit is contained in:
Gareth Jones 2013-08-25 11:55:26 +10:00
parent 5a2771cfed
commit 50074842ad
9 changed files with 31 additions and 112 deletions

View File

@ -1,15 +0,0 @@
"use strict";
module.exports = function(label) {
var debug;
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
debug = function(message) {
console.error('LOG4JS: (%s) %s', label, message);
};
} else {
debug = function() { };
}
return debug;
};

View File

@ -46,7 +46,7 @@
* @static
* Website: http://log4js.berlios.de
*/
var debug = require('./debug')('core')
var debug = require('debug')('log4js:core')
, fs = require('fs')
, cluster = require('cluster')
, util = require('util')
@ -90,10 +90,10 @@ function deserialise(serialised) {
//in a multi-process node environment, worker loggers will use
//process.send
cluster.on('fork', function(worker) {
debug('listening to worker: ' + worker);
debug('listening to worker: ', worker);
worker.on('message', function(message) {
if (message.type && message.type === '::log4js-message') {
debug("received message: " + message.event);
debug("received message: ", message.event);
dispatch(deserialise(message.event));
}
});
@ -106,7 +106,7 @@ cluster.on('fork', function(worker) {
* @static
*/
function getLogger (category) {
debug("getLogger(" + category + ")");
debug("getLogger(", category, ")");
return new Logger(
cluster.isMaster ? dispatch : workerDispatch,
@ -123,9 +123,9 @@ function workerDispatch(event) {
* This would be a good place to implement category hierarchies/wildcards, etc
*/
function dispatch(event) {
debug("event is " + util.inspect(event));
debug("event is ", event);
var category = categories[event.category] || categories.default;
debug("category.level[" + category.level + "] <= " + event.level + " ? " + category.level.isLessThanOrEqualTo(event.level));
debug("category.level[", category.level, "] <= ", event.level, " ? ", category.level.isLessThanOrEqualTo(event.level));
if (category.level.isLessThanOrEqualTo(event.level)) {
category.appenders.forEach(function(appender) {
@ -135,17 +135,24 @@ function dispatch(event) {
}
function load(file) {
debug("loading ", file);
return JSON.parse(fs.readFileSync(file, "utf-8"));
}
function configure(configurationFileOrObject) {
var filename, config = configurationFileOrObject || process.env.LOG4JS_CONFIG;
debug("configure(", configurationFileOrObject, ")");
debug("process.env.LOG4JS_CONFIG = ", process.env.LOG4JS_CONFIG);
var filename, config = process.env.LOG4JS_CONFIG || configurationFileOrObject;
debug("config ", config);
if (!config || !(typeof config === 'string' || typeof config === 'object')) {
throw new Error("You must specify configuration as an object or a filename.");
}
if (typeof config === 'string') {
debug("config is string");
filename = config;
config = load(filename);
}

View File

@ -1,7 +1,6 @@
"use strict";
var debug = require('./debug')('logger')
, levels = require('./levels')
, util = require('util');
var debug = require('debug')('log4js:logger')
, levels = require('./levels');
module.exports = function Logger(dispatch, category) {
if (typeof dispatch !== 'function') {
@ -16,7 +15,7 @@ module.exports = function Logger(dispatch, category) {
var args = Array.prototype.slice.call(arguments)
, logLevel = args.shift()
, loggingEvent = new LoggingEvent(category, logLevel, args);
debug("Logging event " + loggingEvent + " to dispatch = " + util.inspect(dispatch));
debug("Logging event ", loggingEvent, " to dispatch = ", dispatch);
dispatch(loggingEvent);
}

View File

@ -1,7 +1,7 @@
"use strict";
var fs = require('fs')
, stream
, debug = require('../debug')('BaseRollingFileStream')
, debug = require('debug')('log4js:BaseRollingFileStream')
, util = require('util')
, semver = require('semver');

View File

@ -1,6 +1,6 @@
"use strict";
var BaseRollingFileStream = require('./BaseRollingFileStream')
, debug = require('../debug')('DateRollingFileStream')
, debug = require('debug')('log4js:DateRollingFileStream')
, format = require('../date_format')
, async = require('async')
, fs = require('fs')
@ -9,7 +9,7 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
module.exports = DateRollingFileStream;
function DateRollingFileStream(filename, pattern, options, now) {
debug("Now is " + now);
debug("Now is ", now);
if (pattern && typeof(pattern) === 'object') {
now = options;
options = pattern;
@ -31,7 +31,7 @@ function DateRollingFileStream(filename, pattern, options, now) {
options = null;
}
}
debug("this.now is " + this.now + ", now is " + now);
debug("this.now is ", this.now, ", now is ", now);
DateRollingFileStream.super_.call(this, filename, options);
}
@ -41,8 +41,8 @@ DateRollingFileStream.prototype.shouldRoll = function() {
var lastTime = this.lastTimeWeWroteSomething,
thisTime = format.asString(this.pattern, new Date(this.now()));
debug("DateRollingFileStream.shouldRoll with now = " +
this.now() + ", thisTime = " + thisTime + ", lastTime = " + lastTime);
debug("DateRollingFileStream.shouldRoll with now = ",
this.now(), ", thisTime = ", thisTime, ", lastTime = ", lastTime);
this.lastTimeWeWroteSomething = thisTime;
this.previousTime = lastTime;
@ -81,7 +81,7 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
}
function renameTheCurrentFile(cb) {
debug("Renaming the " + filename + " -> " + newFilename);
debug("Renaming the ", filename, " -> ", newFilename);
fs.rename(filename, newFilename, cb);
}

View File

@ -1,6 +1,6 @@
"use strict";
var BaseRollingFileStream = require('./BaseRollingFileStream')
, debug = require('../debug')('RollingFileStream')
, debug = require('debug')('log4js:RollingFileStream')
, util = require('util')
, path = require('path')
, fs = require('fs')
@ -53,13 +53,13 @@ RollingFileStream.prototype.roll = function(filename, callback) {
function increaseFileIndex (fileToRename, cb) {
var idx = index(fileToRename);
debug('Index of ' + fileToRename + ' is ' + idx);
debug('Index of ', fileToRename, ' is ', idx);
if (idx < that.backups) {
//on windows, you can get a EEXIST error if you rename a file to an existing file
//so, we'll try to delete the file we're renaming to first
fs.unlink(filename + '.' + (idx+1), function (err) {
//ignore err: if we could not delete, it's most likely that it doesn't exist
debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1));
debug('Renaming ', fileToRename, ' -> ', filename, '.', (idx+1));
fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb);
});
} else {

View File

@ -30,7 +30,8 @@
"dependencies": {
"async": "0.1.15",
"semver": "~1.1.4",
"readable-stream": "~1.0.2"
"readable-stream": "~1.0.2",
"debug": "~0.7.2"
},
"devDependencies": {
"sandboxed-module": "0.1.3",

View File

@ -1,72 +0,0 @@
"use strict";
var vows = require('vows')
, assert = require('assert')
, sandbox = require('sandboxed-module')
, fakeConsole = {
error: function(format, label, message) {
this.logged = [ format, label, message ];
}
}
, globals = function(debugValue) {
return {
process: {
env: {
'NODE_DEBUG': debugValue
}
},
console: fakeConsole
};
};
vows.describe('../lib/debug').addBatch({
'when NODE_DEBUG is set to log4js': {
topic: function() {
var debug = sandbox.require(
'../lib/debug',
{ 'globals': globals('log4js') }
);
fakeConsole.logged = [];
debug('cheese')('biscuits');
return fakeConsole.logged;
},
'it should log to console.error': function(logged) {
assert.equal(logged[0], 'LOG4JS: (%s) %s');
assert.equal(logged[1], 'cheese');
assert.equal(logged[2], 'biscuits');
}
},
'when NODE_DEBUG is set to not log4js': {
topic: function() {
var debug = sandbox.require(
'../lib/debug',
{ globals: globals('other_module') }
);
fakeConsole.logged = [];
debug('cheese')('biscuits');
return fakeConsole.logged;
},
'it should not log to console.error': function(logged) {
assert.equal(logged.length, 0);
}
},
'when NODE_DEBUG is not set': {
topic: function() {
var debug = sandbox.require(
'../lib/debug',
{ globals: globals(null) }
);
fakeConsole.logged = [];
debug('cheese')('biscuits');
return fakeConsole.logged;
},
'it should not log to console.error': function(logged) {
assert.equal(logged.length, 0);
}
}
}).exportTo(module);

View File

@ -39,7 +39,7 @@ describe('../lib/log4js', function() {
(function() { log4js.configure(); }).should.throw(
"ENOENT, no such file or directory 'made-up-file'"
);
process.env.LOG4JS_CONFIG = null;
delete process.env.LOG4JS_CONFIG;
});
it('should complain if the config does not specify any appenders', function() {
@ -286,7 +286,6 @@ describe('../lib/log4js', function() {
});
it('should reload configuration if specified');
});
describe('with no configuration', function() {