Skip to content

Instantly share code, notes, and snippets.

@sqpierce
Created October 19, 2012 17:54
Show Gist options
  • Save sqpierce/3919625 to your computer and use it in GitHub Desktop.
Save sqpierce/3919625 to your computer and use it in GitHub Desktop.
Simple node.js based logging server using express and Winston
// requirements
var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;
var express = require('express');
// set up winston
winston.add(MongoDB, {db:'winston'});
winston.remove(winston.transports.Console);
winston.emitErrs = true;
winston.handleExceptions(new winston.transports.File({ filename: 'exceptions.log' }))
// set up express app
var app = express();
// found out the hard way that this middleware is required to parse data
app.use(express.bodyParser());
// routing
// note: urls not implemented will serve 404 not found
app.all('/api/log', function(req, res){
res.status(301).redirect('/api/v1/log');
});
app.get('/api/v1/log', function(req, res){
res.send(500, { error: 'GET not implemented in v1' });
});
app.post('/api/v1/log', function(req, res){
var required = ['appname', 'logname', 'source', 'logentry'];
for(param in required){
if(! req.param(required[param])){
res.send(500, { error: required[param]+' is required' });
return false;
}
}
winston.info('Winston Logging', req.body);
res.send('Thanks for that, '+req.body.appname);
});
// start server
app.listen(8888);
console.log('Listening on port 8888');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment