1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import { createLogger, format, transports } from 'winston'; import { TransformableInfo } from 'logform';
const consoleFormat = format.printf((info: TransformableInfo) => { return `[${info.timestamp}] [${info.level}] ${info.message}`; });
const fileFormat = format.printf((info: TransformableInfo) => { const payload = info.payload; return `[${info.timestamp}] [${info.level}] ${info.message} ${payload ? JSON.stringify(payload) : ''}`; });
const consoleTransport = new transports.Console({ level: 'info', format: format.combine( format.colorize(), consoleFormat, ), })
const dailyRotateFileTransport = new transports.DailyRotateFile({ level: 'debug', dirname: './log', filename: '%DATE%.log', datePattern: 'YYYY-MM-DD', maxSize: '10m', })
export const logger = createLogger({ format: format.combine( format.timestamp({ format: () => moment().tz('Asia/Seoul').format('YYYY-MM-DD HH:mm:ss'), }), fileFormat, transports: [consoleTransport, dailyRotateFileTransport], ) })
export function logRequest(req: IncommingMessage, res: ServerResponse) { let ip = req.connection.remoteAddress; const method = req.method; const url = req.url; req['_startAt'] = process.hrtime();
res.on('finish', () => { const [sec, ns] = process.hrtime(req['_startAt']); const responseTime = Math.floor(sec * 1e3 + ns * 1e-6); const status = res.statusCode; }) }
|