Node.js 如何在node js中更改日志txt文件颜色
例如,类似这样的事情Node.js 如何在node js中更改日志txt文件颜色,node.js,logging,logfile,Node.js,Logging,Logfile,例如,类似这样的事情 console.log("text", "red"); 如何在node js中设置日志txt文件字体颜色。我已经在终端中完成了这项工作,但希望在日志txt文件中也能完成。 这是否可以像在终端中一样,在日志txt文件中为不同的消息显示不同的颜色。如何在命令行上获取颜色 在命令行上工作时,为输出着色既有趣又非常有用。要使控制台输出着色,需要使用ANSI转义码。npm上提供的模块colors.js提供了一个非常易于使用的包装器,可以轻松添
console.log("text", "red");
如何在node js中设置日志txt文件字体颜色。我已经在终端中完成了这项工作,但希望在日志txt文件中也能完成。
这是否可以像在终端中一样,在日志txt文件中为不同的消息显示不同的颜色。如何在命令行上获取颜色 在命令行上工作时,为输出着色既有趣又非常有用。要使控制台输出着色,需要使用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
你可以想象你的终端对自己说,“把这个变成绿色。不,把这个变成蓝色。不,把这个变成红色。现在不再有颜色代码了?那就是红色了。”代码按相反的顺序读取,最后一个/‘最里面的’被应用。如果您使用的库设置了自己不喜欢的默认颜色,这将非常有用-如果您自己在传递给库的字符串上设置了颜色代码,它将取代其他作者的颜色代码
最后要注意的是示例脚本的最后一行。虽然之前设置了颜色代码,但没有设置“粗体”代码,因此示例变为粗体,但没有指定不同的颜色
使用颜色而不更改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属性的完整列表供参考
文本颜色
- 黑色的
- 红色的
- 绿色的
- 黄色的
- 蓝色的
- 洋红
- 青色
- 白色的
- 灰色的
- 灰色的
- 黑色
- bgRed
- 绿皮书
- bgYellow
- 蓝色
- 洋红
- BG青色
- bgWhite
- 重置
- 大胆的
- 暗淡的
- 斜体
- 下划线
- 反向
- 隐藏的
- 删除线
- 额外费用
- 彩虹
- 斑马
- 美国
- 陷阱
- 随机的