Improved request logging

This commit is contained in:
Alex Mikhalev 2017-10-03 16:04:42 -06:00
parent a698775388
commit 93a9523442

View File

@ -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++) {