Skip to content

Commit

Permalink
fix: spread tail messages when logging (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone authored Mar 28, 2022
1 parent f12335a commit 7942936
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/chilly-bears-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: spread tail messages when logging

Logged messages (via console, etc) would previously be logged as an array of values. This spreads it when logging to match what is expected.
49 changes: 49 additions & 0 deletions packages/wrangler/src/__tests__/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,55 @@ describe("tail", () => {
`);
});

it("logs console messages and exceptions", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker");

const event = generateMockRequestEvent();
const message = generateMockEventMessage({
event,
logs: [
{ message: ["some string"], level: "log", timestamp: 1234561 },
{
message: [{ complex: "object" }],
level: "log",
timestamp: 1234562,
},
{ message: [1234], level: "error", timestamp: 1234563 },
],
exceptions: [
{ name: "Error", message: "some error", timestamp: 1234564 },
{ name: "Error", message: { complex: "error" }, timestamp: 1234564 },
],
});
const serializedMessage = serialize(message);

api.ws.send(serializedMessage);
expect(
std.out
.replace(
new Date(mockEventTimestamp).toLocaleString(),
"[mock event timestamp]"
)
.replace(
mockTailExpiration.toLocaleString(),
"[mock expiration date]"
)
).toMatchInlineSnapshot(`
"successfully created tail, expires at [mock expiration date]
Connected to test-worker, waiting for logs...
GET https://example.org/ - Ok @ [mock event timestamp]
(log) some string
(log) { complex: 'object' }
(error) 1234"
`);
expect(std.err).toMatchInlineSnapshot(`
" Error: some error
Error: { complex: 'error' }"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("logs scheduled messages in pretty format", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker --format pretty");
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/tail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export type TailEventMessage = {
* Any logs sent out by the worker
*/
logs: {
message: unknown;
message: unknown[];
level: string; // TODO: make this a union of possible values
timestamp: number;
}[];
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/tail/printing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function prettyPrintLogs(data: WebSocket.RawData): void {

if (eventMessage.logs.length > 0) {
eventMessage.logs.forEach(({ level, message }) => {
console.log(` (${level})`, message);
console.log(` (${level})`, ...message);
});
}

Expand Down

0 comments on commit 7942936

Please sign in to comment.