Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb9948145c | ||
|
|
0242bae78f | ||
|
|
b71f635267 | ||
|
|
66872d136d | ||
|
|
49849a545a | ||
|
|
3c4c98bb0b | ||
|
|
d65d053bc1 |
@@ -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
|
||||
@@ -17,8 +19,8 @@ var layouts = require('../layouts')
|
||||
function logglyAppender(config, layout) {
|
||||
var client = loggly.createClient(config);
|
||||
|
||||
var packageMessage = function (loggingEvent) {
|
||||
var BaseItem = function(level, msg) {
|
||||
function packageMessage(loggingEvent) {
|
||||
function BaseItem(level, msg) {
|
||||
this.level = level || loggingEvent.level.toString();
|
||||
this.category = loggingEvent.categoryName;
|
||||
this.hostname = os.hostname().toString();
|
||||
@@ -26,48 +28,13 @@ function logglyAppender(config, layout) {
|
||||
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;
|
||||
var formattedMsg = passThrough(loggingEvent);
|
||||
return new BaseItem(formattedMsg);
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
client.log(a, config.tags);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,4 +48,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); });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "log4js",
|
||||
"version": "0.6.16",
|
||||
"version": "0.6.17",
|
||||
"description": "Port of Log4js to work with node.",
|
||||
"keywords": [
|
||||
"logging",
|
||||
|
||||
@@ -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 {
|
||||
@@ -66,7 +67,7 @@ vows.describe('log4js categoryFilter').addBatch({
|
||||
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,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,10 @@ 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());
|
||||
},
|
||||
'%c should handle category names like java-style package names': function(args) {
|
||||
test(args, '%c{1}', 'tests');
|
||||
@@ -247,7 +249,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 {
|
||||
@@ -60,8 +62,8 @@ vows.describe('log4js logLevelFilter').addBatch({
|
||||
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']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user