Can Chrome\'s built-in JavaScript console display colors?
I want errors in red, warnings in orange and console.log
\'s in green. Is that possible?
Update:
I have written a JavaScript library last year for myself. It contains other features e.g verbosity for debug logs and also provides a download logs method that exports a log file. Have a look at the JS Logger library and its documentation.
I know It's a bit late to answer but as the OP asked to get custom color messages in console for different options. Everyone is passing the color style property in each console.log()
statement which confuses the reader by creating complexity in the code and also harm the overall look & feel of the code.
What I suggest is to write a function with few predetermined colors (e.g success, error, info, warning, default colors) which will be applied on the basis of the parameter passed to the function.
It improves the readability and reduces the complexity in the code. It is too easy to maintain and further extend according to your needs.
Given below is a JavaScript function that you have to write once and than use it again and again.
function colorLog(message, color) {
color = color || "black";
switch (color) {
case "success":
color = "Green";
break;
case "info":
color = "DodgerBlue";
break;
case "error":
color = "Red";
break;
case "warning":
color = "Orange";
break;
default:
color = color;
}
console.log("%c" + message, "color:" + color);
}
The default color is black and you don't have to pass any keyword as parameter in that case. In other cases, you should pass success, error, warning, or info
keywords for desired results.
Here is working JSFiddle. See output in the browser's console.