diff --git a/app.js b/app.js index 460baa408e848adf0b10d8c2812596fed38cc7e1..0fa24f91182449024bcdc4c3ad0b01f697089687 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,5 @@ +const logger = require('./logger') + var http = require('http'), path = require('path'), methods = require('methods'), @@ -14,6 +16,16 @@ var isProduction = process.env.NODE_ENV === 'production'; // Create global app object var app = express(); +app.use((req, res, next) => { + logger.info(req.body) + let origSend = res.send + res.send = function(data) { + logger.info(data) + origSend.apply(res, arguments) + } + next() +}) + app.use(cors()); // Normal express config defaults @@ -57,7 +69,7 @@ app.use(function(req, res, next) { // will print stacktrace if (!isProduction) { app.use(function(err, req, res, next) { - console.log(err.stack); + logger.info(err.stack); res.status(err.status || 500); @@ -80,5 +92,5 @@ app.use(function(err, req, res, next) { // finally, let's start our server... var server = app.listen( process.env.PORT || 3000, function(){ - console.log('Listening on port ' + server.address().port); + logger.info('Listening on port ' + server.address().port); }); diff --git a/logger.js b/logger.js new file mode 100644 index 0000000000000000000000000000000000000000..bab998dd4159721da139bb08706852e0fcb865fc --- /dev/null +++ b/logger.js @@ -0,0 +1,22 @@ +const {​​​​​ createLogger, transports, format }​​​​​ = require('winston') + +constlogger = createLogger({​​​​​ + transports: [ + newtransports.File({​​​​​ + filename:'error.log', + level:'error', + format:format.combine(format.timestamp(), format.json()) + }​​​​​), + newtransports.File({​​​​​ + filename:'info.log', + level:'info', + format:format.combine(format.timestamp(), format.json()) + }​​​​​), + newtransports.Console({​​​​​ + level:'info', + format:format.combine(format.timestamp(), format.colorize(), format.json()) + }​​​​​) + ] +}​​​​​) + +module.exports = logger \ No newline at end of file