2022-02-15 04:33:19 +01:00
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
// constants
|
|
|
|
const LOG_PREFIX_DEBUG = 'debug';
|
|
|
|
const LOG_PREFIX_INFO = 'info';
|
|
|
|
const LOG_PREFIX_WARNING = 'warning';
|
|
|
|
const LOG_PREFIX_ERROR = 'error';
|
|
|
|
const LOGLEVEL_DEBUG = 0;
|
|
|
|
const LOGLEVEL_INFO = 1;
|
|
|
|
const LOGLEVEL_WARNING = 2;
|
|
|
|
const LOGLEVEL_ERROR = 3;
|
|
|
|
|
2022-03-10 12:29:41 +01:00
|
|
|
let loglevel;
|
|
|
|
let timestamp;
|
2022-02-20 21:38:27 +01:00
|
|
|
|
2022-03-10 12:29:41 +01:00
|
|
|
|
|
|
|
function initialize(loglevel, timestamp) {
|
|
|
|
setLogLevel(loglevel);
|
|
|
|
setTimestamp(timestamp);
|
2022-02-20 21:38:27 +01:00
|
|
|
}
|
2022-02-16 02:08:21 +01:00
|
|
|
|
2022-03-10 12:29:41 +01:00
|
|
|
// set the loglevel
|
|
|
|
function setLogLevel(value) {
|
|
|
|
switch (value) {
|
2022-02-15 04:33:19 +01:00
|
|
|
case LOG_PREFIX_DEBUG:
|
|
|
|
case LOGLEVEL_DEBUG:
|
2022-03-10 12:29:41 +01:00
|
|
|
loglevel = LOGLEVEL_DEBUG;
|
|
|
|
break;
|
2022-02-15 04:33:19 +01:00
|
|
|
case LOG_PREFIX_INFO:
|
|
|
|
case LOGLEVEL_INFO:
|
2022-03-10 12:29:41 +01:00
|
|
|
loglevel = LOGLEVEL_INFO;
|
|
|
|
break;
|
2022-02-15 04:33:19 +01:00
|
|
|
case LOG_PREFIX_WARNING:
|
|
|
|
case LOGLEVEL_WARNING:
|
2022-03-10 12:29:41 +01:00
|
|
|
loglevel = LOGLEVEL_WARNING;
|
|
|
|
break;
|
2022-02-15 04:33:19 +01:00
|
|
|
case LOG_PREFIX_ERROR:
|
|
|
|
case LOGLEVEL_ERROR:
|
2022-03-10 12:29:41 +01:00
|
|
|
loglevel = LOGLEVEL_ERROR;
|
|
|
|
break;
|
2022-02-15 04:33:19 +01:00
|
|
|
default:
|
2022-03-10 12:29:41 +01:00
|
|
|
loglevel = LOGLEVEL_INFO;
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-02-16 02:08:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 12:29:41 +01:00
|
|
|
// set the timestamp format
|
|
|
|
function setTimestamp(value) {
|
|
|
|
timestamp = value || 'DD.MM.YYYY HH:mm:ss:SS';
|
2022-02-16 02:08:21 +01:00
|
|
|
}
|
2022-02-15 04:33:19 +01:00
|
|
|
|
|
|
|
// prefix log with 'info'
|
|
|
|
function info(message) {
|
|
|
|
if (loglevel > LOGLEVEL_INFO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefix log with 'info'
|
|
|
|
function warn(message) {
|
|
|
|
if (loglevel > LOGLEVEL_WARNING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace(message, 'warning');
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefix log with 'debug'
|
|
|
|
function debug(message) {
|
|
|
|
if (loglevel > LOGLEVEL_DEBUG) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace(message, 'debug');
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefix log with 'error'
|
|
|
|
function error(message) {
|
|
|
|
if (loglevel > LOGLEVEL_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-10 12:29:41 +01:00
|
|
|
if (message.stack) {
|
|
|
|
trace(message.stack, 'error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message.errors !== undefined) {
|
|
|
|
for (let index = 0; index < message.errors.length; index++) {
|
2022-02-16 02:08:21 +01:00
|
|
|
trace(message.errors[index], 'error');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-03-10 12:29:41 +01:00
|
|
|
if (message.message) {
|
|
|
|
trace(message.message, 'error');
|
|
|
|
return;
|
|
|
|
}
|
2022-02-15 04:33:19 +01:00
|
|
|
trace(message, 'error');
|
|
|
|
}
|
|
|
|
|
|
|
|
// default logging function
|
|
|
|
function trace(message, prefix) {
|
|
|
|
if (message === undefined || message === null || message.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (prefix === undefined || prefix === null || prefix.length === 0) {
|
|
|
|
prefix = 'info';
|
|
|
|
}
|
|
|
|
let print;
|
|
|
|
switch (prefix) {
|
|
|
|
case 'error':
|
|
|
|
print = console.error;
|
|
|
|
break;
|
|
|
|
case 'debug':
|
|
|
|
print = console.debug;
|
|
|
|
break;
|
|
|
|
case 'warning':
|
|
|
|
print = console.warn;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print = console.log;
|
|
|
|
}
|
2022-02-16 02:08:21 +01:00
|
|
|
message = moment().format(timestamp) + ' | ' + prefix + ' > ' + message;
|
2022-02-15 04:33:19 +01:00
|
|
|
print(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
2022-02-20 21:38:27 +01:00
|
|
|
initialize,
|
2022-02-15 04:33:19 +01:00
|
|
|
info,
|
|
|
|
warn,
|
|
|
|
debug,
|
|
|
|
error
|
|
|
|
};
|