简体   繁体   中英

How to add/modify color to logger using log4js?

How to customize the color of a logger using log4js library (javascript)?

const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = 'debug';

logger.debug(`print this in red.`);
logger.debug(`print this in purple.`);

Just make use of term escaped sequences.

const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = 'debug';

logger.debug(`print this in \u001b[31mred.`);
logger.debug(`print this in \u001b[35mpurple.`);

According to

man terminal-colors.d

they are the following:

               0   to restore default color
               1   for brighter colors
               4   for underlined text
               5   for flashing text
              30   for black foreground
              31   for red foreground
              32   for green foreground
              33   for yellow (or brown) foreground
              34   for blue foreground
              35   for purple foreground
              36   for cyan foreground
              37   for white (or gray) foreground
              40   for black background
              41   for red background
              42   for green background
              43   for yellow (or brown) background
              44   for blue background
              45   for purple background
              46   for cyan background
              47   for white (or gray) background

Different codes must be separated by semi-colon ; , eg

logger.debug(`print this in \u001b[4;31mred\u001b[0m.`);

would print red underlined and red coloured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM