-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add global logger object #56
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
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { describe, test, expect, beforeEach, afterEach, vi } from "vitest"; | ||
| import { logger, setLogger } from "./Logger.js"; | ||
| import type { ILogger } from "$lib/types.js"; | ||
|
|
||
| describe("logger", () => { | ||
| test("Should default to globalThis.console", () => { | ||
| expect(logger).toBe(globalThis.console); | ||
| }); | ||
| }); | ||
|
|
||
| describe("setLogger", () => { | ||
| let originalLogger: ILogger; | ||
| let mockLogger: ILogger; | ||
| let consoleSpy: { | ||
| debug: any; | ||
| log: any; | ||
| warn: any; | ||
| error: any; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| // Store original logger state | ||
| originalLogger = logger; | ||
|
|
||
| // Create mock logger | ||
| mockLogger = { | ||
| debug: vi.fn(), | ||
| log: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn() | ||
| }; | ||
|
|
||
| // Create console spies | ||
| consoleSpy = { | ||
| debug: vi.spyOn(globalThis.console, 'debug').mockImplementation(() => {}), | ||
| log: vi.spyOn(globalThis.console, 'log').mockImplementation(() => {}), | ||
| warn: vi.spyOn(globalThis.console, 'warn').mockImplementation(() => {}), | ||
| error: vi.spyOn(globalThis.console, 'error').mockImplementation(() => {}) | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Restore original state | ||
| setLogger(originalLogger); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("Boolean arguments", () => { | ||
| test("Should set logger to globalThis.console when true", () => { | ||
| setLogger(true); | ||
|
|
||
| expect(logger).toBe(globalThis.console); | ||
|
|
||
| logger.debug("test"); | ||
| expect(consoleSpy.debug).toHaveBeenCalledWith("test"); | ||
| }); | ||
|
|
||
| test("Should set logger to noop functions when false", () => { | ||
| setLogger(false); | ||
|
|
||
| expect(logger).not.toBe(globalThis.console); | ||
|
|
||
| // Should not throw and should not call console | ||
| logger.debug("debug message"); | ||
| logger.log("log message"); | ||
| logger.warn("warn message"); | ||
| logger.error("error message"); | ||
|
|
||
| expect(consoleSpy.debug).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.log).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.warn).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.error).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("Should allow switching between true and false", () => { | ||
| setLogger(true); | ||
| logger.log("enabled message"); | ||
| expect(consoleSpy.log).toHaveBeenCalledWith("enabled message"); | ||
|
|
||
| setLogger(false); | ||
| logger.log("disabled message"); | ||
| expect(consoleSpy.log).toHaveBeenCalledTimes(1); // Should not be called again | ||
|
|
||
| setLogger(true); | ||
| logger.log("re-enabled message"); | ||
| expect(consoleSpy.log).toHaveBeenCalledWith("re-enabled message"); | ||
| expect(consoleSpy.log).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe("ILogger implementations", () => { | ||
| test("Should set custom logger as the global logger", () => { | ||
| setLogger(mockLogger); | ||
|
|
||
| expect(logger).toBe(mockLogger); | ||
|
|
||
| logger.debug("custom debug"); | ||
| logger.log("custom log"); | ||
| logger.warn("custom warn"); | ||
| logger.error("custom error"); | ||
|
|
||
| expect(mockLogger.debug).toHaveBeenCalledWith("custom debug"); | ||
| expect(mockLogger.log).toHaveBeenCalledWith("custom log"); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith("custom warn"); | ||
| expect(mockLogger.error).toHaveBeenCalledWith("custom error"); | ||
|
|
||
| // Console should not be called | ||
| expect(consoleSpy.debug).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.log).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.warn).not.toHaveBeenCalled(); | ||
| expect(consoleSpy.error).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("Should work with extended logger implementations", () => { | ||
| const extendedLogger = { | ||
| ...mockLogger, | ||
| trace: vi.fn(), | ||
| info: vi.fn() | ||
| }; | ||
|
|
||
| setLogger(extendedLogger); | ||
|
|
||
| expect(logger).toBe(extendedLogger); | ||
|
|
||
| logger.debug("debug"); | ||
| logger.log("log"); | ||
| logger.warn("warn"); | ||
| logger.error("error"); | ||
|
|
||
| expect(extendedLogger.debug).toHaveBeenCalledWith("debug"); | ||
| expect(extendedLogger.log).toHaveBeenCalledWith("log"); | ||
| expect(extendedLogger.warn).toHaveBeenCalledWith("warn"); | ||
| expect(extendedLogger.error).toHaveBeenCalledWith("error"); | ||
| }); | ||
|
|
||
| test("Should allow switching from custom logger back to stock logger", () => { | ||
| setLogger(mockLogger); | ||
| logger.log("custom message"); | ||
| expect(mockLogger.log).toHaveBeenCalledWith("custom message"); | ||
|
|
||
| setLogger(true); | ||
| logger.log("stock message"); | ||
| expect(consoleSpy.log).toHaveBeenCalledWith("stock message"); | ||
| expect(mockLogger.log).toHaveBeenCalledTimes(1); // Should not be called again | ||
| }); | ||
|
|
||
| test("Should handle multiple parameters correctly", () => { | ||
| setLogger(mockLogger); | ||
|
|
||
| logger.debug("debug", 123, { key: "value" }); | ||
| logger.log("log", true, null); | ||
| logger.warn("warn", "multiple", "parameters"); | ||
| logger.error("error", { error: "object" }); | ||
|
|
||
| expect(mockLogger.debug).toHaveBeenCalledWith("debug", 123, { key: "value" }); | ||
| expect(mockLogger.log).toHaveBeenCalledWith("log", true, null); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith("warn", "multiple", "parameters"); | ||
| expect(mockLogger.error).toHaveBeenCalledWith("error", { error: "object" }); | ||
| }); | ||
| }); | ||
| }); |
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,18 @@ | ||
| import type { ILogger } from "$lib/types.js"; | ||
|
|
||
| const stockLogger: ILogger = globalThis.console; | ||
|
|
||
| const noop = () => { }; | ||
|
|
||
| const offLogger: ILogger = { | ||
| debug: noop, | ||
| log: noop, | ||
| warn: noop, | ||
| error: noop | ||
| }; | ||
|
|
||
| export let logger: ILogger = stockLogger; | ||
|
|
||
| export function setLogger(newLogger: boolean | ILogger) { | ||
| logger = newLogger === true ? stockLogger : (newLogger === false ? offLogger : newLogger); | ||
| }; | ||
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
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.