mongologger
v0.0.8
Published
A lightweight tool for sending logs to MongoDB
Downloads
12
Readme
mongoLogger
A lightweight tool for shipping logs to mongoDB.
Install
npm install mongologger
Usage
mongoLogger will look for a config.js and package.json in the working directory. It should look something like this:
module.exports = {
mongologger: {
dbaddress : "mongodb://localhost/",
database : "logs",
appname : "mongologger"
}
};
Require mongoLogger (note the ()
on the require) and start logging:
var log = require('mongoLogger')()
log.trace('tracing...');
log.info('information');
log.warn('something doesnt look right');
log.error('houston we have a problem');
log.fatal('AGGGHHHH!!!');
Logs will be stored with the log level, message, hostname, appname, and version (from package.json) number:
{
"_id" : ObjectId("55ca6b8485303dfc1bc4ccc7"),
"logLevel" : 1,
"message" : "pls halp is full of squirlz!",
"hostname" : "honeypot",
"appname" : "mongoLogger",
"version" : "0.0.0"
}
Options & overrides
Additional options can be passed to basic logging functions. These options include:
log.error('logging some errors...', {
servicename: 'https://fooservice.com/api',
request: 'POST /foo-the-bar',
response: 'HTTP 403 - fooing the bar is forbidden!!!',
requestID: '79c750df-d4cf-49fe-9b71-461ec43a7f25'
});
Data pulled from the config.js can also be overriden:
log.info('logging some info...', {
hostname: 'hannibal',
appname: 'souffle',
version: '1.0.0'
});
Tack
Additional information can be appended to the log by using the tack method:
log.tack(id, 'corkboard', {
thumbtacks: ["blue", "red", "green"]
});
The object passed will be set in the additional
property:
{
"_id" : ObjectId("55d64f6de865edf430262d02"),
"logLevel" : 1,
"message" : "wall",
"additional" :
{
"corkboard" { thumbtacks : ["blue", "red", "green"] }
},
"hostname" : "zeus",
"appname" : "mongoLogger",
"version" : "0.0.0"
}
Duration logging
mongoLogger also supports timing the duration of a function block by passing a watch
object and logging the results as appropriate:
log.duration({ message: "database call took a long time!!", info: false, warn: 250}, function (watch) {
setTimeout(function () {
log.stop(watch)
}, 500);
});
By default, any errors thrown will log as logLevel: 3
(error). This can be overidden with warn: false
, just as logging the duration of ALL calls under info can be overriden with info: false
.