[ NodeJS ] winston 콘솔에서 색상 지정하기
💻기술/NodeJS

[ NodeJS ] winston 콘솔에서 색상 지정하기

반응형

 

winston 설치 및 설정

 

아래 명령어를 입력해서 설치해줍니다.

npm i winston

 

코드 설정

import winston from 'winston';
import dayjs from 'dayjs';
const messageSymbol = Symbol.for('message');

const colorset = {
    "INFO": "\x1b[34m[ %s ]\x1b[0m",
    "ERROR": "\x1b[31m[ %s ]\x1b[0m",
    "WARN": "\x1b[33m[ %s ]\x1b[0m"
}

export async function init () {
    winston.configure({
        level: 'info',
        format: winston.format.combine(
            winston.format(function(info, opts) {
                let prefix = util.format("[ %s ]", dayjs().format('YYYY.MM.DD HH:mm:ss A'));
                let level = util.format(colorset[info.level.toUpperCase()], info.level.toUpperCase());

                info.message = util.format('\x1b[36m%s\x1b[0m %s %s ', prefix, level, info.message);

                return info;
            })(),
            winston.format(function(info) {
                info[messageSymbol] = info.message;
                return info;
            })()
        ),
        transports: [
            new (winston.transports.Console)({
                level: 'debug',
                colorize: true,
                prettyPrint: true
            })
        ]
    });
}

 

 

콘솔 색상표

 

위에서 쓰인 색상표는 아래를 참고하면 됩니다.

 

 

 

 

 

미리 보기

 

그럼 다음과 같이 색상이 적용되는 모습을 확인할 수 있습니다.

 

 

 

 

반응형