From 93a952344293ac37885c229dd4cf150f1055d0e3 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Tue, 3 Oct 2017 16:04:42 -0600 Subject: [PATCH] Improved request logging --- server/logging/prettyPrint.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/server/logging/prettyPrint.ts b/server/logging/prettyPrint.ts index 1932cab..f13b7f2 100644 --- a/server/logging/prettyPrint.ts +++ b/server/logging/prettyPrint.ts @@ -1,5 +1,4 @@ import * as chalk from "chalk"; - import pretty = require("pino/pretty"); type Level = "default" | 60 | 50 | 40 | 30 | 20 | 10; @@ -37,7 +36,13 @@ function formatter(value: any) { // } // line += value.pid + " on " + value.hostname + ")"; + const isRequest = value.req && value.res; + line += ": "; + if (isRequest) { + line += chalk.reset(formatRequest(value)); + return line; + } if (value.msg) { line += chalk.cyan(value.msg); } @@ -50,6 +55,14 @@ function formatter(value: any) { return line; } +function formatRequest(value: any): string { + const matches = /Content-Length: (\d+)/.exec(value.res.header); + const contentLength = matches ? matches[1] : null; + return `${value.req.remoteAddress} - ` + + `"${value.req.method} ${value.req.url} ${value.res.statusCode}" ` + + `${value.responseTime} ms - ${contentLength}`; +} + function withSpaces(value: string): string { const lines = value.split("\n"); for (let i = 1; i < lines.length; i++) {