Skip to content

Instantly share code, notes, and snippets.

@rkdgusrnrlrl
Created May 29, 2018 05:02
Show Gist options
  • Save rkdgusrnrlrl/450a58dd8fd8889814d9ab1d1c07fe3a to your computer and use it in GitHub Desktop.
Save rkdgusrnrlrl/450a58dd8fd8889814d9ab1d1c07fe3a to your computer and use it in GitHub Desktop.
winston 으로 만든 logger 파일
const winston = require('winston');
const config = winston.config;
function getLocalDateTimeString(date) {
if (date) {
const todatStr = date.toLocaleDateString('ko-KR', { year: "numeric", month: "2-digit", day: "2-digit", hour : "numeric", minute : "numeric", second : "numeric" });
const tempArr = todatStr.split(", ");
const dateArr = tempArr[0].split("/");
return[dateArr[2], dateArr[0], dateArr[1]].join("-")+" "+tempArr[1];
}
return "";
}
const logger = new (winston.Logger)({
transports: [
//콘솔 로그
new (winston.transports.Console)({
level : "info",
timestamp: function() {
return getLocalDateTimeString(new Date());
},
formatter: function(options) {
// - Return string will be passed to logger.
// - Optionally, use options.colorize(options.level, <string>) to
// colorize output based on the log level.
return options.timestamp() + ' ' +
config.colorize(options.level, options.level.toUpperCase()) + ' ' +
(options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
}),
// 파일 로그
new (winston.transports.File)({
level : "info",
filename: 'file_download_info.log',
timestamp: function() {
return getLocalDateTimeString(new Date());
},
formatter: function(options) {
return options.timestamp() + ' ' +
config.colorize(options.level, options.level.toUpperCase()) + ' ' +
(options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
})
]
});
module.exports = logger;
const logger = require('./logger');
logger.info("hello");
logger.info({name : "rkd", age : 32});
logger.error(new Error('this is error log'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment