diff --git a/src/utils/callbacks/api.spec.ts b/src/utils/callbacks/api.spec.ts index efdc675..1cf6a80 100644 --- a/src/utils/callbacks/api.spec.ts +++ b/src/utils/callbacks/api.spec.ts @@ -12,13 +12,6 @@ jest.mock("~/utils/filesys", () => ({ describe("utils/callback/api.ts", () => { describe("handleApi", () => { - const expectedLogs = - "this is an info\n" + - "this info log should be captured\n" + - "this is another info\n" + - "this error log should be captured\n" + - "this comes from process.stdout.write"; - const exampleRequest = { method: "GET", url: "/", @@ -60,6 +53,13 @@ describe("utils/callback/api.ts", () => { test("should handle returning a response body", async () => { const response = await handleApi(exampleRequest, "/"); + const expectedLogs = + "stdout:this is an info\n" + + "stdout:this info log should be captured\n" + + "stdout:this is another info\n" + + "stderr:this error log should be captured\n" + + "stdout:this comes from process.stdout.write"; + expect(response).toEqual({ body: "Hello world", status: 201, @@ -94,7 +94,7 @@ describe("utils/callback/api.ts", () => { buffer: "SGkgd29ybGQ=", status: 200, statusMessage: "OK", - logs: "captured logs\n", + logs: "stdout:captured logs\n", headers: { connection: "close", date: expect.any(String), diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 97cf2f7..3b0ec95 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -8,25 +8,28 @@ export class Logger { stdout: string[] = []; constructor() { - const s = this.streamWithContext(); + const stdout = this.streamWithContext("stdout"); + const stderr = this.streamWithContext("stderr"); - // @ts-ignore; - process.stdout.write = process.stderr.write = s.write.bind(s); + // @ts-ignore + process.stdout.write = stdout.write.bind(stdout); + // @ts-ignore + process.stderr.write = stderr.write.bind(stdout); console = new console.Console( - this.streamWithContext(), - this.streamWithContext() + this.streamWithContext("stdout"), + this.streamWithContext("stderr") ); } - streamWithContext() { + streamWithContext(level: "stderr" | "stdout") { return new stream.Writable({ write: ( chunk: any, _: BufferEncoding, callback: (error?: Error | null) => void ): void => { - this.stdout.push(chunk.toString()); + this.stdout.push(`${level}:${chunk.toString()}`); callback(null); }, });