Skip to content

Commit 0ca5710

Browse files
committed
refactor(core): Logger abstraction added
1 parent 505caf2 commit 0ca5710

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export type { Logger } from './utils/log'
12
export { UserManager } from './types/user-manager'

packages/core/src/utils/log.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type LogType = 'log' | 'warn' | 'error' | 'info';
2+
3+
export interface Logger {
4+
(msg: string, type?: LogType, args?: unknown): void;
5+
}

packages/javascript/src/catcher.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Socket from './modules/socket';
22
import Sanitizer from './modules/sanitizer';
3-
import log from './utils/log';
3+
import windowLog from './utils/log';
44
import StackParser from './modules/stackParser';
55
import type { CatcherMessage, HawkInitialSettings, BreadcrumbsAPI, Transport } from './types';
66
import { VueIntegration } from './integrations/vue';
@@ -18,7 +18,7 @@ import { isErrorProcessed, markErrorAsProcessed } from './utils/event';
1818
import { ConsoleCatcher } from './addons/consoleCatcher';
1919
import { BreadcrumbManager } from './addons/breadcrumbs';
2020
import { validateUser, validateContext, isValidEventPayload } from './utils/validation';
21-
import { UserManager } from "@hawk.so/core";
21+
import { Logger, UserManager } from "@hawk.so/core";
2222
import { LocalStorageUserManager } from "./utils/local-storage-user-manager";
2323

2424
/**
@@ -112,6 +112,8 @@ export default class Catcher {
112112
*/
113113
private readonly userManager: UserManager = new LocalStorageUserManager();
114114

115+
private readonly log: Logger = windowLog
116+
115117
/**
116118
* Catcher constructor
117119
*
@@ -142,7 +144,7 @@ export default class Catcher {
142144
: true;
143145

144146
if (!this.token) {
145-
log(
147+
this.log(
146148
'Integration Token is missed. You can get it on https://hawk.so at Project Settings.',
147149
'warn'
148150
);
@@ -158,7 +160,7 @@ export default class Catcher {
158160
reconnectionAttempts: settings.reconnectionAttempts,
159161
reconnectionTimeout: settings.reconnectionTimeout,
160162
onClose(): void {
161-
log(
163+
this.log(
162164
'Connection lost. Connection will be restored when new errors occurred',
163165
'info'
164166
);
@@ -381,7 +383,7 @@ export default class Catcher {
381383
return;
382384
}
383385

384-
log('Unable to send error. Seems like it is Hawk internal bug. Please, report it here: https://github.com/codex-team/hawk.javascript/issues/new', 'warn', e);
386+
this.log('Unable to send error. Seems like it is Hawk internal bug. Please, report it here: https://github.com/codex-team/hawk.javascript/issues/new', 'warn', e);
385387
}
386388
}
387389

@@ -393,7 +395,7 @@ export default class Catcher {
393395
private sendErrorFormatted(errorFormatted: CatcherMessage): void {
394396
this.transport.send(errorFormatted)
395397
.catch((sendingError) => {
396-
log('WebSocket sending error', 'error', sendingError);
398+
this.log('WebSocket sending error', 'error', sendingError);
397399
});
398400
}
399401

@@ -450,7 +452,7 @@ export default class Catcher {
450452
/**
451453
* Anything else is invalid — warn, payload stays untouched (hook only received a clone)
452454
*/
453-
log(
455+
this.log(
454456
'Invalid beforeSend value. It should return event or false. Event is sent without changes.',
455457
'warn'
456458
);
@@ -605,7 +607,7 @@ export default class Catcher {
605607
try {
606608
return await this.stackParser.parse(error as Error);
607609
} catch (e) {
608-
log('Can not parse stack:', 'warn', e);
610+
this.log('Can not parse stack:', 'warn', e);
609611

610612
return null;
611613
}

0 commit comments

Comments
 (0)