2022-02-11 00:27:29 +01:00
|
|
|
const config = require('../config.json');
|
|
|
|
const moment = require('moment');
|
2022-02-07 15:41:27 +01:00
|
|
|
|
|
|
|
// constants
|
2022-02-11 00:27:29 +01:00
|
|
|
const LOG_PREFIX_DEBUG = 'debug';
|
|
|
|
const LOG_PREFIX_INFO = 'info';
|
|
|
|
const LOG_PREFIX_WARNING = 'warning';
|
|
|
|
const LOG_PREFIX_ERROR = 'error';
|
2022-02-07 15:41:27 +01:00
|
|
|
const LOGLEVEL_DEBUG = 0;
|
|
|
|
const LOGLEVEL_INFO = 1;
|
|
|
|
const LOGLEVEL_WARNING = 2;
|
|
|
|
const LOGLEVEL_ERROR = 3;
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// set loglevel on 'require'
|
|
|
|
const loglevel = function () {
|
2022-02-07 15:41:27 +01:00
|
|
|
switch (config.log.level) {
|
|
|
|
case LOG_PREFIX_DEBUG:
|
|
|
|
case LOGLEVEL_DEBUG:
|
|
|
|
return LOGLEVEL_DEBUG;
|
|
|
|
case LOG_PREFIX_INFO:
|
|
|
|
case LOGLEVEL_INFO:
|
|
|
|
return LOGLEVEL_INFO;
|
|
|
|
case LOG_PREFIX_WARNING:
|
|
|
|
case LOGLEVEL_WARNING:
|
|
|
|
return LOGLEVEL_WARNING;
|
|
|
|
case LOG_PREFIX_ERROR:
|
|
|
|
case LOGLEVEL_ERROR:
|
|
|
|
return LOGLEVEL_ERROR;
|
|
|
|
default:
|
|
|
|
return LOGLEVEL_INFO;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// log a http request - response object
|
|
|
|
function http(object) {
|
|
|
|
if (object == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let message = '[' + object.request.method + ':' + object.code + '] url: \'' + object.request.url + '\'';
|
2022-02-07 15:41:27 +01:00
|
|
|
let counter = 1;
|
2022-02-11 00:27:29 +01:00
|
|
|
for (let param in object.request.body) {
|
|
|
|
message += ', parameter ' + counter + ': \'' + param + '=' + object.request.body[param] + '\'';
|
2022-02-07 15:41:27 +01:00
|
|
|
counter++;
|
|
|
|
}
|
2022-02-11 00:27:29 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-02-07 15:41:27 +01:00
|
|
|
debug(message.trim());
|
|
|
|
}
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// prefix log with 'info'
|
2022-02-07 15:41:27 +01:00
|
|
|
function info(message) {
|
|
|
|
if (loglevel > LOGLEVEL_INFO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace(message);
|
|
|
|
}
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// prefix log with 'info'
|
2022-02-07 15:41:27 +01:00
|
|
|
function warn(message) {
|
|
|
|
if (loglevel > LOGLEVEL_WARNING) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-11 00:27:29 +01:00
|
|
|
trace(message, 'warning');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// prefix log with 'debug'
|
2022-02-07 15:41:27 +01:00
|
|
|
function debug(message) {
|
|
|
|
if (loglevel > LOGLEVEL_DEBUG) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-11 00:27:29 +01:00
|
|
|
trace(message, 'debug');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 00:27:29 +01:00
|
|
|
// prefix log with 'error'
|
2022-02-07 15:41:27 +01:00
|
|
|
function error(message) {
|
|
|
|
if (loglevel > LOGLEVEL_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-11 00:27:29 +01:00
|
|
|
trace(message, 'error');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// default logging function
|
|
|
|
function trace(message, prefix) {
|
|
|
|
if (message === undefined || message === null || message.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (prefix === undefined || prefix === null || prefix.length === 0) {
|
2022-02-11 00:27:29 +01:00
|
|
|
prefix = 'info';
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
let print;
|
|
|
|
switch (prefix) {
|
2022-02-11 00:27:29 +01:00
|
|
|
case 'error':
|
2022-02-07 15:41:27 +01:00
|
|
|
print = console.error;
|
|
|
|
break;
|
2022-02-11 00:27:29 +01:00
|
|
|
case 'debug':
|
2022-02-07 15:41:27 +01:00
|
|
|
print = console.debug;
|
|
|
|
break;
|
2022-02-11 00:27:29 +01:00
|
|
|
case 'warning':
|
2022-02-07 15:41:27 +01:00
|
|
|
print = console.warn;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print = console.log;
|
|
|
|
}
|
2022-02-11 00:27:29 +01:00
|
|
|
message = moment().format(config.server.timestamp) + ' | ' + prefix + ' > ' + message;
|
2022-02-07 15:41:27 +01:00
|
|
|
print(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
|
|
|
info,
|
|
|
|
warn,
|
|
|
|
debug,
|
|
|
|
error,
|
2022-02-11 00:27:29 +01:00
|
|
|
http
|
2022-02-07 15:41:27 +01:00
|
|
|
};
|