| 15 | } |
| 16 | |
| 17 | const compileArgs = ( |
| 18 | args: (string | number)[], |
| 19 | req: Request & Partial<{ originalUrl: string; ip: any }>, |
| 20 | res: Response, |
| 21 | options: LoggerOptions = {}, |
| 22 | status?: string, |
| 23 | msg?: string |
| 24 | ) => { |
| 25 | const { method } = req |
| 26 | const { statusCode } = res |
| 27 | |
| 28 | const url = req.originalUrl || req.url |
| 29 | |
| 30 | const methods = options.methods ?? METHODS |
| 31 | const timestamp = options.timestamp ?? false |
| 32 | const emojiEnabled = options.emoji |
| 33 | |
| 34 | if (methods.includes(method) && timestamp) { |
| 35 | args.push( |
| 36 | `${dayjs() |
| 37 | .format(typeof timestamp !== 'boolean' && timestamp.format ? timestamp.format : 'HH:mm:ss') |
| 38 | .toString()} - ` |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | if (options.ip) args.push(req.ip) |
| 43 | |
| 44 | if (emojiEnabled) args.push(statusEmoji[statusCode]) |
| 45 | |
| 46 | args.push(method) |
| 47 | |
| 48 | args.push(status || res.statusCode) |
| 49 | args.push(msg || res.statusMessage) |
| 50 | args.push(url) |
| 51 | } |
| 52 | |
| 53 | export const logger = (options: LoggerOptions = {}) => { |
| 54 | const methods = options.methods ?? METHODS |