Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function mongodbAppender(config) {

function ERROR(err) {
Error.call(this);
Error.captureStackTrace(this, this.constructor);
Error.captureStackTrace(this,this.constructor);

this.name = err.toString();
this.message = err.message || 'error';
Expand All @@ -33,16 +33,16 @@ function mongodbAppender(config) {
function replaceKeys(src) {
var result = {};

function mixin(dest, source, cloneFunc) {
function mixin(dest,source,cloneFunc) {
if (lxHelpers.isObject(source)) {
lxHelpers.forEach(source, function (value, key) {
lxHelpers.forEach(source,function(value,key) {
// replace $ at start
if (key[0] === '$') {
key = key.replace('$', '_dollar_');
key = key.replace('$','_dollar_');
}

// replace all dots
key = key.replace(/\./g, '_dot_');
key = key.replace(/\./g,'_dot_');

dest[key] = cloneFunc ? cloneFunc(value) : value;
});
Expand All @@ -51,7 +51,7 @@ function mongodbAppender(config) {
return dest;
}

if (!src || typeof src !== 'object' || typeof src === 'function' || src instanceof Date || src instanceof RegExp ||  src instanceof mongodb.ObjectID) {
if (!src || typeof src !== 'object' || typeof src === 'function' || src instanceof Date || src instanceof RegExp || src instanceof mongodb.ObjectID) {
return src;
}

Expand All @@ -64,12 +64,12 @@ function mongodbAppender(config) {
if (lxHelpers.isArray(src)) {
result = [];

lxHelpers.arrayForEach(src, function (item) {
lxHelpers.arrayForEach(src,function(item) {
result.push(replaceKeys(item));
});
}

return mixin(result, src, replaceKeys);
return mixin(result,src,replaceKeys);
}

function getOptions() {
Expand All @@ -89,28 +89,33 @@ function mongodbAppender(config) {

function insert(loggingEvent) {
var options = getOptions();

var category;
if (loggingEvent.hasOwnProperty('logger.category')) {
category = loggingEvent.logger.category;
} else {
category = loggingEvent.categoryName;
}
if (collection) {
if (options.w === 0) {
// fast write
collection.insert({
timestamp: loggingEvent.startTime,
data: loggingEvent.data,
level: loggingEvent.level,
category: loggingEvent.categoryName
}, options);
category: category
},options);
} else {
// save write
collection.insert({
timestamp: loggingEvent.startTime,
data: loggingEvent.data,
level: loggingEvent.level,
category: loggingEvent.categoryName
}, options, function (error) {
category: category
},options,function(error) {
if (error) {
console.error('log: Error writing data to log!');
console.error(error);
console.log('log: Connection: %s, collection: %, data: %j', config.connectionString, collectionName, loggingEvent);
console.log('log: Connection: %s, collection: %, data: %j',config.connectionString,collectionName,loggingEvent);
}
});
}
Expand All @@ -125,23 +130,21 @@ function mongodbAppender(config) {
}

// connect to mongodb
mongodb.MongoClient.connect(config.connectionString, connectionOptions, function (err, db) {
mongodb.MongoClient.connect(config.connectionString,connectionOptions,function(err,db) {
if (err) {
console.error('Error connecting to mongodb! URL: %s', config.connectionString);
console.error('Error connecting to mongodb! URL: %s',config.connectionString);
console.error(err);
}

collection = db.collection(config.collectionName || 'log');

// process cache
cache.forEach(function (loggingEvent) {
setImmediate(function () {
cache.forEach(function(loggingEvent) {
setImmediate(function() {
insert(loggingEvent);
});
});
});

return function (loggingEvent) {
return function(loggingEvent) {
// get the information to log
if (Object.prototype.toString.call(loggingEvent.data[0]) === '[object String]') {
// format string with layout
Expand All @@ -159,7 +162,7 @@ function mongodbAppender(config) {

function configure(config) {
if (config.layout) {
config.layout = log4js.layouts.layout(config.layout.type, config.layout);
config.layout = log4js.layouts.layout(config.layout.type,config.layout);
}

return mongodbAppender(config);
Expand Down