// first define a new colorutil.inspect.colors.lightmagenta = [95,39];
// now assign it to the output for date typesutil.inspect.styles.date = 'lightmagenta';
现在,当您尝试new Date()时,输出更具可读性。
如果您想在启动节点时自动设置颜色,请创建一个启动repl的脚本,如下所示:
// set your colors however desiredvar util = require('util');util.inspect.colors.lightmagenta = [95,39];util.inspect.styles.date = 'lightmagenta';
// start the replrequire('repl').start({});
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.
var StyleMe = require('styleme');StyleMe.extend() // extend the string prototype
console.log("gre{Hello} blu{world}!".styleMe()) // Logs hello world! with 'hello' being green, and 'world' being blue with '!' being normal.
它也可以嵌套:
console.log("This is normal red{this is red blu{this is blue} back to red}".styleMe())
或者,如果您不想扩展字符串原型,您可以选择其他3个选项中的任何一个:
console.log(styleme.red("a string"))console.log("Hello, this is yellow text".yellow().end())console.log(styleme.style("some text","red,bbl"))
var colorSet = {Reset: "\x1b[0m",Red: "\x1b[31m",Green: "\x1b[32m",Yellow: "\x1b[33m",Blue: "\x1b[34m",Magenta: "\x1b[35m"};
var funcNames = ["info", "log", "warn", "error"];var colors = [colorSet.Green, colorSet.Blue, colorSet.Yellow, colorSet.Red];
for (var i = 0; i < funcNames.length; i++) {let funcName = funcNames[i];let color = colors[i];let oldFunc = console[funcName];console[funcName] = function () {var args = Array.prototype.slice.call(arguments);if (args.length) {args = [color + args[0]].concat(args.slice(1), colorSet.Reset);}oldFunc.apply(null, args);};}
// Test:console.info("Info is green.");console.log("Log is blue.");console.warn("Warn is orange.");console.error("Error is red.");console.info("--------------------");console.info("Formatting works as well. The number = %d", 123);
// This is a object for use ANSI escape to color the text in the terminalconst bColors = {HEADER : '\033[95m',OKBLUE : '\033[94m',OKGREEN : '\033[92m',WARNING : '\033[93m',FAIL : '\033[91m',ENDC : '\033[0m',BOLD : '\033[1m',UNDERLINE : '\033[4m'}
要使用这样的代码,您可以执行以下操作
console.log(`${bColors.WARNING} My name is sami ${bColors.ENDC}`)
📕: error message📙: warning message📗: ok status message📘: action message📓: canceled status message📔: Or anything you like and want to recognize immediately by color
🎁奖金:
此方法还可以帮助您快速扫描和查找日志直接在源代码中。
例如:
console.log('Bring with ❤️ to you from Mojtaba Hosseini');
Some Linux distributions default emoji font may not be colorful by default and you may want to make them colorful, first.