Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae04cc9a4a | ||
|
|
70a9444f4d | ||
|
|
3e78fcb630 | ||
|
|
44687e1bd1 | ||
|
|
8e5754371a | ||
|
|
feef9975c7 | ||
|
|
93695fbfc4 | ||
|
|
0571089a8b | ||
|
|
ab77895555 | ||
|
|
9637be8a41 | ||
|
|
0ecd729f49 | ||
|
|
f9c2e78055 | ||
|
|
e7267ecf46 | ||
|
|
c03185b563 | ||
|
|
c0aa8c5c86 | ||
|
|
59a6703549 | ||
|
|
ceffdf92e4 | ||
|
|
c9e72d0f00 | ||
|
|
a27345461b | ||
|
|
fb9948145c | ||
|
|
0242bae78f | ||
|
|
b71f635267 | ||
|
|
66872d136d | ||
|
|
49849a545a | ||
|
|
3c4c98bb0b | ||
|
|
d65d053bc1 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ node_modules
|
||||
.bob/
|
||||
test/streams/test-rolling-file-stream*
|
||||
test/streams/test-rolling-stream-with-existing-files*
|
||||
.idea
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
"use strict";
|
||||
var log4js = require('../log4js')
|
||||
, layouts = require('../layouts')
|
||||
, Hook = require('hook.io').Hook
|
||||
, util = require('util');
|
||||
|
||||
var Logger = function createLogger(options) {
|
||||
var self = this;
|
||||
var actualAppender = options.actualAppender;
|
||||
Hook.call(self, options);
|
||||
self.on('hook::ready', function hookReady() {
|
||||
self.on('*::' + options.name + '::log', function log(loggingEvent) {
|
||||
deserializeLoggingEvent(loggingEvent);
|
||||
actualAppender(loggingEvent);
|
||||
});
|
||||
});
|
||||
};
|
||||
util.inherits(Logger, Hook);
|
||||
|
||||
function deserializeLoggingEvent(loggingEvent) {
|
||||
loggingEvent.startTime = new Date(loggingEvent.startTime);
|
||||
loggingEvent.level.toString = function levelToString() {
|
||||
return loggingEvent.level.levelStr;
|
||||
};
|
||||
}
|
||||
|
||||
function initHook(hookioOptions) {
|
||||
var loggerHook;
|
||||
if (hookioOptions.mode === 'master') {
|
||||
// Start the master hook, handling the actual logging
|
||||
loggerHook = new Logger(hookioOptions);
|
||||
} else {
|
||||
// Start a worker, just emitting events for a master
|
||||
loggerHook = new Hook(hookioOptions);
|
||||
}
|
||||
loggerHook.start();
|
||||
return loggerHook;
|
||||
}
|
||||
|
||||
function getBufferedHook(hook, eventName) {
|
||||
var hookBuffer = [];
|
||||
var hookReady = false;
|
||||
hook.on('hook::ready', function emptyBuffer() {
|
||||
hookBuffer.forEach(function logBufferItem(loggingEvent) {
|
||||
hook.emit(eventName, loggingEvent);
|
||||
});
|
||||
hookReady = true;
|
||||
});
|
||||
|
||||
return function log(loggingEvent) {
|
||||
if (hookReady) {
|
||||
hook.emit(eventName, loggingEvent);
|
||||
} else {
|
||||
hookBuffer.push(loggingEvent);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function createAppender(hookioOptions) {
|
||||
var loggerHook = initHook(hookioOptions);
|
||||
var loggerEvent = hookioOptions.name + '::log';
|
||||
return getBufferedHook(loggerHook, loggerEvent);
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
var actualAppender;
|
||||
if (config.appender && config.mode === 'master') {
|
||||
log4js.loadAppender(config.appender.type);
|
||||
actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
|
||||
config.actualAppender = actualAppender;
|
||||
}
|
||||
return createAppender(config);
|
||||
}
|
||||
|
||||
exports.appender = createAppender;
|
||||
exports.configure = configure;
|
||||
@@ -1,7 +1,9 @@
|
||||
'use strict';
|
||||
var layouts = require('../layouts')
|
||||
, loggly = require('loggly')
|
||||
, os = require('os');
|
||||
, os = require('os')
|
||||
, passThrough = layouts.messagePassThroughLayout;
|
||||
|
||||
|
||||
/**
|
||||
* Loggly Appender. Sends logging events to Loggly using node-loggly
|
||||
@@ -16,59 +18,17 @@ var layouts = require('../layouts')
|
||||
*/
|
||||
function logglyAppender(config, layout) {
|
||||
var client = loggly.createClient(config);
|
||||
if(!layout) layout = passThrough;
|
||||
|
||||
var packageMessage = function (loggingEvent) {
|
||||
var BaseItem = function(level, msg) {
|
||||
this.level = level || loggingEvent.level.toString();
|
||||
this.category = loggingEvent.categoryName;
|
||||
this.hostname = os.hostname().toString();
|
||||
if (typeof msg !== 'undefined')
|
||||
this.msg = msg;
|
||||
};
|
||||
|
||||
var packageItem = function (item) {
|
||||
if (item instanceof Error)
|
||||
return new BaseItem('ERROR', item.message);
|
||||
|
||||
if (['string', 'number', 'boolean'].indexOf(typeof item) > -1 )
|
||||
return new BaseItem(undefined, item);
|
||||
|
||||
var obj = new BaseItem();
|
||||
if (Array.isArray(item))
|
||||
return item.unshift(obj); //add base object as first item
|
||||
|
||||
if (item && Object.prototype.toString.call(item) === '[object Object]') {
|
||||
for (var key in item) {
|
||||
if (item.hasOwnProperty(key)) {
|
||||
obj[key] = item[key]; //don't do packageItem on nested items, because level, category and hostname are needed on top level items only.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
if (loggingEvent.data.length === 1) {
|
||||
return packageItem(loggingEvent.data[0]);
|
||||
}
|
||||
//length >1
|
||||
var msg = loggingEvent.data;
|
||||
for (var i = 0, l = msg.length; i < l; i++) {
|
||||
msg[i] = packageItem(msg[i]);
|
||||
}
|
||||
|
||||
return msg;
|
||||
};
|
||||
|
||||
return function(loggingEvent) {
|
||||
var a = layout ? layout(loggingEvent) : packageMessage(loggingEvent);
|
||||
//console.log('log now', a);
|
||||
client.log(a, config.tags, function(err, result) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
return function(loggingEvent) {
|
||||
var msg = layout(loggingEvent);
|
||||
client.log({
|
||||
msg: msg,
|
||||
level: loggingEvent.level.levelStr,
|
||||
category: loggingEvent.categoryName,
|
||||
hostname: os.hostname().toString(),
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
@@ -81,4 +41,4 @@ function configure(config) {
|
||||
|
||||
exports.name = 'loggly';
|
||||
exports.appender = logglyAppender;
|
||||
exports.configure = configure;
|
||||
exports.configure = configure;
|
||||
|
||||
@@ -84,6 +84,12 @@ function getLogger(logger4js, options) {
|
||||
res.end = end;
|
||||
res.end(chunk, encoding);
|
||||
res.responseTime = new Date() - start;
|
||||
//status code response level handling
|
||||
if(res.statusCode && options.level === 'auto'){
|
||||
level = levels.INFO;
|
||||
if(res.statusCode >= 300) level = levels.WARN;
|
||||
if(res.statusCode >= 400) level = levels.ERROR;
|
||||
}
|
||||
if (thislogger.isLevelEnabled(level)) {
|
||||
if (typeof fmt === 'function') {
|
||||
var line = fmt(req, res, function(str){ return format(str, req, res); });
|
||||
|
||||
@@ -125,6 +125,7 @@ function messagePassThroughLayout (loggingEvent) {
|
||||
* - %d date in various formats
|
||||
* - %% %
|
||||
* - %n newline
|
||||
* - %z pid
|
||||
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
|
||||
* You can use %[ and %] to define a colored block.
|
||||
*
|
||||
@@ -144,7 +145,7 @@ function messagePassThroughLayout (loggingEvent) {
|
||||
*/
|
||||
function patternLayout (pattern, tokens) {
|
||||
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdhmnprx%])(\{([^\}]+)\})?|([^%]+)/;
|
||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdhmnprzx%])(\{([^\}]+)\})?|([^%]+)/;
|
||||
|
||||
pattern = pattern || TTCC_CONVERSION_PATTERN;
|
||||
|
||||
@@ -211,6 +212,10 @@ function patternLayout (pattern, tokens) {
|
||||
return '%';
|
||||
}
|
||||
|
||||
function pid() {
|
||||
return process.pid;
|
||||
}
|
||||
|
||||
function userDefined(loggingEvent, specifier) {
|
||||
if (typeof(tokens[specifier]) !== 'undefined') {
|
||||
if (typeof(tokens[specifier]) === 'function') {
|
||||
@@ -232,6 +237,7 @@ function patternLayout (pattern, tokens) {
|
||||
'r': startTime,
|
||||
'[': startColour,
|
||||
']': endColour,
|
||||
'z': pid,
|
||||
'%': percent,
|
||||
'x': userDefined
|
||||
};
|
||||
|
||||
@@ -70,6 +70,29 @@ function hasLogger(logger) {
|
||||
}
|
||||
|
||||
|
||||
function getBufferedLogger(categoryName) {
|
||||
var base_logger = getLogger(categoryName);
|
||||
var logger = {};
|
||||
logger.temp = [];
|
||||
logger.target = base_logger;
|
||||
logger.flush = function () {
|
||||
for (var i = 0; i < logger.temp.length; i++) {
|
||||
var log = logger.temp[i];
|
||||
logger.target[log.level](log.message);
|
||||
delete logger.temp[i];
|
||||
}
|
||||
};
|
||||
logger.trace = function (message) { logger.temp.push({level: 'trace', message: message}); };
|
||||
logger.debug = function (message) { logger.temp.push({level: 'debug', message: message}); };
|
||||
logger.info = function (message) { logger.temp.push({level: 'info', message: message}); };
|
||||
logger.warn = function (message) { logger.temp.push({level: 'warn', message: message}); };
|
||||
logger.error = function (message) { logger.temp.push({level: 'error', message: message}); };
|
||||
logger.fatal = function (message) { logger.temp.push({level: 'fatal', message: message}); };
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a logger instance. Instance is cached on categoryName level.
|
||||
* @param {String} categoryName name of category to log to.
|
||||
@@ -375,6 +398,7 @@ function shutdown(cb) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getBufferedLogger: getBufferedLogger,
|
||||
getLogger: getLogger,
|
||||
getDefaultLogger: getDefaultLogger,
|
||||
hasLogger: hasLogger,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "log4js",
|
||||
"version": "0.6.16",
|
||||
"version": "0.6.19",
|
||||
"description": "Port of Log4js to work with node.",
|
||||
"keywords": [
|
||||
"logging",
|
||||
@@ -35,7 +35,6 @@
|
||||
"devDependencies": {
|
||||
"vows": "0.7.0",
|
||||
"sandboxed-module": "0.1.3",
|
||||
"hook.io": "0.8.10",
|
||||
"underscore": "1.2.1"
|
||||
},
|
||||
"browser": {
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
var vows = require('vows')
|
||||
, fs = require('fs')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, EOL = require('os').EOL || '\n';
|
||||
|
||||
function remove(filename) {
|
||||
try {
|
||||
@@ -15,24 +16,24 @@ function remove(filename) {
|
||||
vows.describe('log4js categoryFilter').addBatch({
|
||||
'appender': {
|
||||
topic: function() {
|
||||
|
||||
|
||||
var log4js = require('../lib/log4js'), logEvents = [], webLogger, appLogger;
|
||||
log4js.clearAppenders();
|
||||
var appender = require('../lib/appenders/categoryFilter')
|
||||
.appender(
|
||||
['app'],
|
||||
['app'],
|
||||
function(evt) { logEvents.push(evt); }
|
||||
);
|
||||
log4js.addAppender(appender, ["app","web"]);
|
||||
|
||||
|
||||
webLogger = log4js.getLogger("web");
|
||||
appLogger = log4js.getLogger("app");
|
||||
|
||||
|
||||
webLogger.debug('This should get logged');
|
||||
appLogger.debug('This should not');
|
||||
webLogger.debug('Hello again');
|
||||
log4js.getLogger('db').debug('This shouldn\'t be included by the appender anyway');
|
||||
|
||||
|
||||
return logEvents;
|
||||
},
|
||||
'should only pass matching category' : function(logEvents) {
|
||||
@@ -41,32 +42,32 @@ vows.describe('log4js categoryFilter').addBatch({
|
||||
assert.equal(logEvents[1].data[0], 'Hello again');
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
'configure': {
|
||||
topic: function() {
|
||||
var log4js = require('../lib/log4js')
|
||||
, logger, weblogger;
|
||||
|
||||
|
||||
remove(__dirname + '/categoryFilter-web.log');
|
||||
remove(__dirname + '/categoryFilter-noweb.log');
|
||||
|
||||
|
||||
log4js.configure('test/with-categoryFilter.json');
|
||||
logger = log4js.getLogger("app");
|
||||
weblogger = log4js.getLogger("web");
|
||||
|
||||
|
||||
logger.info('Loading app');
|
||||
logger.debug('Initialising indexes');
|
||||
weblogger.info('00:00:00 GET / 200');
|
||||
weblogger.warn('00:00:00 GET / 500');
|
||||
//wait for the file system to catch up
|
||||
setTimeout(this.callback, 100);
|
||||
setTimeout(this.callback, 500);
|
||||
},
|
||||
'tmp-tests.log': {
|
||||
topic: function() {
|
||||
fs.readFile(__dirname + '/categoryFilter-noweb.log', 'utf8', this.callback);
|
||||
},
|
||||
'should contain all log messages': function(contents) {
|
||||
var messages = contents.trim().split('\n');
|
||||
var messages = contents.trim().split(EOL);
|
||||
assert.deepEqual(messages, ['Loading app','Initialising indexes']);
|
||||
}
|
||||
},
|
||||
@@ -75,7 +76,7 @@ vows.describe('log4js categoryFilter').addBatch({
|
||||
fs.readFile(__dirname + '/categoryFilter-web.log','utf8',this.callback);
|
||||
},
|
||||
'should contain only error and warning log messages': function(contents) {
|
||||
var messages = contents.trim().split('\n');
|
||||
var messages = contents.trim().split(EOL);
|
||||
assert.deepEqual(messages, ['00:00:00 GET / 200','00:00:00 GET / 500']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ vows.describe('../lib/appenders/dateFile').addBatch({
|
||||
}
|
||||
},
|
||||
'with cwd option': {
|
||||
topic: function() {
|
||||
topic: function () {
|
||||
var fileOpened,
|
||||
appender = sandbox.require(
|
||||
'../lib/appenders/dateFile',
|
||||
@@ -213,8 +213,9 @@ vows.describe('../lib/appenders/dateFile').addBatch({
|
||||
);
|
||||
return fileOpened;
|
||||
},
|
||||
'should prepend options.cwd to config.filename': function(fileOpened) {
|
||||
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
|
||||
'should prepend options.cwd to config.filename': function (fileOpened) {
|
||||
var expected = path.sep + path.join("absolute", "path", "to", "whatever.log");
|
||||
assert.equal(fileOpened, expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ var vows = require('vows')
|
||||
, path = require('path')
|
||||
, sandbox = require('sandboxed-module')
|
||||
, log4js = require('../lib/log4js')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, EOL = require('os').EOL || '\n';
|
||||
|
||||
log4js.clearAppenders();
|
||||
|
||||
@@ -93,8 +94,8 @@ vows.describe('log4js fileAppender').addBatch({
|
||||
fs.readFile(testFile, "utf8", that.callback);
|
||||
}, 100);
|
||||
},
|
||||
'should write log messages to the file': function(err, fileContents) {
|
||||
assert.include(fileContents, "This should be in the file.\n");
|
||||
'should write log messages to the file': function (err, fileContents) {
|
||||
assert.include(fileContents, "This should be in the file." + EOL);
|
||||
},
|
||||
'log messages should be in the basic layout format': function(err, fileContents) {
|
||||
assert.match(
|
||||
@@ -125,7 +126,7 @@ vows.describe('log4js fileAppender').addBatch({
|
||||
}, 100);
|
||||
},
|
||||
'log file should only contain the second message': function(err, fileContents) {
|
||||
assert.include(fileContents, "This is the second log message.\n");
|
||||
assert.include(fileContents, "This is the second log message.");
|
||||
assert.equal(fileContents.indexOf("This is the first log message."), -1);
|
||||
},
|
||||
'the number of files': {
|
||||
@@ -229,8 +230,8 @@ vows.describe('log4js fileAppender').addBatch({
|
||||
|
||||
fs.readFile('tmp-tests.log', 'utf8', this.callback);
|
||||
},
|
||||
'should load appender configuration from a json file': function(err, contents) {
|
||||
assert.include(contents, 'this should be written to the file\n');
|
||||
'should load appender configuration from a json file': function (err, contents) {
|
||||
assert.include(contents, 'this should be written to the file' + EOL);
|
||||
assert.equal(contents.indexOf('this should not be written to the file'), -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ var vows = require('vows')
|
||||
, path = require('path')
|
||||
, sandbox = require('sandboxed-module')
|
||||
, log4js = require('../lib/log4js')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, EOL = require('os').EOL || '\n';
|
||||
|
||||
log4js.clearAppenders();
|
||||
|
||||
@@ -31,8 +32,8 @@ vows.describe('log4js fileSyncAppender').addBatch({
|
||||
|
||||
fs.readFile(testFile, "utf8", that.callback);
|
||||
},
|
||||
'should write log messages to the file': function(err, fileContents) {
|
||||
assert.include(fileContents, "This should be in the file.\n");
|
||||
'should write log messages to the file': function (err, fileContents) {
|
||||
assert.include(fileContents, "This should be in the file." + EOL);
|
||||
},
|
||||
'log messages should be in the basic layout format': function(err, fileContents) {
|
||||
assert.match(
|
||||
@@ -60,8 +61,8 @@ vows.describe('log4js fileSyncAppender').addBatch({
|
||||
|
||||
fs.readFile(testFile, "utf8", that.callback);
|
||||
},
|
||||
'log file should only contain the second message': function(err, fileContents) {
|
||||
assert.include(fileContents, "This is the second log message.\n");
|
||||
'log file should only contain the second message': function (err, fileContents) {
|
||||
assert.include(fileContents, "This is the second log message." + EOL);
|
||||
assert.equal(fileContents.indexOf("This is the first log message."), -1);
|
||||
},
|
||||
'the number of files': {
|
||||
@@ -173,7 +174,7 @@ vows.describe('log4js fileSyncAppender').addBatch({
|
||||
fs.readFile('tmp-sync-tests.log', 'utf8', this.callback);
|
||||
},
|
||||
'should load appender configuration from a json file': function(err, contents) {
|
||||
assert.include(contents, 'this should be written to the file\n');
|
||||
assert.include(contents, 'this should be written to the file' + EOL);
|
||||
assert.equal(contents.indexOf('this should not be written to the file'), -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, assert = require('assert')
|
||||
, sandbox = require('sandboxed-module');
|
||||
|
||||
function fancyResultingHookioAppender(hookNotReady) {
|
||||
var emitHook = !hookNotReady
|
||||
, result = { ons: {}, emissions: {}, logged: [], configs: [] };
|
||||
|
||||
var fakeLog4Js = {
|
||||
appenderMakers: {}
|
||||
};
|
||||
fakeLog4Js.loadAppender = function (appender) {
|
||||
fakeLog4Js.appenderMakers[appender] = function (config) {
|
||||
result.actualLoggerConfig = config;
|
||||
return function log(logEvent) {
|
||||
result.logged.push(logEvent);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
var fakeHookIo = { Hook: function(config) { result.configs.push(config); } };
|
||||
fakeHookIo.Hook.prototype.start = function () {
|
||||
result.startCalled = true;
|
||||
};
|
||||
fakeHookIo.Hook.prototype.on = function (eventName, functionToExec) {
|
||||
result.ons[eventName] = { functionToExec: functionToExec };
|
||||
if (emitHook && eventName === 'hook::ready') {
|
||||
functionToExec();
|
||||
}
|
||||
};
|
||||
fakeHookIo.Hook.prototype.emit = function (eventName, data) {
|
||||
result.emissions[eventName] = result.emissions[eventName] || [];
|
||||
result.emissions[eventName].push({data: data});
|
||||
var on = '*::' + eventName;
|
||||
if (eventName !== 'hook::ready' && result.ons[on]) {
|
||||
result.ons[on].callingCount =
|
||||
result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1;
|
||||
result.ons[on].functionToExec(data);
|
||||
}
|
||||
};
|
||||
|
||||
return { theResult: result,
|
||||
theModule: sandbox.require('../lib/appenders/hookio', {
|
||||
requires: {
|
||||
'../log4js': fakeLog4Js,
|
||||
'hook.io': fakeHookIo
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
vows.describe('log4js hookioAppender').addBatch({
|
||||
'master': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender();
|
||||
var logger = fancy.theModule.configure(
|
||||
{
|
||||
name: 'ohno',
|
||||
mode: 'master',
|
||||
'hook-port': 5001,
|
||||
appender: { type: 'file' }
|
||||
}
|
||||
);
|
||||
logger(
|
||||
{
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "ALRIGHTY THEN",
|
||||
startTime: '2011-10-27T03:53:16.031Z'
|
||||
}
|
||||
);
|
||||
logger(
|
||||
{
|
||||
level: { levelStr: 'DEBUG' },
|
||||
data: "OH WOW",
|
||||
startTime: '2011-10-27T04:53:16.031Z'
|
||||
}
|
||||
);
|
||||
return fancy.theResult;
|
||||
},
|
||||
|
||||
'should write to the actual appender': function (result) {
|
||||
assert.isTrue(result.startCalled);
|
||||
assert.equal(result.configs.length, 1);
|
||||
assert.equal(result.configs[0]['hook-port'], 5001);
|
||||
assert.equal(result.logged.length, 2);
|
||||
assert.equal(result.emissions['ohno::log'].length, 2);
|
||||
assert.equal(result.ons['*::ohno::log'].callingCount, 2);
|
||||
},
|
||||
|
||||
'data written should be formatted correctly': function (result) {
|
||||
assert.equal(result.logged[0].level.toString(), 'INFO');
|
||||
assert.equal(result.logged[0].data, 'ALRIGHTY THEN');
|
||||
assert.isTrue(typeof(result.logged[0].startTime) === 'object');
|
||||
assert.equal(result.logged[1].level.toString(), 'DEBUG');
|
||||
assert.equal(result.logged[1].data, 'OH WOW');
|
||||
assert.isTrue(typeof(result.logged[1].startTime) === 'object');
|
||||
},
|
||||
|
||||
'the actual logger should get the right config': function (result) {
|
||||
assert.equal(result.actualLoggerConfig.type, 'file');
|
||||
}
|
||||
},
|
||||
'worker': {
|
||||
'should emit logging events to the master': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender();
|
||||
var logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker',
|
||||
appender: { type: 'file' }
|
||||
});
|
||||
logger({
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "ALRIGHTY THEN",
|
||||
startTime: '2011-10-27T03:53:16.031Z'
|
||||
});
|
||||
logger({
|
||||
level: { levelStr: 'DEBUG' },
|
||||
data: "OH WOW",
|
||||
startTime: '2011-10-27T04:53:16.031Z'
|
||||
});
|
||||
return fancy.theResult;
|
||||
},
|
||||
|
||||
'should not write to the actual appender': function (result) {
|
||||
assert.isTrue(result.startCalled);
|
||||
assert.equal(result.logged.length, 0);
|
||||
assert.equal(result.emissions['ohno::log'].length, 2);
|
||||
assert.isUndefined(result.ons['*::ohno::log']);
|
||||
}
|
||||
}
|
||||
},
|
||||
'when hook not ready': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender(true)
|
||||
, logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker'
|
||||
});
|
||||
|
||||
logger({
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "something",
|
||||
startTime: '2011-10-27T03:45:12.031Z'
|
||||
});
|
||||
return fancy;
|
||||
},
|
||||
'should buffer the log events': function(fancy) {
|
||||
assert.isUndefined(fancy.theResult.emissions['ohno::log']);
|
||||
},
|
||||
},
|
||||
'when hook ready': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender(true)
|
||||
, logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker'
|
||||
});
|
||||
|
||||
logger({
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "something",
|
||||
startTime: '2011-10-27T03:45:12.031Z'
|
||||
});
|
||||
|
||||
fancy.theResult.ons['hook::ready'].functionToExec();
|
||||
return fancy;
|
||||
},
|
||||
'should emit the buffered events': function(fancy) {
|
||||
assert.equal(fancy.theResult.emissions['ohno::log'].length, 1);
|
||||
}
|
||||
}
|
||||
|
||||
}).exportTo(module);
|
||||
@@ -1,6 +1,8 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, os = require('os')
|
||||
, EOL = os.EOL || '\n';
|
||||
|
||||
//used for patternLayout tests.
|
||||
function test(args, pattern, value) {
|
||||
@@ -195,7 +197,7 @@ vows.describe('log4js layouts').addBatch({
|
||||
},
|
||||
|
||||
'should default to "time logLevel loggerName - message"': function(args) {
|
||||
test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n");
|
||||
test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test" + EOL);
|
||||
},
|
||||
'%r should output time only': function(args) {
|
||||
test(args, '%r', '14:18:30');
|
||||
@@ -210,10 +212,13 @@ vows.describe('log4js layouts').addBatch({
|
||||
test(args, '%m', 'this is a test');
|
||||
},
|
||||
'%n should output a new line': function(args) {
|
||||
test(args, '%n', '\n');
|
||||
test(args, '%n', EOL);
|
||||
},
|
||||
'%h should output hostname' : function(args) {
|
||||
test(args, '%h', require('os').hostname().toString());
|
||||
test(args, '%h', os.hostname().toString());
|
||||
},
|
||||
'%z should output pid' : function(args) {
|
||||
test(args, '%z', process.pid);
|
||||
},
|
||||
'%c should handle category names like java-style package names': function(args) {
|
||||
test(args, '%c{1}', 'tests');
|
||||
@@ -247,7 +252,7 @@ vows.describe('log4js layouts').addBatch({
|
||||
'should handle complicated patterns': function(args) {
|
||||
test(args,
|
||||
'%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
|
||||
'this is a test\n of.tests at 14:18:30.045 cheese DEBUG\n'
|
||||
'this is a test'+ EOL +' of.tests at 14:18:30.045 cheese DEBUG' + EOL
|
||||
);
|
||||
},
|
||||
'should truncate fields if specified': function(args) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, assert = require('assert')
|
||||
, path = require('path')
|
||||
, sandbox = require('sandboxed-module');
|
||||
|
||||
vows.describe('log4js-abspath').addBatch({
|
||||
@@ -69,7 +70,8 @@ vows.describe('log4js-abspath').addBatch({
|
||||
return fileOpened;
|
||||
},
|
||||
'should prepend options.cwd to config.filename': function(fileOpened) {
|
||||
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
|
||||
var expected = path.sep + path.join("absolute", "path", "to", "whatever.log");
|
||||
assert.equal(fileOpened, expected);
|
||||
}
|
||||
},
|
||||
}).export(module);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, fs = require('fs')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, os = require('os')
|
||||
, EOL = require('os').EOL || '\n';
|
||||
|
||||
function remove(filename) {
|
||||
try {
|
||||
@@ -19,12 +21,12 @@ vows.describe('log4js logLevelFilter').addBatch({
|
||||
log4js.addAppender(
|
||||
require('../lib/appenders/logLevelFilter')
|
||||
.appender(
|
||||
'ERROR',
|
||||
'ERROR',
|
||||
function(evt) { logEvents.push(evt); }
|
||||
),
|
||||
),
|
||||
"logLevelTest"
|
||||
);
|
||||
|
||||
|
||||
logger = log4js.getLogger("logLevelTest");
|
||||
logger.debug('this should not trigger an event');
|
||||
logger.warn('neither should this');
|
||||
@@ -43,10 +45,10 @@ vows.describe('log4js logLevelFilter').addBatch({
|
||||
topic: function() {
|
||||
var log4js = require('../lib/log4js')
|
||||
, logger;
|
||||
|
||||
|
||||
remove(__dirname + '/logLevelFilter.log');
|
||||
remove(__dirname + '/logLevelFilter-warnings.log');
|
||||
|
||||
|
||||
log4js.configure('test/with-logLevelFilter.json');
|
||||
logger = log4js.getLogger("tests");
|
||||
logger.info('main');
|
||||
@@ -54,14 +56,14 @@ vows.describe('log4js logLevelFilter').addBatch({
|
||||
logger.warn('both');
|
||||
logger.debug('main');
|
||||
//wait for the file system to catch up
|
||||
setTimeout(this.callback, 100);
|
||||
setTimeout(this.callback, 500);
|
||||
},
|
||||
'tmp-tests.log': {
|
||||
topic: function() {
|
||||
fs.readFile(__dirname + '/logLevelFilter.log', 'utf8', this.callback);
|
||||
},
|
||||
'should contain all log messages': function(contents) {
|
||||
var messages = contents.trim().split('\n');
|
||||
'should contain all log messages': function (contents) {
|
||||
var messages = contents.trim().split(EOL);
|
||||
assert.deepEqual(messages, ['main','both','both','main']);
|
||||
}
|
||||
},
|
||||
@@ -70,7 +72,7 @@ vows.describe('log4js logLevelFilter').addBatch({
|
||||
fs.readFile(__dirname + '/logLevelFilter-warnings.log','utf8',this.callback);
|
||||
},
|
||||
'should contain only error and warning log messages': function(contents) {
|
||||
var messages = contents.trim().split('\n');
|
||||
var messages = contents.trim().split(EOL);
|
||||
assert.deepEqual(messages, ['both','both']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,72 @@ function setupConsoleTest() {
|
||||
}
|
||||
|
||||
vows.describe('log4js').addBatch({
|
||||
|
||||
'getBufferedLogger': {
|
||||
topic: function () {
|
||||
var log4js = require('../lib/log4js');
|
||||
log4js.clearAppenders();
|
||||
var logger = log4js.getBufferedLogger('tests');
|
||||
return logger;
|
||||
},
|
||||
|
||||
'should take a category and return a logger': function (logger) {
|
||||
assert.equal(logger.target.category, 'tests');
|
||||
assert.isFunction(logger.flush);
|
||||
assert.isFunction(logger.trace);
|
||||
assert.isFunction(logger.debug);
|
||||
assert.isFunction(logger.info);
|
||||
assert.isFunction(logger.warn);
|
||||
assert.isFunction(logger.error);
|
||||
assert.isFunction(logger.fatal);
|
||||
},
|
||||
|
||||
'cache events': {
|
||||
topic: function () {
|
||||
var log4js = require('../lib/log4js');
|
||||
log4js.clearAppenders();
|
||||
var logger = log4js.getBufferedLogger('tests1');
|
||||
var events = [];
|
||||
logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
|
||||
logger.debug("Debug event");
|
||||
logger.trace("Trace event 1");
|
||||
logger.trace("Trace event 2");
|
||||
logger.warn("Warning event");
|
||||
logger.error("Aargh!", new Error("Pants are on fire!"));
|
||||
logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
|
||||
return events;
|
||||
},
|
||||
|
||||
'should not emit log events if .flush() is not called.': function (events) {
|
||||
assert.equal(events.length, 0);
|
||||
}
|
||||
},
|
||||
|
||||
'log events after flush() is called': {
|
||||
topic: function () {
|
||||
var log4js = require('../lib/log4js');
|
||||
log4js.clearAppenders();
|
||||
var logger = log4js.getBufferedLogger('tests2');
|
||||
logger.target.setLevel("TRACE");
|
||||
var events = [];
|
||||
logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
|
||||
logger.debug("Debug event");
|
||||
logger.trace("Trace event 1");
|
||||
logger.trace("Trace event 2");
|
||||
logger.warn("Warning event");
|
||||
logger.error("Aargh!", new Error("Pants are on fire!"));
|
||||
logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
|
||||
logger.flush();
|
||||
return events;
|
||||
},
|
||||
|
||||
'should emit log events when .flush() is called.': function (events) {
|
||||
assert.equal(events.length, 6);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
'getLogger': {
|
||||
topic: function() {
|
||||
var log4js = require('../lib/log4js');
|
||||
|
||||
@@ -7,7 +7,7 @@ var vows = require('vows')
|
||||
|
||||
function setupLogging(category, options) {
|
||||
var msgs = [];
|
||||
|
||||
|
||||
var fakeMailer = {
|
||||
createTransport: function (name, options) {
|
||||
return {
|
||||
@@ -49,7 +49,7 @@ function setupLogging(category, options) {
|
||||
});
|
||||
|
||||
log4js.addAppender(smtpModule.configure(options), category);
|
||||
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
mailer: fakeMailer,
|
||||
@@ -150,10 +150,10 @@ vows.describe('log4js smtpAppender').addBatch({
|
||||
}, 500);
|
||||
setTimeout(function () {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1050);
|
||||
}, 1100);
|
||||
setTimeout(function () {
|
||||
self.callback(null, setup);
|
||||
}, 2100);
|
||||
}, 3000);
|
||||
},
|
||||
'there should be three messages': function (result) {
|
||||
assert.equal(result.results.length, 3);
|
||||
@@ -181,13 +181,13 @@ vows.describe('log4js smtpAppender').addBatch({
|
||||
}, 0);
|
||||
setTimeout(function () {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 500);
|
||||
}, 100);
|
||||
setTimeout(function () {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1050);
|
||||
}, 1500);
|
||||
setTimeout(function () {
|
||||
self.callback(null, setup);
|
||||
}, 2100);
|
||||
}, 3000);
|
||||
},
|
||||
'there should be two messages': function (result) {
|
||||
assert.equal(result.results.length, 2);
|
||||
@@ -218,7 +218,7 @@ vows.describe('log4js smtpAppender').addBatch({
|
||||
close: function() { }
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
setup.logger.info("This will break");
|
||||
return setup.console;
|
||||
},
|
||||
@@ -228,6 +228,4 @@ vows.describe('log4js smtpAppender').addBatch({
|
||||
assert.equal(cons.errors[0].value.message, 'oh noes');
|
||||
}
|
||||
}
|
||||
|
||||
}).export(module);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user