Chrome内置的JavaScript控制台能够显示颜色吗?
我希望错误消息以红色显示,警告消息以橙色显示,而console.log
中的消息以绿色显示。这可能吗?
Chrome内置的JavaScript控制台能够显示颜色吗?
我希望错误消息以红色显示,警告消息以橙色显示,而console.log
中的消息以绿色显示。这可能吗?
只需要在您的消息前面添加%c符号和样式,然后跟随您的消息即可。
console.log('%c Hello World','color:red;border:1px solid dodgerblue');
如果您正在使用Node并希望在终端中着色控制台,则可以使用转义序列,例如
console.log('\x1b[33m%s\x1b[0m', 'hi!')
如果你想将控制台颜色设置为黄色,可以使用chalk等库来实现。
const chalk = require('chalk')
console.log(chalk.yellow('hi!'))
CSS格式说明符允许您自定义控制台中的显示。以该说明符开头,将要应用的样式作为第二个参数给出。
一个例子:
console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");
我发现您可以使用ANSI颜色代码来制作彩色日志,这使得在调试中更容易找到特定的消息。试一试:
console.log( "\u001b[1;31m Red message" );
console.log( "\u001b[1;32m Green message" );
console.log( "\u001b[1;33m Yellow message" );
console.log( "\u001b[1;34m Blue message" );
console.log( "\u001b[1;35m Purple message" );
console.log( "\u001b[1;36m Cyan message" );
"\x1b[0m"
将结束着色,如下面的帖子所示:https://dev59.com/-Gs05IYBdhLWcg3wAdST#60602339例如,可以按以下方式打印绿色消息:console.log("\u001b[1;32m绿色消息\x1b[0m");
- Sascha Klatt看看这个:
控制台中的动画,加上CSS
(function() {
var frame = 0;
var frames = [
"This",
"is",
"SPARTA!",
" ",
"SPARTA!",
" ",
"SPARTA!",
" ",
"SPARTA!",
" ",
"SPARTA!",
" ",
"SPARTA!"
];
var showNext = () => {
console.clear();
console.log(
`%c `,
"background: red; color: white; font-size: 15px; padding: 3px 41%;"
);
console.log(
`%c ${frames[frame]}`,
"background: red; color: white; font-size: 25px; padding: 3px 40%;"
);
console.log(
`%c `,
"background: red; color: white; font-size: 15px; padding: 3px 41%;"
);
setTimeout(
showNext,
frames[frame] === "SPARTA!" || frames[frame] === " " ? 100 : 1500
);
// next frame and loop
frame++;
if (frame >= frames.length) {
frame = 0;
}
};
showNext();
})();
https://jsfiddle.net/a8y3jhfL/
您可以在每个帧中粘贴ASCII以观看ASCII动画。
我认为不会有人真正看到这个,但是我有一个简单的解决方案,适用于那些想在同一行中混合多种颜色的人:
export enum Colors {
Black = '\033[30m',
Red = '\x1b[31m',
Green = '\x1b[32m',
Yellow = '\x1b[33m',
Blue = '\033[34m',
Magenta = '\033[35m',
Cyan = '\033[36m',
White = '\033[37m'
}
function color(text: string, color: color: Colors) {
return `${color}${text}\x1b[0m`;
}
console.log(`This is ${color('green text', Colors.Green)} but this is black. This is red ${color('red', Colors.Red)} etc`);
/*
* Simple example
*/
console.log(
`%c[line 42] | fooBar.js myFunc() -> YOU DEBUG MESSAGE HERE `,
` color:white; background-color:black; border-left: 1px solid yellow; padding: 4px;`
);
/*
* Complex example
*/
const path = `../this/goes/to/my/dir`;
const line = 42;
const ref = `myFunc()`;
const message = `A FANCY DEBUG MESSAGE `;
const styling = `
color:white;
background-color:black;
border-left: 1px solid yellow;
padding: 8px;
font-weight: 600;
font-family: Courier;
`;
console.log(
`%c ✨ F A N C Y - L O G G E R ✨`,
`${styling} font-size: 16px; border-top: 1px solid yellow;`
);
console.log(
`%c Path: ${path} `,
`${styling} font-size: 10px; font-weight: 100;`
);
console.log(
`%c Line: ${line}`,
`${styling} font-size: 10px; font-weight: 100;`
);
console.log(
`%c Ref: ${ref} `,
`${styling} font-size: 10px; font-weight: 100;`
);
console.log(
`%c Message: ${message} `,
`${styling} font-size: 12px; border-bottom: 1px solid yellow;`
);
var styles = [
'background: linear-gradient(#D33106, #571402)'
, 'border: 1px solid #3E0E02'
, 'color: white'
, 'display: block'
, 'text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)'
, 'box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset'
, 'line-height: 40px'
, 'text-align: center'
, 'font-weight: bold'
].join(';');
console.log('%c a spicy log message ?', styles);
结果
查看更多:https://coderwall.com/p/fskzdw/colorful-console-log
干杯。
从Chrome 60开始,他们在编写console.info时删除了蓝色文本颜色的功能,并对控制台API进行了许多更改。
如果您在es6模式下使用反引号``作为标识符(称为模板字符串)在console.log()中编写字符串文字,则可以按以下方式着色控制台输出。
console.log(`%cToday date=>%c${new Date()}`,`color:#F74C2F`, `color:green`);
// output :Today date (in red color) => Date (in green color)
console.error()
代替console.log
,即可在红色(默认)中获得错误提示。请注意,此操作仅为翻译,不包含任何解释或其他信息。 - nrabinowitzconsole.warn()
也可以使用橙色的“警告”图标,但文本本身仍然是黑色。 - Charlie Schliesserconsole.log("%c", "background: red;padding: 100000px;");
会导致非常奇怪的行为。 - user6560716