【问题标题】:split log messages into different colors in NodeJs在 NodeJs 中将日志消息拆分为不同的颜色
【发布时间】:2019-07-09 07:47:47
【问题描述】:

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

消息类型有特定的颜色

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

在我想记录日期时间之后,它始终是青色。

在那之后,我的日志消息总是白色的。

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

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);
}

它工作得很好,但是控制台会记录这个结构

如何将所有消息放在一行中?

我可以去

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

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

【问题讨论】:

  • 不相关type !== undefined是非常错误的,请使用typeof type !== 'undefined'。您正在创建一个名为undefined 的变量,并且因为它没有被初始化它值得undefined,所以您认为它有效,但这是错误的。在代码顶部添加const undefined = true;,您将遇到不需要的行为
  • 我更新了我的脚本,在脚本的顶部,我保存了我的颜色值

标签: node.js logging


【解决方案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));
}

off 转义码被分解出来,现在是一个单独的值。创建了一个名为color() 的新函数,它返回一个带有彩色消息的字符串。输出现在将如下所示:

如果你不想自己处理这个问题,我也可以推荐chalk

【讨论】:

    【解决方案2】:

    你也可以使用 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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-06
      • 2022-08-18
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多