JS/NODE.JS
[node] Node Log process(로그 처리) [winston]
밍글링글링
2018. 10. 10. 17:34
728x90
노드에서 로그 처리를 하기 위해 'winston' 이라는 외장 모듈을 사용할 것입니다.
해당 모듈을 사용하기 위해서는 우선 아래와 같은 명령어를 입력하여 줍니다.
npm install --save winston
단계별로 로그를 찍어낼 수 있습니다.
logger.debug('디버그 로깅');
logger.info('인포 로깅');
logger.error('에러 로깅');
날짜별로 파일을 관리하기 위해서는
아래와 같은 명령어를 입력하여 줍니다.
npm install --save winston-daily-rotate-file
아래는 라우터에서 설정할 소스입니다.
var winston = require('winston');
var winstonDaily = require('winston-daily-rotate-file');
var moment = require('moment');
function timeStamp(){
return moment().format('YYYY-MM-DD HH:mm:ss SSS ZZ');
}
var logger= new (winston.Logger)({
transports: [
new (winstonDaily)({
name: 'info-file',
filename: './log/server',
datePattern: '_yyyy-MM-dd.log',
colorize: false,
maxsize: 50000000,
maxFiles: 1000,
level: 'info',
showLevel: true,
json: false,
timestamp: timeStampFormat
}),
new (winston.transports.Console)({
name: 'debug-console',
colorize: true,
level: 'debug',
showLevel: true,
json: false,
timestamp: timeStampFormat
})
},
exceptionHandlers:[
new (winstonDaily)({
name: 'exception-file',
filename: './log/exception',
datePattern: '_yyyy-MM-dd.log',
colorize: false,
maxsize: 50000000,
maxFiles: 1000,
level: 'error',
showLevel: true,
json: false,
timestamp: timeStampFormat
}),
new (winston.transports.Console)({
name: 'exception-console',
colorize: true,
level: 'debug',
showLevel: true,
json: false,
timestamp:L timeStampFormat
})
]
});
728x90