openvidu-browser: Send logs for with sessionId and first connectionId. Avoid polling after unauthorized logs requests.

pull/621/head
cruizba 2021-04-08 16:17:35 +02:00
parent 74ad11dd5c
commit e3f66a75ec
1 changed files with 40 additions and 30 deletions

View File

@ -1,6 +1,7 @@
import {JL} from 'jsnlog' import {JL} from 'jsnlog'
import {OpenVidu} from "../../OpenVidu/OpenVidu"; import {OpenVidu} from "../../OpenVidu/OpenVidu";
import {OpenViduLoggerConfiguration} from "./OpenViduLoggerConfiguration"; import {OpenViduLoggerConfiguration} from "./OpenViduLoggerConfiguration";
import JSNLogAjaxAppender = JL.JSNLogAjaxAppender;
export class OpenViduLogger { export class OpenViduLogger {
@ -10,29 +11,43 @@ export class OpenViduLogger {
private MAX_JSNLOG_BATCH_LOG_MESSAGES: number = 50; private MAX_JSNLOG_BATCH_LOG_MESSAGES: number = 50;
private MAX_MSECONDS_BATCH_MESSAGES: number = 5000; private MAX_MSECONDS_BATCH_MESSAGES: number = 5000;
private openvidu: OpenVidu;
private logger: Console = window.console; private logger: Console = window.console;
private loggingSessionId: string;
private LOG_FNS = [this.logger.log, this.logger.debug, this.logger.info, this.logger.warn, this.logger.error]; private LOG_FNS = [this.logger.log, this.logger.debug, this.logger.info, this.logger.warn, this.logger.error];
private currentAppender: any;
private isProdMode = false; private isProdMode = false;
private isJSNLogEnabled = true;
private isJSNLogSetup = false; private isJSNLogSetup = false;
private customAppenders: JL.JSNLogAjaxAppender[] = [];
private constructor() {} private constructor() {}
/**
* Configure http uri to send logs using JSNlog
*/
static configureJSNLog(openVidu: OpenVidu, sessionId: string, connectionId: string, token: string) { static configureJSNLog(openVidu: OpenVidu, sessionId: string, connectionId: string, token: string) {
// If instance is not null, JSNLog is enabled and is OpenVidu Pro // If instance is created is OpenVidu Pro
if (this.instance && this.instance.isJSNLogEnabled && openVidu.webrtcStatsInterval > -1 && openVidu.sendBrowserLogs === OpenViduLoggerConfiguration.debug) { if (this.instance && openVidu.webrtcStatsInterval > -1
this.instance.info("Configuring JSNLogs."); // If logs are enabled
try { && openVidu.sendBrowserLogs === OpenViduLoggerConfiguration.debug
this.instance.openvidu = openVidu; // If diferent session or first session
&& sessionId !== this.instance.loggingSessionId) {
// Use connection id as user and token as password try {
const openViduJSNLogHeaders = (xhr) => { // isJSNLogSetup will not be true until completed setup
this.instance.isJSNLogSetup = false;
this.instance.info("Configuring JSNLogs.");
const beforeSendCallback = (xhr) => {
// If 401 or 403 or 404 modify ready and status so JSNLog don't retry to send logs
// https://github.com/mperdeck/jsnlog.js/blob/v2.30.0/jsnlog.ts#L805-L818
const parentReadyStateFunction = xhr.onreadystatechange;
xhr.onreadystatechange = () => {
if ((xhr.status == 401) || (xhr.status == 403) || (xhr.status == 404)) {
Object.defineProperty( xhr, "readyState", {value: 4});
Object.defineProperty( xhr, "status", {value: 200});
}
parentReadyStateFunction();
}
// Headers to identify and authenticate logs
xhr.setRequestHeader('Authorization', "Basic " + btoa(`${connectionId}%/%${sessionId}` + ":" + token)); xhr.setRequestHeader('Authorization', "Basic " + btoa(`${connectionId}%/%${sessionId}` + ":" + token));
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
// Additional headers for OpenVidu // Additional headers for OpenVidu
@ -41,14 +56,14 @@ export class OpenViduLogger {
xhr.setRequestHeader('OV-Token', btoa(token)); xhr.setRequestHeader('OV-Token', btoa(token));
} }
const customAppender: any = JL.createAjaxAppender("openvidu-browser-logs-appender-" + connectionId); // Creation of the appender.
customAppender.setOptions({ this.instance.currentAppender = JL.createAjaxAppender("appender-" + connectionId);
beforeSend: openViduJSNLogHeaders, this.instance.currentAppender.setOptions({
beforeSend: beforeSendCallback,
maxBatchSize: 1000, maxBatchSize: 1000,
batchSize: this.instance.MAX_JSNLOG_BATCH_LOG_MESSAGES, batchSize: this.instance.MAX_JSNLOG_BATCH_LOG_MESSAGES,
batchTimeout: this.instance.MAX_MSECONDS_BATCH_MESSAGES batchTimeout: this.instance.MAX_MSECONDS_BATCH_MESSAGES
}); });
this.instance.customAppenders.push(customAppender);
// Avoid circular dependencies // Avoid circular dependencies
const logSerializer = (obj): string => { const logSerializer = (obj): string => {
@ -69,14 +84,15 @@ export class OpenViduLogger {
// Initialize JL to send logs // Initialize JL to send logs
JL.setOptions({ JL.setOptions({
defaultAjaxUrl: OpenViduLogger.instance.openvidu.httpUri + this.instance.JSNLOG_URL, defaultAjaxUrl: openVidu.httpUri + this.instance.JSNLOG_URL,
serialize: logSerializer serialize: logSerializer
}); });
JL().setOptions({ JL().setOptions({
appenders: [customAppender] appenders: [this.instance.currentAppender]
}); });
this.instance.isJSNLogSetup = true; this.instance.isJSNLogSetup = true;
this.instance.loggingSessionId = sessionId;
this.instance.info("JSNLog configured."); this.instance.info("JSNLog configured.");
} catch (e) { } catch (e) {
console.error("Error configuring JSNLog: "); console.error("Error configuring JSNLog: ");
@ -137,10 +153,8 @@ export class OpenViduLogger {
} }
flush() { flush() {
if(this.isDebugLogEnabled()) { if(this.isDebugLogEnabled() && this.currentAppender != null) {
for(const appender of this.customAppenders) { this.currentAppender.sendBatch();
if (appender.sendBatch) appender.sendBatch();
}
} }
} }
@ -148,12 +162,8 @@ export class OpenViduLogger {
this.isProdMode = true; this.isProdMode = true;
} }
disableBrowserLogsMonitoring() {
this.isJSNLogEnabled = false;
}
private isDebugLogEnabled() { private isDebugLogEnabled() {
return this.isJSNLogEnabled && this.isJSNLogSetup; return this.isJSNLogSetup;
} }
} }