Change console.log message color

后端 未结 7 705
我寻月下人不归
我寻月下人不归 2021-01-31 03:12

Is there a way to do something like:

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

in Chrome/Safari or Firefox ?

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 03:40

    ALL of currently given answers will cause major debugging issue - the line number reported in the log output will always correspond to the line where the custom log function ultimately invokes the native console.log

    coloring is achievable by adding %c on the beginning of first parameter and simple css rules as a second parameter of console.log:

    console.log('%c' + $message, 'color:' + $color + ';background-color: #444; padding: 3px 7px; margin-left: -7px;');
    

    and the proper logging wrapper you could find under this answer

    A proper wrapper for console.log with correct line number?

提交回复
热议问题