Merge pull request #5109 from hardillb/colour-logs

Colourise the Node-RED logs
This commit is contained in:
Nick O'Leary
2025-04-25 17:07:09 +01:00
committed by GitHub
3 changed files with 19 additions and 6 deletions

View File

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

View File

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