Skip to content

Commit

Permalink
Use Winston for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sixeyed committed Jul 25, 2020
1 parent 3d1786f commit abc140c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
13 changes: 10 additions & 3 deletions ch10/docker-images/web-ping/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
FROM node:12.17 AS builder

WORKDIR /src
COPY src/package.json .
RUN npm install

FROM node:12.17-alpine3.11

CMD ["node", "/web-ping/app.js"]
CMD ["node", "/app/app.js"]

ENV TARGET="blog.sixeyed.com" \
METHOD="HEAD" \
INTERVAL="3000"

WORKDIR /web-ping
COPY app.js .
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY src/ .
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
const https = require('https');
const log = require("./log");

const options = {
hostname: process.env.TARGET,
method: process.env.METHOD
};

console.log('** web-ping ** Pinging: %s; method: %s; %dms intervals', options.hostname, options.method, process.env.INTERVAL);
log.Logger.info('** web-ping ** Pinging: %s; method: %s; %dms intervals', options.hostname, options.method, process.env.INTERVAL);

let i = 1;
let start = new Date().getTime();
setInterval(() => {
start = new Date().getTime();
console.log('Making request number: %d; at %d', i++, start);
log.Logger.debug('Making request number: %d; at %d', i++, start);
var req = https.request(options, (res) => {
var end = new Date().getTime();
var duration = end-start;
console.log('Got response status: %s at %d; duration: %dms', res.statusCode, end, duration);
log.Logger.debug('Got response status: %s at %d; duration: %dms', res.statusCode, end, duration);
});
req.on('error', (e) => {
console.error(e);
Expand Down
16 changes: 16 additions & 0 deletions ch10/docker-images/web-ping/src/config/logConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { format, transports } = require('winston');
var logConfig = module.exports = {};

logConfig.options = {
transports: [
new transports.Console({
level: 'debug',
format: format.combine(
format.splat(),
format.printf(log => {
return `${log.message}`
})
)
})
]
};
5 changes: 5 additions & 0 deletions ch10/docker-images/web-ping/src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const winston = require('winston');
var logConfig = require('./config/logConfig');

const logger = winston.createLogger(logConfig.options);
exports.Logger = logger;
9 changes: 9 additions & 0 deletions ch10/docker-images/web-ping/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "web-ping",
"version": "1.0.0",
"main": "app.js",
"author": "kiamol",
"dependencies": {
"winston": "3.3.3"
}
}

0 comments on commit abc140c

Please sign in to comment.