openvidu-node-client: Added custom logger. Fixes #791

Added custom logger and exported a new enableProdMode method with the aim of enabling the production mode and avoiding unnecessary logs
pull/780/head
csantosm 2023-02-22 23:31:32 +01:00
parent 027e85052a
commit f3550c4f6a
4 changed files with 133 additions and 17 deletions

View File

@ -0,0 +1,41 @@
type ConsoleFunction = (...data: any) => void;
export class ConsoleLogger {
/**
* @hidden
*/
logger: Console;
/**
* @hidden
*/
log: ConsoleFunction;
/**
* @hidden
*/
info: ConsoleFunction;
/**
* @hidden
*/
debug: ConsoleFunction;
/**
* @hidden
*/
warn: ConsoleFunction;
/**
* @hidden
*/
error: ConsoleFunction;
constructor(console: Console) {
this.logger = console;
this.log = console.log;
this.info = console.info;
this.debug = console.debug;
this.warn = console.warn;
this.error = console.error;
}
}

View File

@ -0,0 +1,62 @@
import { ConsoleLogger } from './ConsoleLogger';
export class OpenViduLogger {
private static instance: OpenViduLogger;
private defaultConsoleLogger: ConsoleLogger = new ConsoleLogger(globalThis.console);
private isProdMode = false;
/**
* @hidden
*/
static getInstance(): OpenViduLogger {
if (!OpenViduLogger.instance) {
OpenViduLogger.instance = new OpenViduLogger();
}
return OpenViduLogger.instance;
}
/**
* @hidden
*/
log(...args: any[]) {
if (!this.isProdMode) {
this.defaultConsoleLogger.log.apply(this.defaultConsoleLogger.logger, arguments);
}
}
/**
* @hidden
*/
debug(...args: any[]) {
if (!this.isProdMode) {
this.defaultConsoleLogger.debug.apply(this.defaultConsoleLogger.logger, arguments);
}
}
/**
* @hidden
*/
info(...args: any[]) {
if (!this.isProdMode) {
this.defaultConsoleLogger.info.apply(this.defaultConsoleLogger.logger, arguments);
}
}
/**
* @hidden
*/
warn(...args: any[]) {
this.defaultConsoleLogger.warn.apply(this.defaultConsoleLogger.logger, arguments);
}
/**
* @hidden
*/
error(...args: any[]) {
this.defaultConsoleLogger.error.apply(this.defaultConsoleLogger.logger, arguments);
}
enableProdMode() {
this.isProdMode = true;
}
}

View File

@ -16,6 +16,7 @@
*/ */
import axios, { AxiosError } from 'axios'; import axios, { AxiosError } from 'axios';
import { OpenViduLogger } from './Logger/OpenViduLogger';
import { Connection } from './Connection'; import { Connection } from './Connection';
import { Recording } from './Recording'; import { Recording } from './Recording';
import { RecordingProperties } from './RecordingProperties'; import { RecordingProperties } from './RecordingProperties';
@ -29,6 +30,8 @@ interface ObjMap<T> {
[s: string]: T; [s: string]: T;
} }
const logger: OpenViduLogger = OpenViduLogger.getInstance();
export class OpenVidu { export class OpenVidu {
private Buffer = require('buffer/').Buffer; private Buffer = require('buffer/').Buffer;
@ -199,7 +202,7 @@ export class OpenVidu {
if (!!activeSession) { if (!!activeSession) {
activeSession.recording = true; activeSession.recording = true;
} else { } else {
console.warn( logger.warn(
"No active session found for sessionId '" + "No active session found for sessionId '" +
r.sessionId + r.sessionId +
"'. This instance of OpenVidu Node Client didn't create this session" "'. This instance of OpenVidu Node Client didn't create this session"
@ -244,7 +247,7 @@ export class OpenVidu {
if (!!activeSession) { if (!!activeSession) {
activeSession.recording = false; activeSession.recording = false;
} else { } else {
console.warn( logger.warn(
"No active session found for sessionId '" + "No active session found for sessionId '" +
r.sessionId + r.sessionId +
"'. This instance of OpenVidu Node Client didn't create this session" "'. This instance of OpenVidu Node Client didn't create this session"
@ -424,7 +427,7 @@ export class OpenVidu {
if (!!activeSession) { if (!!activeSession) {
activeSession.broadcasting = true; activeSession.broadcasting = true;
} else { } else {
console.warn( logger.warn(
"No active session found for sessionId '" + "No active session found for sessionId '" +
sessionId + sessionId +
"'. This instance of OpenVidu Node Client didn't create this session" "'. This instance of OpenVidu Node Client didn't create this session"
@ -472,7 +475,7 @@ export class OpenVidu {
if (!!activeSession) { if (!!activeSession) {
activeSession.broadcasting = false; activeSession.broadcasting = false;
} else { } else {
console.warn( logger.warn(
"No active session found for sessionId '" + "No active session found for sessionId '" +
sessionId + sessionId +
"'. This instance of OpenVidu Node Client didn't create this session" "'. This instance of OpenVidu Node Client didn't create this session"
@ -521,12 +524,12 @@ export class OpenVidu {
// 2. Update existing Session // 2. Update existing Session
const changed: boolean = !storedSession.equalTo(fetchedSession); const changed: boolean = !storedSession.equalTo(fetchedSession);
storedSession.resetWithJson(jsonSession); storedSession.resetWithJson(jsonSession);
console.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed); logger.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed);
hasChanged = hasChanged || changed; hasChanged = hasChanged || changed;
} else { } else {
// 3. Add new Session // 3. Add new Session
this.activeSessions.push(fetchedSession); this.activeSessions.push(fetchedSession);
console.log("New session '" + fetchedSession.sessionId + "' info fetched"); logger.log("New session '" + fetchedSession.sessionId + "' info fetched");
hasChanged = true; hasChanged = true;
} }
}); });
@ -535,13 +538,13 @@ export class OpenVidu {
for (var i = this.activeSessions.length - 1; i >= 0; --i) { for (var i = this.activeSessions.length - 1; i >= 0; --i) {
let sessionId = this.activeSessions[i].sessionId; let sessionId = this.activeSessions[i].sessionId;
if (!fetchedSessionIds.includes(sessionId)) { if (!fetchedSessionIds.includes(sessionId)) {
console.log("Removing closed session '" + sessionId + "'"); logger.log("Removing closed session '" + sessionId + "'");
hasChanged = true; hasChanged = true;
this.activeSessions.splice(i, 1); this.activeSessions.splice(i, 1);
} }
} }
console.log('Active sessions info fetched: ', fetchedSessionIds); logger.log('Active sessions info fetched: ', fetchedSessionIds);
resolve(hasChanged); resolve(hasChanged);
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
@ -671,13 +674,13 @@ export class OpenVidu {
storedSession.connections.forEach((connection) => { storedSession.connections.forEach((connection) => {
addWebRtcStatsToConnections(connection, jsonSession.connections.content); addWebRtcStatsToConnections(connection, jsonSession.connections.content);
}); });
console.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed); logger.log("Available session '" + storedSession.sessionId + "' info fetched. Any change: " + changed);
sessionChanges[storedSession.sessionId] = changed; sessionChanges[storedSession.sessionId] = changed;
globalChanges = globalChanges || changed; globalChanges = globalChanges || changed;
} else { } else {
// 3. Add new Session // 3. Add new Session
this.activeSessions.push(fetchedSession); this.activeSessions.push(fetchedSession);
console.log("New session '" + fetchedSession.sessionId + "' info fetched"); logger.log("New session '" + fetchedSession.sessionId + "' info fetched");
sessionChanges[fetchedSession.sessionId] = true; sessionChanges[fetchedSession.sessionId] = true;
globalChanges = true; globalChanges = true;
} }
@ -687,14 +690,14 @@ export class OpenVidu {
for (var i = this.activeSessions.length - 1; i >= 0; --i) { for (var i = this.activeSessions.length - 1; i >= 0; --i) {
let sessionId = this.activeSessions[i].sessionId; let sessionId = this.activeSessions[i].sessionId;
if (!fetchedSessionIds.includes(sessionId)) { if (!fetchedSessionIds.includes(sessionId)) {
console.log("Removing closed session '" + sessionId + "'"); logger.log("Removing closed session '" + sessionId + "'");
sessionChanges[sessionId] = true; sessionChanges[sessionId] = true;
globalChanges = true; globalChanges = true;
this.activeSessions.splice(i, 1); this.activeSessions.splice(i, 1);
} }
} }
console.log('Active sessions info fetched: ', fetchedSessionIds); logger.log('Active sessions info fetched: ', fetchedSessionIds);
resolve({ changes: globalChanges, sessionChanges }); resolve({ changes: globalChanges, sessionChanges });
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
@ -708,6 +711,13 @@ export class OpenVidu {
} }
// tslint:enable:no-string-literal // tslint:enable:no-string-literal
/**
* Disable all logging except error level
*/
enableProdMode(): void {
logger.enableProdMode();
}
private getBasicAuth(secret: string): string { private getBasicAuth(secret: string): string {
return 'Basic ' + this.Buffer('OPENVIDUAPP:' + secret).toString('base64'); return 'Basic ' + this.Buffer('OPENVIDUAPP:' + secret).toString('base64');
} }

View File

@ -28,6 +28,9 @@ import { SessionProperties } from './SessionProperties';
import { TokenOptions } from './TokenOptions'; import { TokenOptions } from './TokenOptions';
import { RecordingProperties } from './RecordingProperties'; import { RecordingProperties } from './RecordingProperties';
import { IceServerProperties } from './IceServerProperties'; import { IceServerProperties } from './IceServerProperties';
import { OpenViduLogger } from './Logger/OpenViduLogger';
const logger: OpenViduLogger = OpenViduLogger.getInstance();
export class Session { export class Session {
/** /**
@ -245,7 +248,7 @@ export class Session {
this.resetWithJson(res.data); this.resetWithJson(res.data);
const afterJSON: string = JSON.stringify(this, this.removeCircularOpenViduReference); const afterJSON: string = JSON.stringify(this, this.removeCircularOpenViduReference);
const hasChanged: boolean = !(beforeJSON === afterJSON); const hasChanged: boolean = !(beforeJSON === afterJSON);
console.log("Session info fetched for session '" + this.sessionId + "'. Any change: " + hasChanged); logger.log("Session info fetched for session '" + this.sessionId + "'. Any change: " + hasChanged);
resolve(hasChanged); resolve(hasChanged);
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
@ -316,12 +319,12 @@ export class Session {
}); });
}); });
} else { } else {
console.warn( logger.warn(
"The closed connection wasn't fetched in OpenVidu Node Client. No changes in the collection of active connections of the Session" "The closed connection wasn't fetched in OpenVidu Node Client. No changes in the collection of active connections of the Session"
); );
} }
this.updateActiveConnectionsArray(); this.updateActiveConnectionsArray();
console.log("Connection '" + connectionId + "' closed"); logger.log("Connection '" + connectionId + "' closed");
resolve(); resolve();
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
@ -379,7 +382,7 @@ export class Session {
} }
}); });
this.updateActiveConnectionsArray(); this.updateActiveConnectionsArray();
console.log("Stream '" + streamId + "' unpublished"); logger.log("Stream '" + streamId + "' unpublished");
resolve(); resolve();
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
@ -428,7 +431,7 @@ export class Session {
}) })
.then((res) => { .then((res) => {
if (res.status === 200) { if (res.status === 200) {
console.log('Connection ' + connectionId + ' updated'); logger.log('Connection ' + connectionId + ' updated');
} else { } else {
// ERROR response from openvidu-server. Resolve HTTP status // ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString())); reject(new Error(res.status.toString()));