Colors in JavaScript console

前端 未结 27 2481
说谎
说谎 2020-11-22 11:42

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?

27条回答
  •  醉梦人生
    2020-11-22 12:15

    Older versions of Chrome do not allow you to get console.log()s to show in a specific color programmatically, but calling console.error() will put a red X icon on error lines and make the text red, and console.warn() gets you a yellow ! icon.

    You can then filter console entries with the All, Errors, Warnings, and Logs buttons beneath the console.


    It turns out Firebug has supported custom CSS for console.logs since 2010 and Chrome support has been added as of Chrome 24.

    console.log('%c Oh my heavens! ', 'background: #222; color: #bada55',
                'more text');
    

    When %c appears anywhere in the first argument, the next argument is used as the CSS to style the console line. Further arguments are concatenated (as has always been the case).

提交回复
热议问题