在NodeJ中将日志消息拆分为不同的颜色

时间:2018-01-05 11:02:39

标签: node.js logging

我想记录一些消息并希望它们具有不同的颜色。

消息类型具有特定颜色

  • 成功将是绿色
  • 错误将为红色
  • 警告将为黄色

在我要记录日期时间后面,这总是青色。

之后,我的日志消息始终为白色。

所以我创建了这个简单的记录器脚本

const consoleColorWhite = '\x1b[37m%s\x1b[0m';
const consoleColorGreen = '\x1b[32m%s\x1b[0m';
const consoleColorRed = '\x1b[31m%s\x1b[0m';
const consoleColorYellow = '\x1b[33m%s\x1b[0m';
const consoleColorCyan = '\x1b[36m%s\x1b[0m';

exports.log = function(type, msg){
  var msgType;
  var msgTypeColor;

  switch (type) {
  case 'inf':
    msgType = 'INF';
    msgTypeColor = consoleColorGreen;
    break;
  case 'err':
    msgType = 'ERR';
    msgTypeColor = consoleColorRed;
    break;
  case 'wrn':
    msgType = 'WRN';
    msgTypeColor = consoleColorYellow;
    break;
  default:
    msgType = '';
    msgTypeColor = consoleColorWhite;
  }

  if(type !== undefined && type !== null && msgType.length > 0){
    msgType = '[' + msgType + ']';
  }

  var dateTime = new Date();
  var date = dateTime.toLocaleDateString();
  var time = dateTime.toLocaleTimeString();
  var dateTimeString = '[' + date + '  ' + time + ']';

  console.log(msgTypeColor, msgType);
  console.log(consoleColorCyan, dateTimeString);
  console.log(consoleColorWhite, msg);
}

它工作得很好,但控制台将记录此结构

enter image description here

如何将所有邮件放入一行?

我可以去

string output = msgType + dateTimeString + msg;
console.log(output);

但我希望在行中有不同的颜色。

2 个答案:

答案 0 :(得分:1)

例如,您可以这样做:

const consoleColorOff = '\x1b[0m';
const consoleColorWhite = '\x1b[37m';
const consoleColorGreen = '\x1b[32m';
const consoleColorRed = '\x1b[31m';
const consoleColorYellow = '\x1b[33m';
const consoleColorCyan = '\x1b[36m';

function color(color, msg) {
  return `${color}${msg}${consoleColorOff} `
}

exports.log = function (type, msg) {
  var msgType;
  var msgTypeColor;

  switch (type) {
    case 'inf':
      msgType = 'INF';
      msgTypeColor = consoleColorGreen;
      break;
    case 'err':
      msgType = 'ERR';
      msgTypeColor = consoleColorRed;
      break;
    case 'wrn':
      msgType = 'WRN';
      msgTypeColor = consoleColorYellow;
      break;
    default:
      msgType = '';
      msgTypeColor = consoleColorWhite;
  }

  if (type !== undefined && type !== null && msgType.length > 0) {
    msgType = '[' + msgType + ']';
  }

  var dateTime = new Date();
  var date = dateTime.toLocaleDateString();
  var time = dateTime.toLocaleTimeString();
  var dateTimeString = '[' + date + '  ' + time + ']';

  console.log(
    color(msgTypeColor, msgType),
    color(consoleColorCyan, dateTimeString),
    color(consoleColorWhite, msg));
}

关闭转义代码已被分解,现在是一个单独的值。创建了一个名为color()的新函数,它返回一个带有彩色消息的字符串。输出现在看起来像这样:

console with colors on one line

如果您不想自己处理此问题,我也可以推荐chalk

答案 1 :(得分:0)

您还可以使用ansicolor软件包:

const { green, red, yellow, white, cyan } = require("ansicolor");
require("ansicolor").nice; // .nice for unsafe String extensions

log = function(type, msg) {
  var msgType;
  var msgTypeColor;

  switch (type) {
    case "inf":
      msgType = "INF";
      msgTypeColor = green;
      break;
    case "err":
      msgType = "ERR";
      msgTypeColor = red;
      break;
    case "wrn":
      msgType = "WRN";
      msgTypeColor = yellow;
      break;
    default:
      msgType = "";
      msgTypeColor = white;
  }

  if (type !== undefined && type !== null && msgType.length > 0) {
    msgType = "[" + msgType + "]";
  }

  var dateTime = new Date();
  var date = dateTime.toLocaleDateString();
  var time = dateTime.toLocaleTimeString();
  var dateTimeString = "[" + date + "  " + time + "]";

  console.log(
    (msgTypeColor || (s => s))(msgType),
    dateTimeString.cyan,
    msg.white
  );
};

log("inf", "This is info");
log("wrn", "This is wrn");
log("err", "This is err");

enter image description here