Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 15 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function mongodbAppender(config) {
var layout = config.layout || log4js.layouts.messagePassThroughLayout;
var collectionName = config.collectionName || 'log';
var connectionOptions = config.connectionOptions || {};
var metadata = config.metadata || {};

function ERROR(err) {
Error.call(this);
Expand Down Expand Up @@ -51,7 +52,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 Down Expand Up @@ -96,6 +97,7 @@ function mongodbAppender(config) {
collection.insert({
timestamp: loggingEvent.startTime,
data: loggingEvent.data,
metadata: loggingEvent.metadata,
level: loggingEvent.level,
category: loggingEvent.logger.category
}, options);
Expand All @@ -104,6 +106,7 @@ function mongodbAppender(config) {
collection.insert({
timestamp: loggingEvent.startTime,
data: loggingEvent.data,
metadata: loggingEvent.metadata,
level: loggingEvent.level,
category: loggingEvent.logger.category
}, options, function (error) {
Expand All @@ -127,18 +130,20 @@ function mongodbAppender(config) {
// connect to mongodb
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');
console.error(err);
}

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

// process cache
cache.forEach(function (loggingEvent) {
setImmediate(function () {
insert(loggingEvent);
if (db && db.collection) {
collection = db.collection(config.collectionName || 'log', function() {
// process cache
cache.forEach(function (loggingEvent) {
setImmediate(function () {
insert(loggingEvent);
});
});
});
});
}
});

return function (loggingEvent) {
Expand All @@ -151,6 +156,7 @@ function mongodbAppender(config) {
}

loggingEvent.data = replaceKeys(loggingEvent.data);
loggingEvent.metadata = replaceKeys(metadata);

// save in db
insert(loggingEvent);
Expand Down
Loading