Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #271 from SuperViz/chore/logger-config
Browse files Browse the repository at this point in the history
chore(debug-config): remove default logger and implement logger by namespace
  • Loading branch information
carlossantos74 authored Jul 23, 2023
2 parents 548cfb0 + d2c6d71 commit c810869
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 110 deletions.
4 changes: 2 additions & 2 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import doRequest from './do-request';
import logger from './logger';
import { Logger } from './logger';
import { Observer } from './observer';

export { logger, doRequest, Observer };
export { Logger, doRequest, Observer };
24 changes: 7 additions & 17 deletions src/common/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
import debug from 'debug';

import { Debug } from './logger';
import { Logger } from './logger';

jest.mock('debug');

describe('Debug', () => {
let debugInstance: Debug;
describe('Logger', () => {
let debugInstance: Logger;
let mockDebug: jest.MockedFunction<debug.Debugger>;

beforeEach(() => {
mockDebug = jest.fn() as unknown as jest.MockedFunction<debug.Debugger>;
(debug as jest.MockedFunction<typeof debug>).mockReturnValue(mockDebug);
debugInstance = new Debug('@superviz/sdk');
debugInstance = new Logger('@superviz/sdk');
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should create a new instance of Debug', () => {
test('should create a new instance of Debug', () => {
expect(debugInstance).toBeDefined();
});

it('should call debug with the correct scope on initialization', () => {
test('should call debug with the correct scope on initialization', () => {
expect(debug).toHaveBeenCalledWith('@superviz/sdk');
});

it('should call debug with the correct arguments on log', () => {
test('should call debug with the correct arguments on log', () => {
debugInstance.log('test-message', 123, { foo: 'bar' });
expect(mockDebug).toHaveBeenCalledWith('test-message', 123, { foo: 'bar' });
});

it('should call debug.enable with the correct prefix on enable', () => {
debugInstance.enable('test-prefix');
expect(debug.enable).toHaveBeenCalledWith('test-prefix');
});

it('should call debug.disable on disable', () => {
debugInstance.disable();
expect(debug.disable).toHaveBeenCalled();
});
});
13 changes: 1 addition & 12 deletions src/common/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@ import debug from 'debug';

export type Message = string | Error | number | Object;

export class Debug {
export class Logger {
private debug: debug.Debugger;

constructor(scope: string) {
this.disable();
this.debug = debug(scope);
}

log(formatter: Message, ...args: Array<Message>) {
this.debug(formatter, ...args);
}

enable(prefix: string = '*') {
debug.enable(prefix);
}

disable() {
debug.disable();
}
}

export default new Debug('@superviz/sdk');
8 changes: 4 additions & 4 deletions src/common/utils/observer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import throttle from 'lodash/throttle';

import { Debug } from './logger';
import { Logger } from './logger';

export type OberverOptions = {
throttleTime?: number;
logger?: Debug;
logger?: Logger;
};

export class Observer {
private logger: Debug;
private logger: Logger;
private callbacks: Function[];
private throttle: number;

constructor(options: OberverOptions = {}) {
const { logger, throttleTime } = options;

this.logger = logger ?? new Debug('@superviz/sdk/observer-helper');
this.logger = logger ?? new Logger('@superviz/sdk/observer-helper');
this.throttle = throttleTime;
this.callbacks = [];

Expand Down
15 changes: 5 additions & 10 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from './common/utils';
import debug from 'debug';

import ApiService from './services/api';
import RemoteConfigService from './services/remote-config-service';
import { ColorsVariables } from './services/video-conference-manager/types';
Expand Down Expand Up @@ -71,14 +72,6 @@ const COLOR_VARIABLES_MOCK = {
'sv-gray-200': '57 54 62',
};

jest.mock('./common/utils', () => ({
__esModule: true,
logger: {
enable: jest.fn(),
disable: jest.fn(),
log: jest.fn(),
},
}));
jest.mock('./services/api');
jest.mock('./services/auth-service', () => ({
__esModule: true,
Expand Down Expand Up @@ -258,12 +251,14 @@ describe('initialization success', () => {
});

test('should initialize with debug mode', async () => {
debug.enable = jest.fn();

const instance = await sdk.init(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
debug: true,
});

expect(logger.enable).toBeCalled();
expect(debug.enable).toBeCalled();
expect(instance).toBeDefined();
expect(instance).toBe(COMMUNICATOR_INSTANCE_MOCK);
});
Expand Down
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import debug from 'debug';

import {
MeetingEvent,
RealtimeEvent,
Expand All @@ -8,7 +10,6 @@ import {
} from './common/types/events.types';
import { Participant, Group, Avatar, ParticipantType } from './common/types/participant.types';
import { SuperVizSdkOptions, DevicesOptions } from './common/types/sdk-options.types';
import { logger } from './common/utils';
import ApiService from './services/api';
import AuthService from './services/auth-service';
import { BrowserService } from './services/browser';
Expand Down Expand Up @@ -98,7 +99,9 @@ const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<SuperV
validateOptions(options);

if (options.debug) {
logger.enable('@superviz/*');
debug.enable('*');
} else {
debug.disable();
}

const { apiUrl, conferenceLayerUrl } = await RemoteConfigService.getRemoteConfig(
Expand All @@ -120,12 +123,14 @@ const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<SuperV

const { ablyKey } = environment;
return Communicator(
Object.assign({}, options, { apiKey,
Object.assign({}, options, {
apiKey,
ablyKey,
conferenceLayerUrl,
apiUrl,
waterMark,
layoutPosition }),
layoutPosition,
}),
);
};

Expand Down
8 changes: 5 additions & 3 deletions src/services/communicator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TranscriptionEvent,
} from '../../common/types/events.types';
import { Participant, Group } from '../../common/types/participant.types';
import { Observer, logger } from '../../common/utils';
import { Observer, Logger } from '../../common/utils';
import { BrowserService } from '../browser';
import { ConnectionService } from '../connection-status';
import { IntegrationManager } from '../integration';
Expand All @@ -26,6 +26,7 @@ import { VideoFrameState, VideoManagerOptions } from '../video-conference-manage
import { SuperVizSdk, CommunicatorOptions, PluginOptions, ParticipandToFrame } from './types';

class Communicator {
private readonly logger: Logger;
private readonly realtime: AblyRealtimeService;
private readonly connectionService: ConnectionService;
private readonly browserService: BrowserService;
Expand Down Expand Up @@ -74,6 +75,7 @@ class Communicator {
this.group = group;
this.participant = participant;

this.logger = new Logger('@superviz/sdk/communicator');
this.realtime = new AblyRealtimeService(apiUrl, ablyKey);
this.browserService = new BrowserService();

Expand Down Expand Up @@ -234,7 +236,7 @@ class Communicator {
*/
public subscribe = (type: string, listener: Function): void => {
if (!this.observers[type]) {
this.observers[type] = new Observer({ logger });
this.observers[type] = new Observer({ logger: this.logger });
}

this.observers[type].subscribe(listener);
Expand Down Expand Up @@ -670,7 +672,7 @@ class Communicator {
* @returns {void}
*/
private onMeetingStateUpdate = (newState: MeetingState): void => {
logger.log('MEETING STATE', newState);
this.logger.log('MEETING STATE', newState);
this.publish(MeetingEvent.MEETING_STATE_UPDATE, newState);
};

Expand Down
14 changes: 9 additions & 5 deletions src/services/connection-status/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { MeetingConnectionStatus } from '../../common/types/events.types';
import { Observer, logger } from '../../common/utils';
import { Logger, Observer } from '../../common/utils';

import { DefaultConnectionService, WindowConnectionStatus } from './types';

export class ConnectionService implements DefaultConnectionService {
connectionStatus: MeetingConnectionStatus;
oldConnectionStatus: MeetingConnectionStatus;
private readonly logger: Logger;

connectionStatusObserver = new Observer({ logger });
public connectionStatus: MeetingConnectionStatus;
public oldConnectionStatus: MeetingConnectionStatus;
public connectionStatusObserver: Observer;

constructor() {
this.logger = new Logger('@superviz/sdk/connection-service');

this.connectionStatus = MeetingConnectionStatus.NOT_AVAILABLE;
this.connectionStatusObserver = new Observer({ logger: this.logger });
}

/**
Expand Down Expand Up @@ -44,7 +48,7 @@ export class ConnectionService implements DefaultConnectionService {
this.connectionStatus = newStatus;
this.connectionStatusObserver.publish(newStatus);

logger.log('CONNECTION STATUS CHANGE', newStatus);
this.logger.log('CONNECTION STATUS CHANGE', newStatus);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions src/services/message-bridge/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MOCK_OBSERVER_HELPER } from '../../../__mocks__/observer-helper.mock';
import { Debug } from '../../common/utils/logger';
import { Logger } from '../../common/utils/logger';

import { MessageBridge } from '.';

Expand All @@ -15,7 +15,7 @@ describe('MessageBridge', () => {

MessageBridgeInstance = new MessageBridge({
contentWindow: window,
logger: new Debug('@superviz/message-bridge'),
logger: new Logger('@superviz/message-bridge'),
sourceBlockList: ['https://google.com'],
});
});
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('MessageBridge', () => {
expect(() => {
const instance = new MessageBridge({
contentWindow: window,
logger: new Debug('@superviz/message-bridge'),
logger: new Logger('@superviz/message-bridge'),
});
}).toThrowError();
});
Expand Down
4 changes: 2 additions & 2 deletions src/services/message-bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Observer } from '../../common/utils';
import { Debug } from '../../common/utils/logger';
import { Logger } from '../../common/utils/logger';

import { Message, MessageBridgeOptions } from './types';

export class MessageBridge {
private logger: Debug;
private logger: Logger;
private allowedOrigins: string;
private contentWindow: Window;
private domains: Array<string>;
Expand Down
4 changes: 2 additions & 2 deletions src/services/message-bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
MeetingEvent,
RealtimeEvent,
} from '../../common/types/events.types';
import { Debug } from '../../common/utils/logger';
import { Logger } from '../../common/utils/logger';

export interface MessageBridgeOptions {
contentWindow: Window;
logger: Debug;
logger: Logger;
domains?: Array<string>;
allowedOrigins?: string;
sourceBlockList?: Array<string>;
Expand Down
Loading

0 comments on commit c810869

Please sign in to comment.