위의 블로그 글의 도움을 받아 코드를 수정했다.
npm i moment-timezone
appendTimestamp
로 시간대와 시간출력포맷 지정해주기
// 변경 전 코드
import * as winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
import path from "path";
const ___dirname = path.resolve();
const logDir = `${___dirname}/src/logs/`;
const format = winston.format.combine(
winston.format.timestamp({ format: " YYYY-MM-DD HH:mm:ss ||" }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level} | ${info.message}`
)
);
const transport: DailyRotateFile = new DailyRotateFile({
filename: `${logDir}/%DATE%.log`,
datePattern: "YYYY-MM-DD",
zippedArchive: true,
});
export const logger = winston.createLogger({
format,
transports: [
new winston.transports.DailyRotateFile({
level: "info",
datePattern: "YYYY-MM-DD",
dirname: logDir,
filename: `%DATE%.log`,
handleExceptions: true,
}),
new winston.transports.DailyRotateFile({
level: "error",
datePattern: "YYYY-MM-DD",
dirname: logDir + "/error",
filename: `%DATE%.error.log`,
}),
new winston.transports.Console({ handleExceptions: true }),
],
});
//변경 후 코드
...
import moment from "moment-timezone";
...
const appendTimestamp = winston.format((info, opts) => {
if (opts.tz)
info.timestamp = moment().tz(opts.tz).format(" YYYY-MM-DD HH:mm:ss ||");
return info;
});
const format = winston.format.combine(
appendTimestamp({ tz: "Asia/Seoul" }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level} | ${info.message}`
)
);