Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit da3904f

Browse files
committed
use winston for logging
1 parent 2660e25 commit da3904f

File tree

6 files changed

+204
-10
lines changed

6 files changed

+204
-10
lines changed

app.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const express = require('express');
22
const app = express();
3+
const path = require('path');
34
const GitHub = require('github-api');
45
const Promise = require('bluebird');
56
const JSONStream = require('JSONStream');
67
const moment = require('moment');
7-
const morgan = require('morgan');
8+
const requestLog = require('./request-log');
9+
const logger = require('./logger');
810
const retry = require('bluebird-retry');
911
const _ = require('lodash');
1012

@@ -24,7 +26,7 @@ const getMessage = (err) => {
2426
};
2527

2628
const catchGitHub401or404 = (err) => {
27-
console.log(`GITHUB ERROR: ${err.message}`);
29+
logger.info(`GITHUB ERROR: ${err.message}`);
2830
const errorCode = getErrorCode(err.response || {});
2931
if (errorCode === 401 || errorCode === 404) {
3032
err.status = errorCode;
@@ -34,7 +36,7 @@ const catchGitHub401or404 = (err) => {
3436
};
3537

3638
const processGitHubResponse = (r) => {
37-
console.log(`${r.config.method} ${r.status} ${r.config.url} | ${r.statusText}`);
39+
logger.info(`${r.config.method} ${r.status} ${r.config.url} | ${r.statusText}`);
3840
if (r.status === 204) {
3941
return [];
4042
}
@@ -90,7 +92,7 @@ const writeRepoStatsToStream = async (github, repo, stream) => {
9092
const writeStatsToStream = async (repos, github, stream) => {
9193
await Promise.each(repos, (repo) => writeRepoStatsToStream(github, repo, stream))
9294
.catch(err => {
93-
console.log(`ERROR STREAMING: ${err.message}`);
95+
logger.info(`ERROR STREAMING: ${err.message}`);
9496
return stream.write({
9597
__streamError: {
9698
message: getMessage(err),
@@ -110,23 +112,29 @@ const getAuthToken = (req) => {
110112

111113

112114
const streamStats = async (github, repos, out) => {
113-
console.log(`Count of repos to process is ${repos.length}`);
115+
logger.info(`Count of repos to process is ${repos.length}`);
114116
out.type('json');
115117
const stream = JSONStream.stringify();
116118
stream.pipe(out);
117119
await writeStatsToStream(repos, github, stream);
118120
stream.end();
119121
};
120122

121-
morgan.token('path', (req) => req.path);
123+
app.use(requestLog());
122124

123-
app.use(morgan(':method :status :path :response-time ms'));
125+
app.get('/favicon.ico', function (req, res) {
126+
res.sendFile(path.resolve('./favicon.ico'));
127+
});
124128

125129
app.get('/vizydrop-status-ping', (req, res) => {
126130
res.json({ok:true});
127131
});
128132

129133
app.get('/', async (req, res, next) => {
134+
if(!getAuthToken(req)){
135+
res.sendStatus(401);
136+
return;
137+
}
130138
try {
131139
const github = await initGitHub(req);
132140
const repos = await getRepositoriesForLoggedUser(github);
@@ -181,5 +189,5 @@ app.use((err, req, res, next) => res.status(getErrorCode(err)).send({
181189
}));
182190

183191
const port = process.env.PORT || 7770;
184-
const server = app.listen(port, () => console.log(`github-data-link is listening on port ${port}!`));
192+
const server = app.listen(port, () => logger.info(`github-data-link is listening on port ${port}!`));
185193
module.exports = () => server;

favicon.ico

14.7 KB
Binary file not shown.

logger.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const winston = require(`winston`);
2+
3+
const isProduction = process.env.NODE_ENV === `production`;
4+
const isDevelopment = isProduction === false;
5+
6+
const logger = new (winston.Logger)({
7+
transports: [
8+
new (winston.transports.Console)({
9+
humanReadableUnhandledException: isDevelopment,
10+
json: isProduction,
11+
level: `info`,
12+
colorize: `true`,
13+
logstash: isProduction,
14+
prettyPrint: isDevelopment,
15+
stringify: isProduction,
16+
timestamp: true,
17+
}),
18+
],
19+
});
20+
21+
module.exports = logger;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"github-api": "^3.0.0",
1010
"lodash": "^4.17.10",
1111
"moment": "^2.22.2",
12-
"morgan": "^1.9.1"
12+
"morgan": "^1.9.1",
13+
"winston": "2.4.4"
1314
},
1415
"scripts": {
1516
"start": "node app.js"

request-log.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const logger = require(`./logger`);
2+
3+
const morgan = require(`morgan`);
4+
5+
morgan.token('path', (req) => req.path);
6+
7+
module.exports = () => morgan(`:method :status :path :response-time ms`, {
8+
stream: {write: (text) => logger.info(text)},
9+
skip: (req) => req.url === `/status`,
10+
});

0 commit comments

Comments
 (0)