因为眼睛问题,我不得不将控制台背景颜色更改为白色,但字体是灰色的,这使得消息无法阅读。我该如何更改它?
因为眼睛问题,我不得不将控制台背景颜色更改为白色,但字体是灰色的,这使得消息无法阅读。我该如何更改它?
下面是在运行Node.js应用程序时可以使用的文本颜色参考:
console.log('\x1b[36m%s\x1b[0m', 'I am cyan'); //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); //yellow
注意,%s
是字符串中被注入的位置(第二个参数)。\x1b[0m
重置终端颜色,因此在此点之后它不再是所选择的颜色。
颜色参考
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"
FgGray = "\x1b[90m"
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
BgGray = "\x1b[100m"
编辑:
例如,\x1b[31m
是一种转义序列,会被你的终端截获并指示它切换到红色。实际上,\x1b
是不可打印控制字符escape
的代码。仅处理颜色和样式的转义序列也称为ANSI 转义代码,并且已经标准化,因此它们(应该)可以在任何平台上使用。
Wikipedia有一个很好的比较表格,展示了不同终端如何显示颜色 https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
在Node.js中,有多个可用于格式化控制台文本的包。最流行的包括:
chalk
—
— > EDIT: colors
colors
现在不再推荐使用,因为它存在拒绝服务漏洞
详情请参见:https://snyk.io/blog/open-source-npm-packages-colors-faker/
CHALK:
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'));
var colors = require('colors/safe');
来使用颜色,然后使用colors.red('left string all alone')
来使字符串变成红色。 - Laoujinrequire
导入 chalk
时,会出现以下错误:Error [ERR_REQUIRE_ESM]: require() of ES Module [omitted]\node_modules\chalk\source\index.js from [omitted].js not supported.
- Jesus is Lordnpm i chalk@4.1.2
。 - VibingCreator如果你想直接更改颜色而不使用模块,请尝试
console.log('\x1b[36m', 'sometext' ,'\x1b[0m');
首先将颜色更改为36
,然后返回终端颜色0
。这里是ANSI颜色代码的列表。
const colours = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
fg: {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
gray: "\x1b[90m",
crimson: "\x1b[38m" // Scarlet
},
bg: {
black: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m",
gray: "\x1b[100m",
crimson: "\x1b[48m"
}
};
console.log(colours.bg.blue, colours.fg.white, "I am a white message with a blue background", colours.reset) ;
// Make sure that you don't forget "colours.reset" at the so that you can reset the console back to it's original colours.
或者您可以安装一些实用模块:
npm install console-info console-warn console-error --save-dev
当您使用这些模块时,控制台将显示类似以下内容:
+
代替 ,
来连接颜色,以避免空格。 - orta要为输出上色,您可以参考以下示例:
https://help.ubuntu.com/community/CustomizingBashPrompt
这里还有一个针对Node.js的Gist
举个例子,如果您想将文本中的某个部分变成红色,请使用console.log:
"\033[31m this will be red \033[91m and this will be normal"
基于此,我创建了 Node.js 的 "colog" 扩展。您可以使用以下命令进行安装:
npm install colog
\033[31m
可以使用,但 \033[91m
无法使用。对于 Ubuntu 终端应该使用 \033[0m
。 - Redsandro错误:不允许使用八进制转义序列"\033[31m服务器已准备就绪 @ #{app.get('port')}\033[91m"
- jcollum\033[0m
来将文本恢复为正常状态,而不是使用\033[91m
。 - mollerhojSyntaxError:在严格模式下不允许使用八进制文字。
“问题是由ANSI转义代码引起的,它是一个字符串,而不是以0开头的数字(八进制文字),例如0644。在我的情况下,该字符串为'\033[0m'。解决方案是将其替换为'\u001b[0m'”-https://github.com/TypeStrong/ts-node/issues/90#issue-144783379 - craigmichaelmartinReset: "\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"
FgGray: "\x1b[90m"
BgBlack: "\x1b[40m"
BgRed: "\x1b[41m"
BgGreen: "\x1b[42m"
BgYellow: "\x1b[43m"
BgBlue: "\x1b[44m"
BgMagenta: "\x1b[45m"
BgCyan: "\x1b[46m"
BgWhite: "\x1b[47m"
FgGray: "\x1b[100m"
例如,如果您想要一个红色的文字和蓝色的背景,可以使用JavaScript实现如下:console.log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");
颜色和效果的顺序似乎并不那么重要,但请始终记得在最后重置颜色和效果。
FgGray
重复了 - undefined如其他回答中所提到的那样,您可以使用颜色来渲染文字。
但是,您也可以使用 表情符号 代替!例如,您可以使用⚠️
表示警告消息,表示错误消息。
或者只需将这些表情符号用作颜色笔记本:
: error message
: warning message
: ok status message
: action message
: canceled status message
: Or anything you like and want to recognize immediately by color
此方法还可以帮助您快速扫描并在源代码中直接找到日志。
for example:
console.log('Bring with ❤️ to you from Mojtaba Hosseini');
一些 Linux 发行版默认的表情符号字体可能不是彩色的,您可能需要先将其变为彩色。
macOS:按下 control + command + space
Windows:按下 win + .
Linux:按下 control + . 或 control + ;
[Win] + [.]
打开特殊的表情符号窗口 :) - Damian Drygiel// you'll need the util module
var util = require('util');
// let's look at the defaults:
util.inspect.styles
{ special: 'cyan',
number: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
date: 'magenta',
regexp: 'red' }
// what are the predefined colors?
util.inspect.colors
{ bold: [ 1, 22 ],
italic: [ 3, 23 ],
underline: [ 4, 24 ],
inverse: [ 7, 27 ],
white: [ 37, 39 ],
grey: [ 90, 39 ],
black: [ 30, 39 ],
blue: [ 34, 39 ],
cyan: [ 36, 39 ],
green: [ 32, 39 ],
magenta: [ 35, 39 ],
red: [ 31, 39 ],
yellow: [ 33, 39 ] }
这些似乎是ANSI SGR转义代码,其中第一个数字是输出前要发出的代码,第二个数字是输出后要发出的代码。因此,如果我们查看维基百科上的ANSI SGR代码图表,你会发现大多数都以30-37开头,用于设置前景色,并以39结尾,用于重置默认前景色。
所以有一件事我不喜欢,就是它们中的一些颜色太暗了。特别是日期。在控制台中尝试new Date()
。深洋红色在黑色背景下真的很难读。让我们将其更改为浅洋红色。
// first define a new color
util.inspect.colors.lightmagenta = [95,39];
// now assign it to the output for date types
util.inspect.styles.date = 'lightmagenta';
new Date()
时,输出结果会更易读。// set your colors however desired
var util = require('util');
util.inspect.colors.lightmagenta = [95,39];
util.inspect.styles.date = 'lightmagenta';
// start the repl
require('repl').start({});
保存这个文件(例如,init.js
),然后运行 node.exe init.js
。它将设置颜色并启动node.js命令提示符。
(感谢this answer中的loganfsmyth提供repl的想法。)
const Color = {
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",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
BgGray: "\x1b[100m",
}
function colorString(color, string) {
return `${color}${string}${Color.Reset}`;
}
function colorLog(color, ...args) {
console.log(...args.map(
(it) => typeof it === "string" ? colorString(color, string) : it
));
}
像这样使用:
colorLog(Color.FgYellow, "Some Yellow text to console log", { someObj: true });
console.log([
colorString(Color.FgRed, "red"),
colorString(Color.FgGreen, "green"),
colorString(Color.FgBlue, "blue"),
].join(", "));
如果您想保持简单,不使用任何外部模块/学习新的API/黑掉核心console
函数:
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.`);
现在你可以像使用 console
一样使用你的 logger
。没有新的 API 需要记住... 通常你会将它放入一个模块 (logger.js) 并且导出该 class
,以便在应用程序中的任何地方使用它作为 const logger = require('./logger');