[Utils] Create a logger helper

function getTimeNow() {
  const now = new Date().toLocaleTimeString(
    'en-US',
    { hour12: false }
  );

  return now;
}

function info(functionName: string, ...params: [string]) {
  if (process.env.NODE_ENV === 'test') {
    return;
  }

  console.info(`\x1b[36m[INFO: ${functionName}]\x1b[0m`, getTimeNow(), ...params);
}

function error(functionName: string, ...params: [string]) {
  if (process.env.NODE_ENV === 'test') {
    return;
  }
  console.error(`\x1b[31m[ERROR: ${functionName}]\x1b[0m`, getTimeNow(), ...params);
}

 

Here is an example of using ANSI escape codes in JavaScript to display text in different colors in a terminal:

console.log("\x1b[31m%s\x1b[0m", "This text is red");
console.log("\x1b[32m%s\x1b[0m", "This text is green");
console.log("\x1b[33m%s\x1b[0m", "This text is yellow");

In this example, the console.log function is used to display text in different colors in the terminal by using ANSI escape codes. The escape code "\x1b[31m" sets the text color to red, "\x1b[32m" sets the text color to green, and "\x1b[33m" sets the text color to yellow. The "\x1b[0m" code at the end of each line resets the text attributes back to the default values.

posted @ 2023-02-05 01:03  Zhentiw  阅读(16)  评论(0编辑  收藏  举报