Skip to content
Snippets Groups Projects
Commit 07ee5f4c authored by Konsta's avatar Konsta
Browse files

Added logger.

parent 311d865d
No related branches found
No related tags found
1 merge request!7Added logger.
const logger = require('./logger')
var http = require('http'), var http = require('http'),
path = require('path'), path = require('path'),
methods = require('methods'), methods = require('methods'),
...@@ -14,6 +16,16 @@ var isProduction = process.env.NODE_ENV === 'production'; ...@@ -14,6 +16,16 @@ var isProduction = process.env.NODE_ENV === 'production';
// Create global app object // Create global app object
var app = express(); 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()); app.use(cors());
// Normal express config defaults // Normal express config defaults
...@@ -57,7 +69,7 @@ app.use(function(req, res, next) { ...@@ -57,7 +69,7 @@ app.use(function(req, res, next) {
// will print stacktrace // will print stacktrace
if (!isProduction) { if (!isProduction) {
app.use(function(err, req, res, next) { app.use(function(err, req, res, next) {
console.log(err.stack); logger.info(err.stack);
res.status(err.status || 500); res.status(err.status || 500);
...@@ -80,5 +92,5 @@ app.use(function(err, req, res, next) { ...@@ -80,5 +92,5 @@ app.use(function(err, req, res, next) {
// finally, let's start our server... // finally, let's start our server...
var server = app.listen( process.env.PORT || 3000, function(){ 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);
}); });
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment