-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Adjust logs #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adjust logs #1006
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
| import { createScopedLogger } from "./logger"; | ||
|
|
||
| vi.mock("next-axiom", () => ({ | ||
| log: { | ||
| info: vi.fn(), | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| debug: vi.fn(), | ||
| flush: vi.fn().mockResolvedValue(undefined), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@/env", () => ({ | ||
| env: { | ||
| NODE_ENV: "test", | ||
| NEXT_PUBLIC_AXIOM_TOKEN: undefined, | ||
| NEXT_PUBLIC_LOG_SCOPES: undefined, | ||
| ENABLE_DEBUG_LOGS: false, | ||
| }, | ||
| })); | ||
|
|
||
| describe("Logger", () => { | ||
| let consoleErrorSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| it("should serialize simple Error objects", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const error = new Error("Something went wrong"); | ||
|
|
||
| logger.error("Error occurred", { error }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("Something went wrong"); | ||
| }); | ||
|
|
||
| it("should serialize Error instances as message only", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const error = new Error("Custom error") as Error & { | ||
| statusCode: number; | ||
| code: string; | ||
| }; | ||
| error.statusCode = 400; | ||
| error.code = "VALIDATION_ERROR"; | ||
|
|
||
| logger.error("Validation failed", { error }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("Custom error"); | ||
| // Error instances show only message in console logs (custom properties not shown) | ||
| }); | ||
|
|
||
| it("should serialize nested error objects", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const error = { | ||
| response: { | ||
| data: { | ||
| error: { code: 500, message: "Internal error" }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| logger.error("Error processing message", { error }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("500"); | ||
| expect(loggedMessage).toContain("Internal error"); | ||
| }); | ||
|
|
||
| it("should serialize deeply nested errors", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const error = { | ||
| error: { | ||
| response: { | ||
| data: { | ||
| error: { | ||
| code: 404, | ||
| message: "Not found", | ||
| details: { resource: "user", id: "123" }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| logger.error("Resource not found", { error }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("404"); | ||
| expect(loggedMessage).toContain("Not found"); | ||
| expect(loggedMessage).toContain("user"); | ||
| expect(loggedMessage).toContain("123"); | ||
| }); | ||
|
|
||
| it("should serialize arrays of errors", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const errors = [ | ||
| new Error("Error 1"), | ||
| { message: "Error 2", code: 400 }, | ||
| new Error("Error 3"), | ||
| ]; | ||
|
|
||
| logger.error("Multiple errors", { errors }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("Error 1"); | ||
| expect(loggedMessage).toContain("Error 2"); | ||
| expect(loggedMessage).toContain("Error 3"); | ||
| expect(loggedMessage).toContain("400"); | ||
| }); | ||
|
|
||
| it("should serialize axios-like error structure", () => { | ||
| const logger = createScopedLogger("test"); | ||
| const error = { | ||
| response: { | ||
| status: 401, | ||
| data: { | ||
| error: "Unauthorized", | ||
| message: "Invalid token", | ||
| }, | ||
| }, | ||
| config: { | ||
| url: "/api/endpoint", | ||
| method: "POST", | ||
| }, | ||
| }; | ||
|
|
||
| logger.error("API request failed", { error }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
| expect(loggedMessage).toContain("401"); | ||
| expect(loggedMessage).toContain("Unauthorized"); | ||
| expect(loggedMessage).toContain("Invalid token"); | ||
| expect(loggedMessage).toContain("/api/endpoint"); | ||
| }); | ||
|
|
||
| it("should handle complex nested error objects without [object Object]", () => { | ||
| const logger = createScopedLogger("test"); | ||
|
|
||
| // Complex error like Gmail API error | ||
| const complexError = { | ||
| error: { | ||
| response: { | ||
| data: { | ||
| error: { | ||
| code: 404, | ||
| message: "Requested entity was not found.", | ||
| status: "NOT_FOUND", | ||
| }, | ||
| }, | ||
| }, | ||
| code: 404, | ||
| message: "Requested entity was not found.", | ||
| }, | ||
| attemptNumber: 1, | ||
| retriesLeft: 5, | ||
| }; | ||
|
|
||
| logger.error("Error finding draft", { error: complexError }); | ||
|
|
||
| const loggedMessage = consoleErrorSpy.mock.calls[0][0]; | ||
|
|
||
| // Should not have [object Object] | ||
| expect(loggedMessage).not.toContain("[object Object]"); | ||
|
|
||
| // Should contain important details | ||
| expect(loggedMessage).toContain("404"); | ||
| expect(loggedMessage).toContain("Requested entity was not found"); | ||
| expect(loggedMessage).toContain("attemptNumber"); | ||
| expect(loggedMessage).toContain("retriesLeft"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| v2.20.21 | ||
| v2.20.22 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.error is spied on but never restored, so later tests run with a mocked console.error and lose real error output. Add an afterEach (or enable vi.restoreAllMocks) to clean up the spy after each test.
Prompt for AI agents