mirror of https://github.com/OpenVidu/openvidu.git
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 logspull/780/head
parent
027e85052a
commit
f3550c4f6a
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import { OpenViduLogger } from './Logger/OpenViduLogger';
|
||||
import { Connection } from './Connection';
|
||||
import { Recording } from './Recording';
|
||||
import { RecordingProperties } from './RecordingProperties';
|
||||
|
@ -29,6 +30,8 @@ interface ObjMap<T> {
|
|||
[s: string]: T;
|
||||
}
|
||||
|
||||
const logger: OpenViduLogger = OpenViduLogger.getInstance();
|
||||
|
||||
export class OpenVidu {
|
||||
private Buffer = require('buffer/').Buffer;
|
||||
|
||||
|
@ -199,7 +202,7 @@ export class OpenVidu {
|
|||
if (!!activeSession) {
|
||||
activeSession.recording = true;
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
"No active session found for sessionId '" +
|
||||
r.sessionId +
|
||||
"'. This instance of OpenVidu Node Client didn't create this session"
|
||||
|
@ -244,7 +247,7 @@ export class OpenVidu {
|
|||
if (!!activeSession) {
|
||||
activeSession.recording = false;
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
"No active session found for sessionId '" +
|
||||
r.sessionId +
|
||||
"'. This instance of OpenVidu Node Client didn't create this session"
|
||||
|
@ -424,7 +427,7 @@ export class OpenVidu {
|
|||
if (!!activeSession) {
|
||||
activeSession.broadcasting = true;
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
"No active session found for sessionId '" +
|
||||
sessionId +
|
||||
"'. This instance of OpenVidu Node Client didn't create this session"
|
||||
|
@ -472,7 +475,7 @@ export class OpenVidu {
|
|||
if (!!activeSession) {
|
||||
activeSession.broadcasting = false;
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
"No active session found for sessionId '" +
|
||||
sessionId +
|
||||
"'. This instance of OpenVidu Node Client didn't create this session"
|
||||
|
@ -521,12 +524,12 @@ export class OpenVidu {
|
|||
// 2. Update existing Session
|
||||
const changed: boolean = !storedSession.equalTo(fetchedSession);
|
||||
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;
|
||||
} else {
|
||||
// 3. Add new Session
|
||||
this.activeSessions.push(fetchedSession);
|
||||
console.log("New session '" + fetchedSession.sessionId + "' info fetched");
|
||||
logger.log("New session '" + fetchedSession.sessionId + "' info fetched");
|
||||
hasChanged = true;
|
||||
}
|
||||
});
|
||||
|
@ -535,13 +538,13 @@ export class OpenVidu {
|
|||
for (var i = this.activeSessions.length - 1; i >= 0; --i) {
|
||||
let sessionId = this.activeSessions[i].sessionId;
|
||||
if (!fetchedSessionIds.includes(sessionId)) {
|
||||
console.log("Removing closed session '" + sessionId + "'");
|
||||
logger.log("Removing closed session '" + sessionId + "'");
|
||||
hasChanged = true;
|
||||
this.activeSessions.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Active sessions info fetched: ', fetchedSessionIds);
|
||||
logger.log('Active sessions info fetched: ', fetchedSessionIds);
|
||||
resolve(hasChanged);
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
|
@ -671,13 +674,13 @@ export class OpenVidu {
|
|||
storedSession.connections.forEach((connection) => {
|
||||
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;
|
||||
globalChanges = globalChanges || changed;
|
||||
} else {
|
||||
// 3. Add new Session
|
||||
this.activeSessions.push(fetchedSession);
|
||||
console.log("New session '" + fetchedSession.sessionId + "' info fetched");
|
||||
logger.log("New session '" + fetchedSession.sessionId + "' info fetched");
|
||||
sessionChanges[fetchedSession.sessionId] = true;
|
||||
globalChanges = true;
|
||||
}
|
||||
|
@ -687,14 +690,14 @@ export class OpenVidu {
|
|||
for (var i = this.activeSessions.length - 1; i >= 0; --i) {
|
||||
let sessionId = this.activeSessions[i].sessionId;
|
||||
if (!fetchedSessionIds.includes(sessionId)) {
|
||||
console.log("Removing closed session '" + sessionId + "'");
|
||||
logger.log("Removing closed session '" + sessionId + "'");
|
||||
sessionChanges[sessionId] = true;
|
||||
globalChanges = true;
|
||||
this.activeSessions.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Active sessions info fetched: ', fetchedSessionIds);
|
||||
logger.log('Active sessions info fetched: ', fetchedSessionIds);
|
||||
resolve({ changes: globalChanges, sessionChanges });
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
|
@ -708,6 +711,13 @@ export class OpenVidu {
|
|||
}
|
||||
// tslint:enable:no-string-literal
|
||||
|
||||
/**
|
||||
* Disable all logging except error level
|
||||
*/
|
||||
enableProdMode(): void {
|
||||
logger.enableProdMode();
|
||||
}
|
||||
|
||||
private getBasicAuth(secret: string): string {
|
||||
return 'Basic ' + this.Buffer('OPENVIDUAPP:' + secret).toString('base64');
|
||||
}
|
||||
|
|
|
@ -28,6 +28,9 @@ import { SessionProperties } from './SessionProperties';
|
|||
import { TokenOptions } from './TokenOptions';
|
||||
import { RecordingProperties } from './RecordingProperties';
|
||||
import { IceServerProperties } from './IceServerProperties';
|
||||
import { OpenViduLogger } from './Logger/OpenViduLogger';
|
||||
|
||||
const logger: OpenViduLogger = OpenViduLogger.getInstance();
|
||||
|
||||
export class Session {
|
||||
/**
|
||||
|
@ -245,7 +248,7 @@ export class Session {
|
|||
this.resetWithJson(res.data);
|
||||
const afterJSON: string = JSON.stringify(this, this.removeCircularOpenViduReference);
|
||||
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);
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
|
@ -316,12 +319,12 @@ export class Session {
|
|||
});
|
||||
});
|
||||
} 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"
|
||||
);
|
||||
}
|
||||
this.updateActiveConnectionsArray();
|
||||
console.log("Connection '" + connectionId + "' closed");
|
||||
logger.log("Connection '" + connectionId + "' closed");
|
||||
resolve();
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
|
@ -379,7 +382,7 @@ export class Session {
|
|||
}
|
||||
});
|
||||
this.updateActiveConnectionsArray();
|
||||
console.log("Stream '" + streamId + "' unpublished");
|
||||
logger.log("Stream '" + streamId + "' unpublished");
|
||||
resolve();
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
|
@ -428,7 +431,7 @@ export class Session {
|
|||
})
|
||||
.then((res) => {
|
||||
if (res.status === 200) {
|
||||
console.log('Connection ' + connectionId + ' updated');
|
||||
logger.log('Connection ' + connectionId + ' updated');
|
||||
} else {
|
||||
// ERROR response from openvidu-server. Resolve HTTP status
|
||||
reject(new Error(res.status.toString()));
|
||||
|
|
Loading…
Reference in New Issue