Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change console.log message color

Is there a way to do something like:

console.log("hello world", '#FF0000') 

in Chrome/Safari or Firefox ?

like image 894
Blacksad Avatar asked Feb 17 '12 17:02

Blacksad


People also ask

How do I change the color of my node JS console?

For example if you want to have a Dim, Red text with Blue background you can do it in Javascript like this: console. log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");

How do you change the color of the console in C++?

Use SetConsoleTextAttribute() Method to Change Console Color in C++ SetConsoleTextAttribute is the Windows API method to set output text colors using different parameters. This function sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole functions.

Does console log slow down performance?

Yes, it will reduce the speed, though only negligibly. But, don't use it as it's too easy for a person to read your logs.


Video Answer


2 Answers

This works:

function colorTrace(msg, color) {     console.log("%c" + msg, "color:" + color + ";font-weight:bold;"); } colorTrace("Test Me", "red"); 
like image 65
Shane Avatar answered Oct 12 '22 08:10

Shane


Made this and its been helpful:

function log(msg, color) {     color = color || "black";     bgc = "White";     switch (color) {         case "success":  color = "Green";      bgc = "LimeGreen";       break;         case "info":     color = "DodgerBlue"; bgc = "Turquoise";       break;         case "error":    color = "Red";        bgc = "Black";           break;         case "start":    color = "OliveDrab";  bgc = "PaleGreen";       break;         case "warning":  color = "Tomato";     bgc = "Black";           break;         case "end":      color = "Orchid";     bgc = "MediumVioletRed"; break;         default: color = color;     }      if (typeof msg == "object") {         console.log(msg);     } else if (typeof color == "object") {         console.log("%c" + msg, "color: PowderBlue;font-weight:bold; background-color: RoyalBlue;");         console.log(color);     } else {         console.log("%c" + msg, "color:" + color + ";font-weight:bold; background-color: " + bgc + ";");     } } 

Use:

log("hey"); // Will be black log("Hows life?", "green"); // Will be green log("I did it", "success"); // Styled so as to show how great of a success it was! log("FAIL!!", "error"); // Red on black! var someObject = {prop1: "a", prop2: "b", prop3: "c"}; log(someObject); // prints out object log("someObject", someObject); // prints out "someObect" in blue followed by the someObject 
like image 31
Jaden Travnik Avatar answered Oct 12 '22 07:10

Jaden Travnik