Skip to content

Commit

Permalink
chore: specify log level
Browse files Browse the repository at this point in the history
  • Loading branch information
svedova committed Apr 19, 2024
1 parent d98c14d commit 8d97c2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/utils/callbacks/api.spec.ts
Expand Up @@ -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: "/",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
17 changes: 10 additions & 7 deletions src/utils/logger.ts
Expand Up @@ -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);
},
});
Expand Down

0 comments on commit 8d97c2c

Please sign in to comment.