Reputation: 1590
I want to log some messages and want them having different colors.
The messagetype got a specific color
Behind that I want to log the datetime, this has always the color cyan.
And after that, my log message is always white.
So I created this simple logger script
const consoleColorWhite = '\x1b[37m%s\x1b[0m';
const consoleColorGreen = '\x1b[32m%s\x1b[0m';
const consoleColorRed = '\x1b[31m%s\x1b[0m';
const consoleColorYellow = '\x1b[33m%s\x1b[0m';
const consoleColorCyan = '\x1b[36m%s\x1b[0m';
exports.log = function(type, msg){
var msgType;
var msgTypeColor;
switch (type) {
case 'inf':
msgType = 'INF';
msgTypeColor = consoleColorGreen;
break;
case 'err':
msgType = 'ERR';
msgTypeColor = consoleColorRed;
break;
case 'wrn':
msgType = 'WRN';
msgTypeColor = consoleColorYellow;
break;
default:
msgType = '';
msgTypeColor = consoleColorWhite;
}
if(type !== undefined && type !== null && msgType.length > 0){
msgType = '[' + msgType + ']';
}
var dateTime = new Date();
var date = dateTime.toLocaleDateString();
var time = dateTime.toLocaleTimeString();
var dateTimeString = '[' + date + ' ' + time + ']';
console.log(msgTypeColor, msgType);
console.log(consoleColorCyan, dateTimeString);
console.log(consoleColorWhite, msg);
}
and it works really fine but the console will log this structure
How can I put all the messages into a single row?
I can go for
string output = msgType + dateTimeString + msg;
console.log(output);
but I want to have different colors within the row.
Upvotes: 1
Views: 500
Reputation: 7556
You can also use the ansicolor package:
const { green, red, yellow, white, cyan } = require("ansicolor");
require("ansicolor").nice; // .nice for unsafe String extensions
log = function(type, msg) {
var msgType;
var msgTypeColor;
switch (type) {
case "inf":
msgType = "INF";
msgTypeColor = green;
break;
case "err":
msgType = "ERR";
msgTypeColor = red;
break;
case "wrn":
msgType = "WRN";
msgTypeColor = yellow;
break;
default:
msgType = "";
msgTypeColor = white;
}
if (type !== undefined && type !== null && msgType.length > 0) {
msgType = "[" + msgType + "]";
}
var dateTime = new Date();
var date = dateTime.toLocaleDateString();
var time = dateTime.toLocaleTimeString();
var dateTimeString = "[" + date + " " + time + "]";
console.log(
(msgTypeColor || (s => s))(msgType),
dateTimeString.cyan,
msg.white
);
};
log("inf", "This is info");
log("wrn", "This is wrn");
log("err", "This is err");
Upvotes: 0
Reputation: 18939
You can, for example, do it like this:
const consoleColorOff = '\x1b[0m';
const consoleColorWhite = '\x1b[37m';
const consoleColorGreen = '\x1b[32m';
const consoleColorRed = '\x1b[31m';
const consoleColorYellow = '\x1b[33m';
const consoleColorCyan = '\x1b[36m';
function color(color, msg) {
return `${color}${msg}${consoleColorOff} `
}
exports.log = function (type, msg) {
var msgType;
var msgTypeColor;
switch (type) {
case 'inf':
msgType = 'INF';
msgTypeColor = consoleColorGreen;
break;
case 'err':
msgType = 'ERR';
msgTypeColor = consoleColorRed;
break;
case 'wrn':
msgType = 'WRN';
msgTypeColor = consoleColorYellow;
break;
default:
msgType = '';
msgTypeColor = consoleColorWhite;
}
if (type !== undefined && type !== null && msgType.length > 0) {
msgType = '[' + msgType + ']';
}
var dateTime = new Date();
var date = dateTime.toLocaleDateString();
var time = dateTime.toLocaleTimeString();
var dateTimeString = '[' + date + ' ' + time + ']';
console.log(
color(msgTypeColor, msgType),
color(consoleColorCyan, dateTimeString),
color(consoleColorWhite, msg));
}
The off escape code is factored out and now a separate value. A new function called color()
is made that returns a string with the message colored. The output will now look like this:
I can also recommend chalk if you don't want to deal with this yourself.
Upvotes: 2