-
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
Open
Reversean
wants to merge
3
commits into
refactor/user-manager
Choose a base branch
from
refactor/catcher-logger
base: refactor/user-manager
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,5 @@ | ||
| export type { HawkStorage } from './storages/hawk-storage'; | ||
| export type { UserManager } from './users/user-manager'; | ||
| export { HawkStorageUserManager } from './users/hawk-storage-user-manager'; | ||
| export type { Logger, LogType } from './logger/logger'; | ||
| export { setLogger, 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,54 @@ | ||
| /** | ||
| * 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; | ||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
|
|
||
| /** | ||
| * 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,26 @@ | ||
| /** | ||
| * Abstract key–value storage contract used by Hawk internals to persist data across sessions. | ||
| */ | ||
| export interface HawkStorage { | ||
| /** | ||
| * Returns the value associated with the given key, or `null` if none exists. | ||
| * | ||
| * @param key - Storage key to look up. | ||
| */ | ||
| getItem(key: string): string | null | ||
|
|
||
| /** | ||
| * Persists a value under the given key. | ||
| * | ||
| * @param key - Storage key. | ||
| * @param value - Value to store. | ||
| */ | ||
| setItem(key: string, value: string): void | ||
|
|
||
| /** | ||
| * Removes the entry for the given key. | ||
| * | ||
| * @param key - Storage key to remove. | ||
| */ | ||
| removeItem(key: string): void | ||
| } |
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,49 @@ | ||
| import type { AffectedUser } from '@hawk.so/types'; | ||
| import type { HawkStorage } from '../storages/hawk-storage'; | ||
| import type { UserManager } from './user-manager'; | ||
|
|
||
| /** | ||
| * Storage key used to persist the user identifier. | ||
| */ | ||
| const HAWK_USER_STORAGE_KEY = 'hawk-user-id'; | ||
|
|
||
| /** | ||
| * {@link UserManager} implementation that persists the affected user | ||
| * via an injected {@link HawkStorage} backend. | ||
| */ | ||
| export class HawkStorageUserManager implements UserManager { | ||
| /** | ||
| * Underlying storage used to read and write the user identifier. | ||
| */ | ||
| private readonly storage: HawkStorage; | ||
|
|
||
| /** | ||
| * @param storage - Storage backend to use for persistence. | ||
| */ | ||
| constructor(storage: HawkStorage) { | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| public getUser(): AffectedUser | null { | ||
| const storedId = this.storage.getItem(HAWK_USER_STORAGE_KEY); | ||
|
|
||
| if (storedId) { | ||
| return { | ||
| id: storedId, | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| public setUser(user: AffectedUser): void { | ||
| this.storage.setItem(HAWK_USER_STORAGE_KEY, user.id); | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| public clear(): void { | ||
| this.storage.removeItem(HAWK_USER_STORAGE_KEY); | ||
| } | ||
| } |
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,26 @@ | ||
| import type { AffectedUser } from '@hawk.so/types'; | ||
|
|
||
| /** | ||
| * Contract for user identity managers. | ||
| * | ||
| * Implementations are responsible for persisting and retrieving the | ||
| * {@link AffectedUser} that is attached to every error report sent by the catcher. | ||
| */ | ||
| export interface UserManager { | ||
| /** | ||
| * Returns the current affected user, or `null` if none has been set. | ||
| */ | ||
| getUser(): AffectedUser | null | ||
|
|
||
| /** | ||
| * Replaces the stored user with the provided one. | ||
| * | ||
| * @param user - The affected user to persist. | ||
| */ | ||
| setUser(user: AffectedUser): void | ||
|
|
||
| /** | ||
| * Removes any previously stored user data. | ||
| */ | ||
| clear(): void | ||
| } |
41 changes: 41 additions & 0 deletions
41
packages/core/tests/users/hawk-storage-user-manager.test.ts
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,41 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { HawkStorageUserManager } from '../../src'; | ||
| import type { HawkStorage } from '../../src'; | ||
|
|
||
| describe('StorageUserManager', () => { | ||
| let storage: HawkStorage; | ||
| let manager: HawkStorageUserManager; | ||
|
|
||
| beforeEach(() => { | ||
| storage = { | ||
| getItem: vi.fn().mockReturnValue(null), | ||
| setItem: vi.fn(), | ||
| removeItem: vi.fn(), | ||
| }; | ||
| manager = new HawkStorageUserManager(storage); | ||
| }); | ||
|
|
||
| it('should return null when storage is empty', () => { | ||
| expect(manager.getUser()).toBeNull(); | ||
| expect(storage.getItem).toHaveBeenCalledWith('hawk-user-id'); | ||
| }); | ||
|
|
||
| it('should return user when ID exists in storage', () => { | ||
| vi.mocked(storage.getItem).mockReturnValue('test-user-123'); | ||
|
|
||
| expect(manager.getUser()).toEqual({id: 'test-user-123'}); | ||
| expect(storage.getItem).toHaveBeenCalledWith('hawk-user-id'); | ||
| }); | ||
|
|
||
| it('should persist user ID via setUser()', () => { | ||
| manager.setUser({id: 'user-abc'}); | ||
|
|
||
| expect(storage.setItem).toHaveBeenCalledWith('hawk-user-id', 'user-abc'); | ||
| }); | ||
|
|
||
| it('should remove user ID via clear()', () => { | ||
| manager.clear(); | ||
|
|
||
| expect(storage.removeItem).toHaveBeenCalledWith('hawk-user-id'); | ||
| }); | ||
| }); |
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,13 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": null, | ||
| "declaration": false, | ||
| "types": ["vitest/globals"] | ||
| }, | ||
| "include": [ | ||
| "src/**/*", | ||
| "tests/**/*", | ||
| "vitest.config.ts" | ||
| ] | ||
| } |
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,15 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| globals: true, | ||
| include: ['tests/**/*.test.ts'], | ||
| typecheck: { | ||
| tsconfig: './tsconfig.test.json', | ||
| }, | ||
| coverage: { | ||
| provider: 'v8', | ||
| include: ['src/**/*.ts'], | ||
| }, | ||
| }, | ||
| }); |
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
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.
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.
The core logger abstraction (setLogger and log functions in @hawk.so/core) lacks test coverage. While the browser-specific implementation (createBrowserLogger) has comprehensive tests, the core abstraction functions are not tested. Consider adding tests to verify that: 1) log is a no-op when no logger is set, 2) setLogger correctly registers a logger, 3) log correctly delegates to the registered logger, and 4) the logger can be replaced by calling setLogger again.