@@ -6,6 +6,8 @@ import compress from 'compression';
66import methodOverride from 'method-override' ;
77import cors from 'cors' ;
88import httpStatus from 'http-status' ;
9+ import expressWinston from 'express-winston' ;
10+ import winstonInstance from './winston' ;
911import routes from '../server/routes' ;
1012import config from './env' ;
1113import * as utilityService from '../server/services/utility' ;
@@ -25,6 +27,18 @@ app.use(methodOverride());
2527// enable CORS - Cross Origin Resource Sharing
2628app . use ( cors ( ) ) ;
2729
30+ // enable detailed API logging in dev env
31+ if ( config . NODE_ENV === 'development' ) {
32+ expressWinston . requestWhitelist . push ( 'body' ) ;
33+ expressWinston . responseWhitelist . push ( 'body' ) ;
34+ app . use ( expressWinston . logger ( {
35+ winstonInstance,
36+ meta : true , // optional: log meta data about request (defaults to true)
37+ msg : 'HTTP {{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms' ,
38+ colorStatus : true // Color the status code (default green, 3XX cyan, 4XX yellow, 5XX red).
39+ } ) ) ;
40+ }
41+
2842// mount all routes on /api path
2943app . use ( '/api' , routes ) ;
3044
@@ -40,11 +54,16 @@ app.use((req, res, next) => {
4054 return next ( err ) ;
4155} ) ;
4256
57+ // log error in winston transports
58+ app . use ( expressWinston . errorLogger ( {
59+ winstonInstance
60+ } ) ) ;
61+
4362// error handler, send stacktrace only during development
4463app . use ( ( err , req , res , next ) => // eslint-disable-line no-unused-vars
4564 res . status ( err . status ) . json ( {
4665 message : err . isPublic ? err . message : httpStatus [ err . status ] ,
47- error : config . NODE_ENV === 'development' ? err : { }
66+ stack : config . NODE_ENV === 'development' ? err . stack : { }
4867 } )
4968) ;
5069
0 commit comments