@nrchkb/logger
Version:
Unified Logger for Node-RED nodes (using debug)
51 lines (50 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setTimestamp = void 0;
const namespaces = [];
const dateOptions = {
day: '2-digit',
month: 'short',
};
const timeOptions = {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
};
const setTimestamp = (debug, LOGGER_TIMESTAMP_ENABLED, namespacePrefix) => {
if (LOGGER_TIMESTAMP_ENABLED) {
namespaces.push(namespacePrefix, namespacePrefix + '-Trace', namespacePrefix + '-Error');
}
debug.formatArgs = function (args) {
const { namespace, useColors } = this;
if (useColors) {
let timestamp = ' ';
const useTimestamp = namespaces.findIndex((n) => namespace.startsWith(n)) >= 0;
if (useTimestamp) {
const date = new Date();
timestamp =
date.toLocaleDateString('en-GB', dateOptions) +
' ' +
date.toLocaleTimeString('en-GB', timeOptions) +
' ';
}
const c = this.color;
const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
const prefix = `${colorCode};1m${namespace} \u001B[0m`;
args[0] =
timestamp + prefix + args[0].split('\n').join('\n' + prefix);
args.push(colorCode + 'm+' + debug.humanize(this.diff) + '\u001B[0m');
}
else {
const getDate = () => {
var _a;
if ((_a = debug.inspectOpts) === null || _a === void 0 ? void 0 : _a.hideDate) {
return '';
}
return new Date().toISOString() + ' ';
};
args[0] = getDate() + namespace + ' ' + args[0];
}
};
};
exports.setTimestamp = setTimestamp;