'2017/02'에 해당되는 글 2건

  1. 2017.02.03 [Node.js][Winston] Log File Managment
  2. 2017.02.03 [Node.js][Winston] Custom Log Format

winston에서는 로그 파일 관리를 위해 다음과 같은 설정을 제공한다.

  • File Transport Options
    • maxsize: Max size in bytes of the logfile, if the size is exceeded then a new file is created, a counter will become a suffix of the log file.
      • 로그파일의 최대 크기를 지정가능하고, 해당 크기를 넘을 경우 새로운 로그파일을 생성
        • ex) error.log, error1.log, error2.log
    • maxFiles: Limit the number of files created when the size of the logfile is exceeded.
      • 생성되는 로그 파일의 갯수를 지정가능하고, maxFiles 갯수를 넘어갈경우 가장 오래된 파일을 삭제하고 새로 생성
    • zippedArchive: If true, all log files but the current one will be zipped.
      • 로그파일을 zip 파일로 유지
  • 예제
    • 로그파일을 최대 100MB x 5 개로 유지
    • var fileAppender = new winston.transports.File({
          timestamp: _timestamp,
          formatter: _customFormatter,
          json: false,
          filename: 'filelog.log',
          maxsize: 104857600, // 100 MB = 1024 * 1024 * 100 = 104857600 Bytes
          maxFiles: 5
      });
      
  • winston-daily-rotate-file
    • Daily로 로그를 생성하고 싶을 경우 winston-daily-rotate-file 모듈을 사용하면 된다.


Posted by leechwin
,

Winston의 Log Format을 Customizing 하는 방법에 대해 알아본다.


Winston Log Format을 수정하기 위해서는 Winston Transport의 formatter 설정을 구현해야 한다.

  • timestamp 및 output format 함수를 재구현
function _timestamp() {
        return moment().format("YYYY-MM-DD HH:mm:ss.SSS");
}
function _customFormatter(options) {
    return options.timestamp() +
            ' ['+ options.level.toUpperCase() + ']' +
            '['+ options.meta.loggerName + '] ' +
            (!options.meta.clientMessage ? options.message : options.meta.clientMessage);
}
var consoleAppender = new winston.transports.Console({
    timestamp: _timestamp,
    formatter: _customFormatter
});
var fileAppender = new winston.transports.File({
    timestamp: _timestamp,
    formatter: _customFormatter,
    json: false,
    filename: 'filelog.log'
});


Posted by leechwin
,