console message JavaScript with green checkmark

2019-05-07 21:27发布

问题:

I was wondering if the was a possibility to have a green checkmark in your console just like console.warn has the yellow warning sign and console.error has the red error sign.

I have searched on the internet if there already was a function like that, but I couldn't find it. Now I'm looking for a way to put an icon before a console.log message.

Is this possible?

回答1:

Certainly is. You can use CSS in Chrome:

console.log("%c hi", "background: url(path/to/your/icon.png) 0 0 no-repeat; padding-left: 20px;");

From the documentation:

Dev Tools supports the following format specifiers:

%c Formats the output string according to CSS styles you provide.

Caveat:

The console doesn't support background-repeat for some odd reason. So you'd be better either adding some padding to your icon, or else moving the icon to the right-hand side:

console.log("hi %c", "background: url(path/to/your/icon.png) 0 0 no-repeat; padding-left: 20px;");