moved debug fn out to own module, added tests

This commit is contained in:
Gareth Jones 2013-06-24 08:51:10 +10:00
parent fc7f686f65
commit 0e5da1d361
6 changed files with 91 additions and 28 deletions

View File

@ -4,6 +4,7 @@ var layouts = require('../layouts');
var levels = require('../levels'); var levels = require('../levels');
var dgram = require('dgram'); var dgram = require('dgram');
var util = require('util'); var util = require('util');
var debug = require('../debug')('GELF Appender');
var LOG_EMERG=0; // system is unusable var LOG_EMERG=0; // system is unusable
var LOG_ALERT=1; // action must be taken immediately var LOG_ALERT=1; // action must be taken immediately
@ -24,13 +25,6 @@ levelMapping[levels.WARN] = LOG_WARNING;
levelMapping[levels.ERROR] = LOG_ERR; levelMapping[levels.ERROR] = LOG_ERR;
levelMapping[levels.FATAL] = LOG_CRIT; levelMapping[levels.FATAL] = LOG_CRIT;
var debug;
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
debug = function(message) { console.error('LOG4JS: (GELF Appender) %s', message); };
} else {
debug = function() { };
}
/** /**
* GELF appender that supports sending UDP packets to a GELF compatible server such as Graylog * GELF appender that supports sending UDP packets to a GELF compatible server such as Graylog
* *

15
lib/debug.js Normal file
View File

@ -0,0 +1,15 @@
"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

@ -1,6 +1,7 @@
"use strict"; "use strict";
var fs = require('fs') var fs = require('fs')
, stream , stream
, debug = require('../debug')('BaseRollingFileStream')
, util = require('util') , util = require('util')
, semver = require('semver'); , semver = require('semver');
@ -10,13 +11,6 @@ if (semver.satisfies(process.version, '>=0.10.0')) {
stream = require('readable-stream'); stream = require('readable-stream');
} }
var debug;
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
debug = function(message) { console.error('LOG4JS: (BaseRollingFileStream) %s', message); };
} else {
debug = function() { };
}
module.exports = BaseRollingFileStream; module.exports = BaseRollingFileStream;
function BaseRollingFileStream(filename, options) { function BaseRollingFileStream(filename, options) {

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
var BaseRollingFileStream = require('./BaseRollingFileStream') var BaseRollingFileStream = require('./BaseRollingFileStream')
, debug = require('../debug')('DateRollingFileStream')
, format = require('../date_format') , format = require('../date_format')
, async = require('async') , async = require('async')
, fs = require('fs') , fs = require('fs')
@ -7,13 +8,6 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
module.exports = DateRollingFileStream; module.exports = DateRollingFileStream;
var debug;
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
debug = function(message) { console.error('LOG4JS: (DateRollingFileStream) %s', message); };
} else {
debug = function() { };
}
function DateRollingFileStream(filename, pattern, options, now) { function DateRollingFileStream(filename, pattern, options, now) {
debug("Now is " + now); debug("Now is " + now);
if (pattern && typeof(pattern) === 'object') { if (pattern && typeof(pattern) === 'object') {

View File

@ -1,17 +1,11 @@
"use strict"; "use strict";
var BaseRollingFileStream = require('./BaseRollingFileStream') var BaseRollingFileStream = require('./BaseRollingFileStream')
, debug = require('../debug')('RollingFileStream')
, util = require('util') , util = require('util')
, path = require('path') , path = require('path')
, fs = require('fs') , fs = require('fs')
, async = require('async'); , async = require('async');
var debug;
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
debug = function(message) { console.error('LOG4JS: (RollingFileStream) %s', message); };
} else {
debug = function() { };
}
module.exports = RollingFileStream; module.exports = RollingFileStream;
function RollingFileStream (filename, size, backups, options) { function RollingFileStream (filename, size, backups, options) {

72
test/debug-test.js Normal file
View File

@ -0,0 +1,72 @@
"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);