-
Notifications
You must be signed in to change notification settings - Fork 4
Logger abstraction added #159
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
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export type { HawkStorage } from './storages/hawk-storage'; | ||
| export type { RandomGenerator } from './utils/random'; | ||
| export { HawkUserManager } from './users/hawk-user-manager'; | ||
| export type { Logger, LogType } from './logger/logger'; | ||
| export { isLoggerSet, setLogger, resetLogger, log } from './logger/logger'; |
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,68 @@ | ||
| /** | ||
| * Log level type for categorizing log messages. | ||
| * | ||
| * Includes standard console methods supported in both browser and Node.js: | ||
| * - Standard levels: `log`, `warn`, `error`, `info` | ||
| * - Performance timing: `time`, `timeEnd` | ||
| */ | ||
| export type LogType = 'log' | 'warn' | 'error' | 'info' | 'time' | 'timeEnd'; | ||
|
|
||
| /** | ||
| * Logger function interface for environment-specific logging implementations. | ||
| * | ||
| * Implementations should handle message formatting, output styling, | ||
| * and platform-specific logging mechanisms (e.g., console, file, network). | ||
| * | ||
| * @param msg - The message to log. | ||
| * @param type - Log level/severity (default: 'log'). | ||
| * @param args - Additional data to include with the log message. | ||
| */ | ||
| export interface Logger { | ||
| (msg: string, type?: LogType, args?: unknown): void; | ||
| } | ||
|
|
||
| /** | ||
| * Global logger instance, set by environment-specific packages. | ||
| */ | ||
| let loggerInstance: Logger | null = null; | ||
|
|
||
| /** | ||
| * Checks if logger instance has been registered. | ||
| */ | ||
| export function isLoggerSet(): boolean { | ||
| return loggerInstance !== null; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the environment-specific logger implementation. | ||
| * | ||
| * This should be called once during application initialization | ||
| * by the environment-specific package. | ||
| * | ||
| * @param logger - Logger implementation to use globally. | ||
| */ | ||
| export function setLogger(logger: Logger): void { | ||
| loggerInstance = logger; | ||
| } | ||
|
|
||
| /** | ||
| * Clears the registered logger instance. | ||
| */ | ||
| export function resetLogger(): void { | ||
| loggerInstance = null; | ||
| } | ||
|
|
||
| /** | ||
| * Logs a message using the registered logger implementation. | ||
| * | ||
| * If no logger has been registered via {@link setLogger}, this is a no-op. | ||
| * | ||
| * @param msg - Message to log. | ||
| * @param type - Log level (default: 'log'). | ||
| * @param args - Additional arguments to log. | ||
| */ | ||
| export function log(msg: string, type?: LogType, args?: unknown): void { | ||
| if (loggerInstance) { | ||
| loggerInstance(msg, type, args); | ||
| } | ||
| } | ||
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,86 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| /** | ||
| * Each test gets a fresh module instance via vi.resetModules() so that | ||
| * the module-level loggerInstance starts as null. | ||
| */ | ||
| describe('Logger', () => { | ||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| it('should return false from isLoggerSet when no logger has been registered', async () => { | ||
| const { isLoggerSet } = await import('../../src/logger/logger'); | ||
|
|
||
| expect(isLoggerSet()).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true from isLoggerSet after setLogger is called', async () => { | ||
| const { isLoggerSet, setLogger } = await import('../../src/logger/logger'); | ||
|
|
||
| setLogger(vi.fn()); | ||
|
|
||
| expect(isLoggerSet()).toBe(true); | ||
| }); | ||
|
|
||
| it('should not throw when log is called with no logger registered', async () => { | ||
| const { log } = await import('../../src/logger/logger'); | ||
|
|
||
| expect(() => log('test message')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should forward msg, type, and args to the registered logger', async () => { | ||
| const { setLogger, log } = await import('../../src/logger/logger'); | ||
| const mockLogger = vi.fn(); | ||
|
|
||
| setLogger(mockLogger); | ||
| log('something went wrong', 'warn', { code: 42 }); | ||
|
|
||
| expect(mockLogger).toHaveBeenCalledOnce(); | ||
| expect(mockLogger).toHaveBeenCalledWith('something went wrong', 'warn', { code: 42 }); | ||
| }); | ||
|
|
||
| it('should pass undefined for omitted type and args', async () => { | ||
| const { setLogger, log } = await import('../../src/logger/logger'); | ||
| const mockLogger = vi.fn(); | ||
|
|
||
| setLogger(mockLogger); | ||
| log('simple'); | ||
|
|
||
| expect(mockLogger).toHaveBeenCalledWith('simple', undefined, undefined); | ||
| }); | ||
|
|
||
| it('should replace a previously registered logger when setLogger is called again', async () => { | ||
| const { setLogger, log } = await import('../../src/logger/logger'); | ||
| const first = vi.fn(); | ||
| const second = vi.fn(); | ||
|
|
||
| setLogger(first); | ||
| setLogger(second); | ||
| log('msg'); | ||
|
|
||
| expect(first).not.toHaveBeenCalled(); | ||
| expect(second).toHaveBeenCalledWith('msg', undefined, undefined); | ||
| }); | ||
|
|
||
| it('should clear the registered logger when resetLogger is called', async () => { | ||
| const { isLoggerSet, setLogger, resetLogger } = await import('../../src/logger/logger'); | ||
|
|
||
| setLogger(vi.fn()); | ||
| expect(isLoggerSet()).toBe(true); | ||
|
|
||
| resetLogger(); | ||
| expect(isLoggerSet()).toBe(false); | ||
| }); | ||
|
|
||
| it('should become a no-op after resetLogger is called', async () => { | ||
| const { setLogger, resetLogger, log } = await import('../../src/logger/logger'); | ||
| const mockLogger = vi.fn(); | ||
|
|
||
| setLogger(mockLogger); | ||
| resetLogger(); | ||
| log('msg'); | ||
|
|
||
| expect(mockLogger).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,61 @@ | ||
| import type { Logger, LogType } from '@hawk.so/core'; | ||
|
|
||
| /** | ||
| * Creates a browser console logger with Hawk branding and styled output. | ||
| * | ||
| * The logger outputs to `window.console` with a dark label badge | ||
| * containing the Hawk version. Messages are formatted with CSS | ||
| * styling for better visibility in browser developer tools. | ||
| * | ||
| * @param version - Version string to display in log messages. | ||
| * @param style - Optional CSS style for the message text (default: 'color: inherit'). | ||
| * @returns {Logger} Logger function implementation for browser environments. | ||
| * | ||
| * @example | ||
| * ```TypeScript | ||
| * import { createBrowserLogger } from '@hawk.so/javascript'; | ||
| * import { setLogger } from '@hawk.so/core'; | ||
| * | ||
| * const logger = createBrowserLogger('3.2.0'); | ||
| * setLogger(logger); | ||
| * | ||
| * // Custom styling | ||
| * const styledLogger = createBrowserLogger('3.2.0', 'color: blue; font-weight: bold'); | ||
| * setLogger(styledLogger); | ||
| * ``` | ||
| */ | ||
| export function createBrowserLogger(version: string, style = 'color: inherit'): Logger { | ||
| return (msg: string, type: LogType = 'log', args?: unknown): void => { | ||
| if (!('console' in window)) { | ||
| return; | ||
| } | ||
|
|
||
| const editorLabelText = `Hawk (${version})`; | ||
| const editorLabelStyle = `line-height: 1em; | ||
| color: #fff; | ||
| display: inline-block; | ||
| background-color: rgba(0,0,0,.7); | ||
| padding: 3px 5px; | ||
| border-radius: 3px; | ||
| margin-right: 2px`; | ||
|
|
||
| try { | ||
| switch (type) { | ||
| case 'time': | ||
| case 'timeEnd': | ||
| console[type](`( ${editorLabelText} ) ${msg}`); | ||
| break; | ||
| case 'log': | ||
| case 'warn': | ||
| case 'error': | ||
| case 'info': | ||
| if (args !== undefined) { | ||
| console[type](`%c${editorLabelText}%c ${msg} %o`, editorLabelStyle, style, args); | ||
| } else { | ||
| console[type](`%c${editorLabelText}%c ${msg}`, editorLabelStyle, style); | ||
| } | ||
| break; | ||
| } | ||
| } catch (ignored) {} | ||
| }; | ||
| } |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import log from './log'; | ||
| import { log } from '@hawk.so/core'; | ||
|
|
||
| /** | ||
| * Symbol to mark error as processed by Hawk | ||
|
|
||
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.