Skip to content
Permalink
Browse files

Move clf date to own file

  • Loading branch information
davidmerfield committed Nov 24, 2019
1 parent 48e32e1 commit b6b251e2783b112035908bd765a87361604dab48
Showing with 85 additions and 44 deletions.
  1. +58 −0 app/helper/clfdate.js
  2. +27 −44 app/index.js
@@ -0,0 +1,58 @@
var CLF_MONTH = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];

function pad2(num) {
var str = String(num);

return (str.length === 1 ? "0" : "") + str;
}

function rpad3(num) {
var str = String(num);

if (str.length === 2) return str + "0";

if (str.length === 1) return str + "00";

return str;
}

module.exports = function clfdate(dateTime) {
var date = dateTime.getUTCDate();
var hour = dateTime.getUTCHours();
var mins = dateTime.getUTCMinutes();
var secs = dateTime.getUTCSeconds();
var year = dateTime.getUTCFullYear();
var msecs = dateTime.getUTCMilliseconds();

var month = CLF_MONTH[dateTime.getUTCMonth()];

return (
pad2(date) +
"/" +
month +
"/" +
year +
":" +
pad2(hour) +
":" +
pad2(mins) +
":" +
pad2(secs) +
"." +
rpad3(msecs) +
" +0000"
);
};
@@ -2,7 +2,7 @@ var config = require("config");
var Express = require("express");
var helmet = require("helmet");
var vhost = require("vhost");

var clfdate = require("helper").clfdate;
var blog = require("./blog");
var brochure = require("./brochure");
var dashboard = require("./dashboard");
@@ -24,56 +24,39 @@ Blot.set("trust proxy", "loopback");
// Prevent <iframes> embedding pages served by Blot
Blot.use(helmet.frameguard("allow-from", config.host));

Blot.use(function(req, res, next){
Blot.use(function(req, res, next) {
var init = Date.now();

console.log('[' + clfdate(new Date()) +']', req.headers['x-request-id'], 'REQ', req.protocol + '://' + req.hostname + req.originalUrl, req.method);

res.on('finish', function(){
console.log('[' + clfdate(new Date())+']', req.headers['x-request-id'], 'RES', req.protocol + '://' + req.hostname + req.originalUrl, res.statusCode, Date.now() - init + 'ms');
try {
console.log(
"[" + clfdate(new Date()) + "]",
req.headers["x-request-id"],
"REQ",
req.protocol + "://" + req.hostname + req.originalUrl,
req.method
);
} catch (e) {
console.error("Error: Failed to construct canonical log line:", e);
}

res.on("finish", function() {
try {
console.log(
"[" + clfdate(new Date()) + "]",
req.headers["x-request-id"],
"RES",
req.protocol + "://" + req.hostname + req.originalUrl,
res.statusCode,
Date.now() - init + "ms"
);
} catch (e) {
console.error("Error: Failed to construct canonical log line:", e);
}
});

next();
});

var CLF_MONTH = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];

function pad2 (num) {
var str = String(num)

return (str.length === 1 ? '0' : '') + str
}

function rpad3 (num) {
var str = String(num)

if (str.length === 2)
return str + '0';

if (str.length === 1)
return str + '00';

return str;
}

function clfdate (dateTime) {
var date = dateTime.getUTCDate()
var hour = dateTime.getUTCHours()
var mins = dateTime.getUTCMinutes()
var secs = dateTime.getUTCSeconds()
var year = dateTime.getUTCFullYear()
var msecs = dateTime.getUTCMilliseconds();

var month = CLF_MONTH[dateTime.getUTCMonth()]

return pad2(date) + '/' + month + '/' + year +
':' + pad2(hour) + ':' + pad2(mins) + ':' + pad2(secs) + '.' + rpad3(msecs)
' +0000'
}

// Blot is composed of four sub applications.

// The CDN

0 comments on commit b6b251e

Please sign in to comment.
You can’t perform that action at this time.