| 51 | } |
| 52 | |
| 53 | export const logger = (options: LoggerOptions = {}) => { |
| 54 | const methods = options.methods ?? METHODS |
| 55 | const output = options.output ?? { callback: console.log, color: true } |
| 56 | |
| 57 | return (req: Request, res: Response, next?: () => void) => { |
| 58 | res.on('finish', () => { |
| 59 | const args: (string | number)[] = [] |
| 60 | |
| 61 | if (methods.includes(req.method)) { |
| 62 | const s = res.statusCode.toString() |
| 63 | |
| 64 | if (!output.color) { |
| 65 | compileArgs(args, req, res, options) |
| 66 | const m = args.join(' ') |
| 67 | output.callback(m) |
| 68 | } else { |
| 69 | switch (s[0]) { |
| 70 | case '2': |
| 71 | compileArgs(args, req, res, options, cyan(bold(s)), cyan(res.statusMessage)) |
| 72 | output.callback(args.join(' ')) |
| 73 | break |
| 74 | case '4': |
| 75 | compileArgs(args, req, res, options, red(bold(s)), red(res.statusMessage)) |
| 76 | output.callback(args.join(' ')) |
| 77 | break |
| 78 | case '5': |
| 79 | compileArgs(args, req, res, options, magenta(bold(s)), magenta(res.statusMessage)) |
| 80 | output.callback(args.join(' ')) |
| 81 | break |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | }) |
| 86 | |
| 87 | next?.() |
| 88 | } |
| 89 | } |