首页 > 解决方案 > 如何在节点 js 中更改日志 txt 文件的颜色

问题描述

例如,像这样的东西。

console.log("text", "red");

如何在节点 js 中设置日志 txt 文件字体颜色。我已经在终端中完成了此操作,但希望在日志 txt 文件中相同。这是否可以像在终端中一样在日志 txt 文件中为不同的消息显示不同的颜色。

标签: node.jslogginglogfile

解决方案


如何在命令行上获取颜色

在命令行上工作时,为输出着色既有趣又非常有用。要为控制台输出着色,您需要使用 ANSI 转义码。在 npm 上可用的模块 colors.js 提供了一个非常易于使用的包装器,使添加颜色变得轻而易举。

首先,将其安装到您想要工作的目录中。

npm install colors

现在为自己打开一个小测试脚本,并尝试这样的事情:

const colors = require('colors');

const stringOne = 'This is a plain string.';
const stringTwo = 'This string is red.'.red;
const stringThree = 'This string is blue.'.blue;
const today = new Date().toLocaleDateString(); // returns today's date in mm/dd/yyyy format

console.log(stringOne.black.bgMagenta);
console.log(stringOne.yellow.bgRed.bold);
console.log(`Today is: ${today}`.black.bgGreen);

console.log(stringTwo);
console.log(stringThree);

console.log(stringTwo.magenta);
console.log(stringThree.grey.bold);

这里有几件事情需要注意——首先,字符串对象已经被原型化,所以任何颜色都可以通过简单地添加属性到字符串来添加!它适用于字符串文字、模板文字和变量,如上面示例的顶部所示。

另请注意,从第二对 console.log 语句中,一旦设置,颜色值将作为字符串的一部分持续存在。这是因为在幕后,适当的 ANSI 颜色标签已根据需要预先添加和附加 - 任何通过字符串的地方也支持 ANSI 颜色代码,颜色将保留。

最后一对 console.log 语句可能是最重要的。由于 colors.js 和 ANSI 颜色代码的工作方式,如果在字符串上设置了多个颜色属性,则只有在字符串上设置的第一个颜色属性才会生效。这是因为颜色的功能是“状态转换”而不是标签。

让我们看一个更明确的例子。如果您使用 colors.js 设置以下属性:

myString.red.blue.green

你可以想象你的终端对自己说,“把这个变成绿色。不,把这个变成蓝色。不,把这个变成红色。现在不再有颜色代码了?那就是红色。” 以相反的顺序读取代码,并应用最后/“最里面”。如果您使用的库设置了自己不喜欢的默认颜色,这将非常有用 - 如果您自己在传递给库的字符串上设置颜色代码,它将取代其他作者的颜色代码(s)。

最后要注意的是示例脚本的最后一行。虽然之前设置了颜色代码,但没有设置“粗体”代码,因此示例被设置为粗体,但没有赋予不同的颜色。

在不改变 String.prototype 的情况下使用颜色 现在也可以使用颜色的实例。虽然这种方法稍微不那么漂亮,但对初学者很友好,如果你不想接触 String.prototype,它特别有用。这方面的一些例子是:

const colors = require('colors');

const stringOne = 'This is a plain string.';
const stringTwo = 'This string is red.';
const stringThree = 'This string is blue.';
const today = new Date().toLocaleDateString(); // returns today's date in mm/dd/yyyy format

console.log(colors.bgMagenta.black(stringOne));
console.log(colors.bold.bgRed.yellow(stringOne));
console.log(colors.bgGreen.black(`Today is: ${today}`));

console.log(colors.red(stringTwo));
console.log(colors.blue(stringThree));

console.log(colors.magenta.red(stringTwo));
console.log(colors.bold.grey.black.blue(stringThree));

与 String.prototype 方法不同,colors 实例上的链式方法从左到右执行,即最后应用最接近字符串的方法。在最后一个 console.log 中,你可以想象你的终端对自己说,“把这个变成灰色。现在,把它变成黑色。现在,把它变成蓝色。现在没有更多的着色方法了吗?那么它是蓝色的。”

使用最新版本的colors.js,您还可以在color.js中定义自定义主题,这使得我们的代码更加健壮,并且可以更好地封装数据。一个很好的用例可能是:

var colors = require('colors');

colors.setTheme({
  info: 'bgGreen',
  help: 'cyan',
  warn: 'yellow',
  success: 'bgBlue',
  error: 'red'
});

// outputs red text
console.log("this is an error".error);

// outputs text on blue background
console.log("this is a success message".success);

最后一件事:颜色在不同的终端上看起来可能完全不同——有时,粗体是粗体,有时只是不同的颜色。试试看,自己看看!

作为参考,这里是可用的 colors.js 属性的完整列表。

文字颜色

  • 黑色的
  • 红色的
  • 绿色
  • 黄色
  • 蓝色的
  • 品红
  • 青色
  • 白色的
  • 灰色的
  • 灰色的

背景颜色

  • bg黑色
  • 红色
  • 绿
  • bg黄色
  • 蓝蓝
  • bg洋红色
  • 青色
  • 黑白

风格

  • 重置
  • 大胆的
  • 暗淡
  • 斜体
  • 强调
  • 删除线
  • 附加功能
  • 彩虹
  • 斑马
  • 美国
  • 陷阱
  • 随机的

推荐阅读