blinky/libs/logger.js

149 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-02-21 00:48:29 +01:00
const moment = require('moment');
2021-04-06 16:41:49 +02:00
// constants
2022-02-21 00:48:29 +01:00
const LOG_PREFIX_DEBUG = 'debug';
const LOG_PREFIX_INFO = 'info';
const LOG_PREFIX_WARNING = 'warning';
const LOG_PREFIX_ERROR = 'error';
2021-04-06 16:41:49 +02:00
const LOGLEVEL_DEBUG = 0;
const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3;
2022-02-21 11:56:49 +01:00
let loglevel = getLogLevel();
let timestamp = getTimestamp();
2022-02-21 00:48:29 +01:00
function initialize() {
if (global.config === undefined) {
throw new Error('could not initialize logger, config is undefined');
}
loglevel = getLogLevel();
timestamp = getTimestamp();
2022-02-21 00:48:29 +01:00
}
// get the loglevel
function getLogLevel() {
2022-02-21 11:56:49 +01:00
if (global.config === undefined || global.config.log === undefined || global.config.log.level === undefined) {
2022-02-21 00:48:29 +01:00
return LOGLEVEL_INFO;
}
switch (global.config.log.level) {
2021-04-06 16:41:49 +02:00
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-21 00:48:29 +01:00
}
// get the timestamp format
function getTimestamp() {
2022-02-21 11:56:49 +01:00
if (global.config !== undefined && global.config.log !== undefined && global.config.log.format !== undefined) {
2022-02-21 00:48:29 +01:00
return global.config.log.timestamp;
}
return 'DD.MM.YYYY HH:mm:ss:SS';
2022-02-21 00:48:29 +01:00
}
2021-04-06 16:41:49 +02:00
// log a http request
2022-02-21 00:48:29 +01:00
function http(request) {
2022-02-21 11:56:49 +01:00
if (request === undefined) {
2022-02-21 00:48:29 +01:00
return;
}
let message = '[' + request.method + '] url: \'' + request.url + '\'';
2021-04-06 16:41:49 +02:00
let counter = 1;
for (let param in request.body) {
2022-02-21 00:48:29 +01:00
message += ', parameter ' + counter + ': \'' + param + '=' + request.body[param] + '\'';
2021-04-06 16:41:49 +02:00
counter++;
}
debug(message.trim());
}
2022-02-21 00:48:29 +01:00
// prefix log with 'info'
2021-04-06 16:41:49 +02:00
function info(message) {
if (loglevel > LOGLEVEL_INFO) {
return;
}
trace(message);
}
2022-02-21 00:48:29 +01:00
// prefix log with 'info'
2021-04-06 16:41:49 +02:00
function warn(message) {
if (loglevel > LOGLEVEL_WARNING) {
return;
}
2022-02-21 00:48:29 +01:00
trace(message, 'warning');
2021-04-06 16:41:49 +02:00
}
2022-02-21 00:48:29 +01:00
// prefix log with 'debug'
2021-04-06 16:41:49 +02:00
function debug(message) {
if (loglevel > LOGLEVEL_DEBUG) {
return;
}
2022-02-21 00:48:29 +01:00
trace(message, 'debug');
2021-04-06 16:41:49 +02:00
}
2022-02-21 00:48:29 +01:00
// prefix log with 'error'
2021-04-06 16:41:49 +02:00
function error(message) {
if (loglevel > LOGLEVEL_ERROR) {
return;
}
if (message.stack) {
trace(message.stack, 'error');
return;
}
2022-02-21 11:56:49 +01:00
if (message.errors !== undefined) {
for (let index = 0; index < message.errors.length; index++) {
2022-02-21 00:48:29 +01:00
trace(message.errors[index], 'error');
}
return;
}
if (message.message) {
trace(message.message, 'error');
return;
}
trace(message, 'error');
2021-04-06 16:41:49 +02: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-21 00:48:29 +01:00
prefix = 'info';
2021-04-06 16:41:49 +02:00
}
let print;
switch (prefix) {
2022-02-21 00:48:29 +01:00
case 'error':
2021-04-06 16:41:49 +02:00
print = console.error;
break;
2022-02-21 00:48:29 +01:00
case 'debug':
2021-04-06 16:41:49 +02:00
print = console.debug;
break;
2022-02-21 00:48:29 +01:00
case 'warning':
2021-04-06 16:41:49 +02:00
print = console.warn;
break;
default:
print = console.log;
}
2022-02-21 00:48:29 +01:00
message = moment().format(timestamp) + ' | ' + prefix + ' > ' + message;
2021-04-06 16:41:49 +02:00
print(message);
}
// exports
module.exports = {
2022-02-21 00:48:29 +01:00
initialize,
2021-04-06 16:41:49 +02:00
info,
warn,
debug,
error,
2022-02-21 00:48:29 +01:00
http
2022-02-21 11:56:49 +01:00
};