由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
对于一个不影响String对象的内置方法的流行替代颜色,我推荐使用cli-color。
包括颜色和可链接样式,如粗体、斜体和下划线。
有关此类别中各个模块的比较,请参见这里。
其他回答
node-colorify
提供功能打印文本的颜色,也做文本格式,如粗体,闪烁等。
我真的很喜欢@Daniel的回答,但是console.log{color}函数的工作方式与常规的console.log不同。我做了一些更改,现在所有参数的新函数将被传递到console.log(以及颜色代码)。
const _colors = {
Reset : "\x1b[0m",
Bright : "\x1b[1m",
Dim : "\x1b[2m",
Underscore : "\x1b[4m",
Blink : "\x1b[5m",
Reverse : "\x1b[7m",
Hidden : "\x1b[8m",
FgBlack : "\x1b[30m",
FgRed : "\x1b[31m",
FgGreen : "\x1b[32m",
FgYellow : "\x1b[33m",
FgBlue : "\x1b[34m",
FgMagenta : "\x1b[35m",
FgCyan : "\x1b[36m",
FgWhite : "\x1b[37m",
BgBlack : "\x1b[40m",
BgRed : "\x1b[41m",
BgGreen : "\x1b[42m",
BgYellow : "\x1b[43m",
BgBlue : "\x1b[44m",
BgMagenta : "\x1b[45m",
BgCyan : "\x1b[46m",
BgWhite : "\x1b[47m",
};
const enableColorLogging = function(){
Object.keys(_colors).forEach(key => {
console['log' + key] = function(){
return console.log(_colors[key], ...arguments, _colors.Reset);
}
});
}
如果你想保持简单,而不使用任何外部模块/学习新的api /破解核心控制台功能:
const LCERROR = '\x1b[31m%s\x1b[0m'; //red
const LCWARN = '\x1b[33m%s\x1b[0m'; //yellow
const LCINFO = '\x1b[36m%s\x1b[0m'; //cyan
const LCSUCCESS = '\x1b[32m%s\x1b[0m'; //green
const logger = class {
static error(message, ...optionalParams) { console.error(LCERROR, message, ...optionalParams) }
static warn(message, ...optionalParams) { console.warn(LCWARN, message, ...optionalParams) }
static info(message, ...optionalParams) { console.info(LCINFO, message, ...optionalParams) }
static success(message, ...optionalParams) { console.info(LCSUCCESS, message, ...optionalParams) }
}
// then instead (as presented in the accepted answer)
// console.error(LCERROR, 'Error message in red.');
// you write:
logger.error('Error message in red.');
// or with multiple parameters (only the message will be red):
logger.error('Error message in red.', 1, false, null, {someKey: 'whatever'});
// or use backticks (template literal) instead multiple params:
logger.error(`This will be red as ${foo} and ${bar} too.`);
现在您可以像使用控制台一样使用记录器。没有新的API需要记住……通常你会把它放到一个模块(logger.js)中,并导出类,以便在你的应用程序中随处使用它,因为const logger = require('./logger');
内联typescript解决方案
export const color = (function (colors) {
const fn = (code: number, str: string) => `\x1b[${code}m${str}\x1b[39m`;
const obj = { grey: fn.bind(null, 90) };
for (let i = 0; i < colors.length; i++) obj[colors[i]] = fn.bind(null, 30 + i);
return obj as { [K in typeof colors[any] | 'grey']: (str: string) => string };
})(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] as const);
在Node.js中有多个可用于格式化控制台文本的包。最流行的有:
粉笔, cli-color - 颜色- >编辑:颜色不再推荐,因为它有拒绝服务漏洞 详情见:https://snyk.io/blog/open-source-npm-packages-colors-faker/
用法:
粉笔:
const chalk = require('chalk');
console.log(chalk.red('Text in red'));
CLI-COLOR:
const clc = require('cli-color');
console.log(clc.red('Text in red'));
推荐文章
- 我如何改变我的提示在zsh的颜色(不同于正常的文本)?
- 如何改变循环进度指示器的颜色
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成
- Express函数中的“res”和“req”参数是什么?
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]
- Passport.js -错误:序列化用户到会话失败
- Node.js vs .Net性能
- 从电子应用程序中删除菜单栏
- 如何用node.js实现一个安全的REST API