Skip to content

Commit

Permalink
Add logging for requests to miniflare
Browse files Browse the repository at this point in the history
  • Loading branch information
jspspike committed Mar 30, 2023
1 parent 6ec5ee3 commit b7098e7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/tre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
handlePrettyErrorRequest,
reviveError,
} from "./plugins/core";
import { formatResponse } from "./plugins/core/log";
import {
Config,
Runtime,
Expand All @@ -67,6 +68,7 @@ import {
Clock,
HttpError,
Log,
LogLevel,
MiniflareCoreError,
Mutex,
NoOpLog,
Expand Down Expand Up @@ -456,6 +458,15 @@ export class Miniflare {
return this.#workerOpts.map<SourceOptions>(({ core }) => core);
}

async #logResponse(request: Request) {
if (this.#log.level < LogLevel.INFO) {
return;
}

const response = await formatResponse(request);
this.#log.log(response);
}

#handleLoopback = async (
req: http.IncomingMessage,
res?: http.ServerResponse
Expand Down Expand Up @@ -510,6 +521,8 @@ export class Miniflare {
this.#workerSrcOpts,
request
);
} else if (url.pathname === "/core/log") {
await this.#logResponse(request);
} else {
// TODO: check for proxying/outbound fetch header first (with plans for fetch mocking)
response = await this.#handleLoopbackPlugins(request, url);
Expand Down
14 changes: 14 additions & 0 deletions packages/tre/src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ async function handleEvent(event) {
}
}
const headers = new Headers();
headers.append("Content-Type", "application/json")
await ${BINDING_SERVICE_LOOPBACK}.fetch("http://localhost/core/log", {
method: "POST",
headers: headers,
body: JSON.stringify({
"status": response.status,
"statusText": response.statusText,
"method": event.request.method,
"url": response.url,
"time": Date.now(),
}),
});
const liveReloadScript = globalThis.${BINDING_DATA_LIVE_RELOAD_SCRIPT};
if (
liveReloadScript !== undefined &&
Expand Down
34 changes: 34 additions & 0 deletions packages/tre/src/plugins/core/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Colorize, blue, bold, green, grey, red, yellow } from "kleur/colors";
import { Request } from "../../http";

export interface ResponseInfo {
status: number;
statusText: string;
method: string;
url: string;
time: number;
}

export async function formatResponse(request: Request) {
const info: ResponseInfo = JSON.parse(await request.text());
const url = require("node:url").parse(info.url);

const responseTime = Date.now() - info.time;

return [
`${bold(info.method)} ${url.pathname} `,
colourFromHTTPStatus(info.status)(
`${bold(info.status)} ${info.statusText} `
),
grey(`(${responseTime}ms`),
// Only include waitUntilTime if there were waitUntil promises
grey(")"),
].join("");
}

function colourFromHTTPStatus(status: number): Colorize {
if (200 <= status && status < 300) return green;
if (400 <= status && status < 500) return yellow;
if (500 <= status) return red;
return blue;
}

0 comments on commit b7098e7

Please sign in to comment.