| 3 | import chalk from "chalk"; |
| 4 | |
| 5 | export function logRequest(request: Request, response: Response, ms: number) { |
| 6 | let timestamp = chalk.gray(`[${new Date().toLocaleTimeString("en-US")}]`); |
| 7 | let method = request.method; |
| 8 | let url = new URL(request.url); |
| 9 | let path = url.pathname + url.search; |
| 10 | let statusCode = response.status; |
| 11 | // prettier-ignore |
| 12 | let statusColor = statusCode < 200 ? chalk.gray : statusCode < 300 ? chalk.greenBright : statusCode < 400 ? chalk.cyanBright : chalk.redBright; |
| 13 | let status = statusColor(statusCode); |
| 14 | // prettier-ignore |
| 15 | let statusTextColor = statusCode < 200 ? chalk.gray : statusCode < 300 ? chalk.green : statusCode < 400 ? chalk.cyan : chalk.red; |
| 16 | let statusText = statusTextColor(STATUS_CODES[statusCode]); |
| 17 | let duration = chalk.gray(ms < 1000 ? `${ms}ms` : `${(ms / 1000).toFixed(2)}s`); |
| 18 | |
| 19 | console.log(`${timestamp} ${method} ${path} ${status} ${statusText} ${duration}`); |
| 20 | } |