Colourise the Node-RED logs

This commit is contained in:
Ben Hardill
2025-04-15 17:52:34 +01:00
parent 23c44db30d
commit ead0385edf
3 changed files with 16 additions and 5 deletions

View File

@@ -33,6 +33,7 @@
"basic-auth": "2.0.1",
"bcryptjs": "2.4.3",
"body-parser": "1.20.3",
"chalk": "^4.1.2",
"cheerio": "1.0.0-rc.10",
"clone": "2.1.2",
"content-type": "1.0.5",

View File

@@ -23,6 +23,7 @@ var util = require("util");
var EventEmitter = require("events").EventEmitter;
var i18n = require("./i18n");
const chalk = require("chalk");
var levels = {
off: 1,
@@ -47,6 +48,15 @@ var levelNames = {
99: "metric"
};
var levelColours = {
10: 'red',
20: 'red',
30: 'yellow',
40: 'white',
50: 'cyan',
60: 'gray'
};
var logHandlers = [];
var verbose;
@@ -75,20 +85,19 @@ LogHandler.prototype.shouldReportMessage = function(msglevel) {
msglevel <= this.logLevel;
}
// Older versions of Node-RED used the deprecated util.log function.
// With Node.js 22, use of that function causes warnings. So here we
// are replicating the same format output to ensure we don't break any
// log parsing that happens in the real world.
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const utilLog = function (msg) {
const utilLog = function (msg, level) {
const d = new Date();
const time = [
d.getHours().toString().padStart(2, '0'),
d.getMinutes().toString().padStart(2, '0'),
d.getSeconds().toString().padStart(2, '0')
].join(':');
console.log(`${d.getDate()} ${months[d.getMonth()]} ${time} - ${msg}`)
console.log(chalk[levelColours[level]](`${d.getDate()} ${months[d.getMonth()]} ${time} - ${msg}`))
}
var consoleLogger = function(msg) {
@@ -96,7 +105,7 @@ var consoleLogger = function(msg) {
utilLog("["+levelNames[msg.level]+"] "+JSON.stringify(msg));
} else {
if (verbose && msg.msg && msg.msg.stack) {
utilLog("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg.stack);
utilLog("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg.stack, msg.level);
} else {
var message = msg.msg;
try {
@@ -107,7 +116,7 @@ var consoleLogger = function(msg) {
message = 'Exception trying to log: '+util.inspect(message);
}
utilLog("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+message);
utilLog("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+message, msg.level);
}
}
}

View File

@@ -15,6 +15,7 @@
}
],
"dependencies": {
"chalk": "4.1.2",
"fs-extra": "11.2.0",
"i18next": "21.10.0",
"json-stringify-safe": "5.0.1",