Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ export function requestLog(octokit: Octokit) {

return (request as typeof octokit.request)(options)
.then((response) => {
const requestId = response.headers["x-github-request-id"];
octokit.log.info(
`${requestOptions.method} ${path} - ${response.status} in ${
`${requestOptions.method} ${path} - ${response.status} with id ${requestId} in ${
Date.now() - start
}ms`,
);
return response;
})

.catch((error) => {
octokit.log.info(
`${requestOptions.method} ${path} - ${error.status} in ${
const requestId =
error.response.headers["x-github-request-id"] || "UNKNOWN";
octokit.log.error(
`${requestOptions.method} ${path} - ${error.status} with id ${requestId} in ${
Date.now() - start
}ms`,
);
Expand Down
69 changes: 58 additions & 11 deletions test/request-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import { jest } from "@jest/globals";
import { requestLog } from "../src/index.js";

describe("logging", () => {
it("logs sucessful 'GET /'", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/", { ok: true });
it("logs successful 'GET /'", async () => {
const mock = fetchMock.sandbox().getOnce("https://api.github.com/", {
status: 200,
body: {
ok: true,
},
headers: {
"x-github-request-id": "1234",
},
});

const mockLogInfo = jest.fn();
const mockDebugInfo = jest.fn();
Expand All @@ -31,15 +37,22 @@ describe("logging", () => {
method: "GET",
url: "/",
});
expect(mockLogInfo.mock.calls[0][0]).toMatch(/GET \/ - 200 in \d+ms/);
expect(mockLogInfo.mock.calls[0][0]).toMatch(
/GET \/ - 200 with id 1234 in \d+ms/,
);
});

it("logs 404 for 'GET /unknown'", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/unknown", 404);
const mock = fetchMock.sandbox().getOnce("https://api.github.com/unknown", {
status: 404,
body: { message: "Not Found" },
headers: {
"x-github-request-id": "1234",
},
});

const mockLogInfo = jest.fn();
const mockErrorInfo = jest.fn();
const mockDebugInfo = jest.fn();
const MyOctokit = Octokit.plugin(requestLog);
const octokit = new MyOctokit({
Expand All @@ -50,15 +63,49 @@ describe("logging", () => {
debug: mockDebugInfo,
info: mockLogInfo,
warn() {},
error() {},
error: mockErrorInfo,
},
});

try {
await octokit.request("GET /unknown");
} catch (error) {
expect(mockErrorInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 404 with id 1234 in \d+ms/,
);
return;
}

throw new Error('"GET /unknown" should not resolve');
});

it("logs malformed error response for 'GET /unknown'", async () => {
const mock = fetchMock.sandbox().getOnce("https://api.github.com/unknown", {
status: 500,
body: "Internal Server Error",
});

const mockLogInfo = jest.fn();
const mockErrorInfo = jest.fn();
const mockDebugInfo = jest.fn();
const MyOctokit = Octokit.plugin(requestLog);
const octokit = new MyOctokit({
request: {
fetch: mock,
},
log: {
debug: mockDebugInfo,
info: mockLogInfo,
warn() {},
error: mockErrorInfo,
},
});

try {
await octokit.request("GET /unknown");
} catch (error) {
expect(mockLogInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 404 in \d+ms/,
expect(mockErrorInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 500 with id UNKNOWN in \d+ms/,
);
return;
}
Expand Down