Skip to content

Commit

Permalink
feat: new env vars for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dexteryy committed May 27, 2017
1 parent d9f966d commit 536b9f1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion configs/babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"presets": [
["env", {
"targets": {
"node": 7.7
"node": "7.8"
},
"include": [],
"useBuiltIns": true,
Expand Down
6 changes: 5 additions & 1 deletion configs/env.sample.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ NODECUBE_DISABLE_COMPRESS=
NODECUBE_DISABLE_BODY_PARSER=
NODECUBE_ENABLE_HEADERS_LOG=
NODECUBE_ENABLE_RAW_BODY_LOG=
NODECUBE_ENABLE_VERBOSE_LOG=
NODECUBE_LOG_LEVEL=info
NODECUBE_ENABLE_JSON_LOG=
NODECUBE_ENABLE_COLOR_LOG=
NODECUBE_ENABLE_READABLE_EXCEPTION_LOG=
NODECUBE_DISABLE_EXCEPTION_LOG=
NODECUBE_DISABLE_STAT_API=
NODECUBE_ENABLE_INSPECT_API=
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"homepage": "https:/dexteryy/nodecube",
"repository": "https:/dexteryy/nodecube",
"version": "0.4.1",
"version": "0.5.0",
"main": "index.js",
"scripts": {
"prepare": "npm run lint && npm run build",
Expand Down
33 changes: 12 additions & 21 deletions src/errorResponse.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@

const RE_DATA = /(.*)\[\[(.+?)\]\](.*)/;

export default function errorResponse(req, res) {
const requestId = req.get('Request-Id');
return (err = new Error('UNDEFINED ERROR')) => {
let data = err;
const message = err.message
.replace(RE_DATA, ($0, before, json = '', after) => {
try {
data = JSON.parse(json) || {};
} catch (ex) {
data = err;
}
return before + after;
});
if (!data.isExpected) {
if (process.env.NODE_ENV === 'production') {
logger.error(`[Request-Id: ${requestId}]`, err);
} else {
console.error(requestId, err);
}
const requestId = res.get('Request-Id');
return (err = {}) => {
const {
status = -100,
isExpected,
message = 'UNDEFINED ERROR',
meta = {},
} = err;
if (!isExpected) {
logger.error(`[${requestId}] ${status} | ${message}`, meta);
}
res.json({
status: data.status || -100,
status,
message,
meta,
});
};
}
5 changes: 3 additions & 2 deletions src/errorTrigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ export default function errorTrigger({
status = -101,
isExpected = true,
message,
...meta
}) {
const errData = JSON.stringify({
throw Object.assign(new Error(message), {
status,
isExpected,
meta,
});
throw new Error(`[[${errData}]] ${message}`);
}
26 changes: 15 additions & 11 deletions src/httpService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getRawBody from 'raw-body';
import contentType from 'content-type';
import cookieParser from 'cookie-parser';
import httpLogger from 'morgan';
import logger from './logger';
import winston from 'winston';
import expressWinston from 'express-winston';
import errorHandler from 'errorhandler';
Expand All @@ -18,7 +19,7 @@ import helmet from 'helmet';
// import passport from 'passport';
import uuid from 'uuid/v4';
import corsManager from './corsManager';
import logger from './logger';

global.logger = logger;

const isProductionEnv = process.env.NODE_ENV === 'production';
Expand Down Expand Up @@ -63,7 +64,7 @@ export default function httpService({
if (process.env.NODECUBE_ENABLE_HEADERS_LOG) {
server.use((req, res, next) => {
const rid = res.get('Request-Id');
logger.info(`[${rid}] headers: ${JSON.stringify(req.headers)}`);
logger.info(`[${rid}] HEADERS:`, req.headers);
next();
});
}
Expand All @@ -80,9 +81,9 @@ export default function httpService({
length: req.headers['content-length'],
encoding,
}).then(str => {
logger.info(`[${rid}] raw body: ${str}`);
logger.info(`[${rid}] RAW BODY: ${str}`);
}).catch(err => {
logger.info(`[${rid}] raw body: Error! ${err.message}`);
logger.error(`[${rid}] RAW BODY ERROR: ${err.message}`);
});
next();
});
Expand Down Expand Up @@ -167,14 +168,17 @@ export default function httpService({

server.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
level: process.env.NODECUBE_ENABLE_VERBOSE_LOG ? 'verbose' : 'info',
colorize: false,
json: true,
prettyPrint: false,
humanReadableUnhandledException: false,
}),
new winston.transports.Console(logger.consoleConfig),
],
dynamicMeta(req, res) {
const meta = [{
requestId: res.get('Request-Id'),
}];
if (req.user) {
meta.push(req.user);
}
return meta;
},
}));

if (!isProductionEnv) {
Expand Down
18 changes: 8 additions & 10 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@

import winston from 'winston';

const isProductionEnv = process.env.NODE_ENV === 'production';

const consoleConfig = new winston.transports.Console({
level: process.env.NODECUBE_ENABLE_VERBOSE_LOG ? 'verbose' : 'info',
colorize: !isProductionEnv,
json: isProductionEnv,
prettyPrint: !isProductionEnv,
humanReadableUnhandledException: !isProductionEnv,
});
const consoleConfig = {
level: process.env.NODECUBE_LOG_LEVEL || 'info',
colorize: !!process.env.NODECUBE_ENABLE_COLOR_LOG,
json: !!process.env.NODECUBE_ENABLE_JSON_LOG,
handleExceptions: !process.env.NODECUBE_DISABLE_EXCEPTION_LOG,
humanReadableUnhandledException: !!process.env.NODECUBE_ENABLE_READABLE_EXCEPTION_LOG,
};

const logger = new winston.Logger({
transports: [
consoleConfig,
new winston.transports.Console(consoleConfig),
],
});

Expand Down

0 comments on commit 536b9f1

Please sign in to comment.