extended request logging

This commit is contained in:
Daniel Sommer 2022-02-11 00:27:29 +01:00
parent c2793c7e02
commit adb597e83b
2 changed files with 74 additions and 44 deletions

View file

@ -1,17 +1,17 @@
const config = require("../config.json"); const config = require('../config.json');
const moment = require("moment"); const moment = require('moment');
// constants // constants
const LOG_PREFIX_DEBUG = "debug"; const LOG_PREFIX_DEBUG = 'debug';
const LOG_PREFIX_INFO = "info"; const LOG_PREFIX_INFO = 'info';
const LOG_PREFIX_WARNING = "warning"; const LOG_PREFIX_WARNING = 'warning';
const LOG_PREFIX_ERROR = "error"; const LOG_PREFIX_ERROR = 'error';
const LOGLEVEL_DEBUG = 0; const LOGLEVEL_DEBUG = 0;
const LOGLEVEL_INFO = 1; const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2; const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3; const LOGLEVEL_ERROR = 3;
// set loglevel on "require" // set loglevel on 'require'
const loglevel = function () { const loglevel = function () {
switch (config.log.level) { switch (config.log.level) {
case LOG_PREFIX_DEBUG: case LOG_PREFIX_DEBUG:
@ -31,18 +31,31 @@ const loglevel = function() {
} }
}(); }();
// log a http request // log a http request - response object
function request(request) { function http(object) {
let message = "[" + request.method + "] url: \"" + request.url + "\""; if (object == undefined) {
return;
}
let message = '[' + object.request.method + ':' + object.code + '] url: \'' + object.request.url + '\'';
let counter = 1; let counter = 1;
for (let param in request.body) { for (let param in object.request.body) {
message += ", parameter " + counter + ": \"" + param + "=" + request.body[param] + "\""; message += ', parameter ' + counter + ': \'' + param + '=' + object.request.body[param] + '\'';
counter++; counter++;
} }
if (object.request.timestamp) {
message += ' > ' + (new Date().getTime() - object.request.timestamp) + 'ms';
}
if (object.data) {
message += ' > data: ' + object.data;
}
if (object.code != 200) {
error(message.trim());
return;
}
debug(message.trim()); debug(message.trim());
} }
// prefix log with "info" // prefix log with 'info'
function info(message) { function info(message) {
if (loglevel > LOGLEVEL_INFO) { if (loglevel > LOGLEVEL_INFO) {
return; return;
@ -50,28 +63,28 @@ function info(message) {
trace(message); trace(message);
} }
// prefix log with "info" // prefix log with 'info'
function warn(message) { function warn(message) {
if (loglevel > LOGLEVEL_WARNING) { if (loglevel > LOGLEVEL_WARNING) {
return; return;
} }
trace(message, "warning"); trace(message, 'warning');
} }
// prefix log with "debug" // prefix log with 'debug'
function debug(message) { function debug(message) {
if (loglevel > LOGLEVEL_DEBUG) { if (loglevel > LOGLEVEL_DEBUG) {
return; return;
} }
trace(message, "debug"); trace(message, 'debug');
} }
// prefix log with "error" // prefix log with 'error'
function error(message) { function error(message) {
if (loglevel > LOGLEVEL_ERROR) { if (loglevel > LOGLEVEL_ERROR) {
return; return;
} }
trace(message, "error"); trace(message, 'error');
} }
// default logging function // default logging function
@ -80,23 +93,23 @@ function trace(message, prefix) {
return; return;
} }
if (prefix === undefined || prefix === null || prefix.length === 0) { if (prefix === undefined || prefix === null || prefix.length === 0) {
prefix = "info"; prefix = 'info';
} }
let print; let print;
switch (prefix) { switch (prefix) {
case "error": case 'error':
print = console.error; print = console.error;
break; break;
case "debug": case 'debug':
print = console.debug; print = console.debug;
break; break;
case "warning": case 'warning':
print = console.warn; print = console.warn;
break; break;
default: default:
print = console.log; print = console.log;
} }
message = moment().format(config.server.timestamp) + " | " + prefix + " > " + message; message = moment().format(config.server.timestamp) + ' | ' + prefix + ' > ' + message;
print(message); print(message);
} }
@ -106,5 +119,5 @@ module.exports = {
warn, warn,
debug, debug,
error, error,
request http
}; };

View file

@ -21,24 +21,28 @@ function start() {
function handleRequests() { function handleRequests() {
server.on('request', function (request, response) { server.on('request', function (request, response) {
logger.request(request); request.timestamp = new Date().getTime();
if (request.url.endsWith('/')) { if (request.url.endsWith('/')) {
request.url = request.url.substring(0, request.url.length - 1); request.url = request.url.substring(0, request.url.length - 1);
} }
var endpoint = api.getEndpoints().get(request.url); var endpoint = api.getEndpoints().get(request.url);
if (!endpoint) { if (!endpoint) {
var msg = 'endpoint \'' + request.url + '\' not defined'; endRequest(
response.writeHead(501); request,
response.end(msg); response,
logger.debug(msg); 'endpoint \'' + request.url + '\' not defined',
501
);
return; return;
} }
endpoint = endpoint[request.method]; endpoint = endpoint[request.method];
if (endpoint == undefined || endpoint.method == undefined) { if (endpoint == undefined || endpoint.method == undefined) {
var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests'; endRequest(
response.writeHead(405); request,
response.end(msg); response,
logger.debug(msg); 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests',
405
);
return; return;
} }
getRequestParams(request) getRequestParams(request)
@ -46,24 +50,37 @@ function handleRequests() {
return endpoint.method(endpoint.id, params); return endpoint.method(endpoint.id, params);
}) })
.then(function (result) { .then(function (result) {
response.writeHead(200); endRequest(request, response, result);
try {
response.end(JSON.stringify(result));
} catch (err) {
response.end(result);
}
}) })
.catch(function (err, code) { .catch(function (err, code) {
logger.error(err);
if (code == undefined) { if (code == undefined) {
code = 500; code = 500;
} }
response.writeHead(500); endRequest(request, response, code, err);
response.end(err);
}); });
}); });
} }
function endRequest(request, response, msg, code) {
if (code == undefined) {
code = 200;
}
var object = {
request: request,
code: code
}
if (msg != undefined) {
try {
object.data = JSON.stringify(msg);
} catch {
object.data = msg;
}
}
response.writeHead(object.code);
response.end(object.data);
logger.http(object);
}
function getRequestParams(request) { function getRequestParams(request) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var params = ""; var params = "";