2021-03-31 17:12:37 +02:00
|
|
|
import {JL} from 'jsnlog'
|
|
|
|
import {OpenVidu} from "../../OpenVidu/OpenVidu";
|
2021-04-07 17:29:51 +02:00
|
|
|
import {OpenViduLoggerConfiguration} from "./OpenViduLoggerConfiguration";
|
2021-03-31 17:12:37 +02:00
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
export class OpenViduLogger {
|
|
|
|
|
|
|
|
private static instance: OpenViduLogger;
|
2021-03-31 17:12:37 +02:00
|
|
|
|
2021-04-01 03:11:45 +02:00
|
|
|
private JSNLOG_URL: string = "/openvidu/elk/openvidu-browser-logs";
|
2021-03-31 17:12:37 +02:00
|
|
|
private MAX_JSNLOG_BATCH_LOG_MESSAGES: number = 50;
|
|
|
|
private MAX_MSECONDS_BATCH_MESSAGES: number = 5000;
|
|
|
|
|
|
|
|
private openvidu: OpenVidu;
|
2020-05-04 20:01:56 +02:00
|
|
|
private logger: Console = window.console;
|
|
|
|
private LOG_FNS = [this.logger.log, this.logger.debug, this.logger.info, this.logger.warn, this.logger.error];
|
|
|
|
private isProdMode = false;
|
2021-03-31 17:12:37 +02:00
|
|
|
private isJSNLogEnabled = true;
|
|
|
|
private isJSNLogSetup = false;
|
2021-04-07 17:29:51 +02:00
|
|
|
private customAppenders: JL.JSNLogAjaxAppender[] = [];
|
2021-03-31 17:12:37 +02:00
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
|
|
|
|
private constructor() {}
|
|
|
|
|
2021-03-31 17:12:37 +02:00
|
|
|
/**
|
|
|
|
* Configure http uri to send logs using JSNlog
|
|
|
|
*/
|
|
|
|
static configureJSNLog(openVidu: OpenVidu, sessionId: string, connectionId: string, token: string) {
|
|
|
|
// If instance is not null, JSNLog is enabled and is OpenVidu Pro
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.instance && this.instance.isJSNLogEnabled && openVidu.webrtcStatsInterval > -1 && openVidu.sendBrowserLogs === OpenViduLoggerConfiguration.debug) {
|
2021-03-31 17:12:37 +02:00
|
|
|
this.instance.info("Configuring JSNLogs.");
|
|
|
|
try {
|
|
|
|
this.instance.openvidu = openVidu;
|
|
|
|
|
|
|
|
// Use connection id as user and token as password
|
|
|
|
const openViduJSNLogHeaders = (xhr) => {
|
2021-04-07 17:29:51 +02:00
|
|
|
xhr.setRequestHeader('Authorization', "Basic " + btoa(`${connectionId}%/%${sessionId}` + ":" + token));
|
2021-03-31 17:12:37 +02:00
|
|
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
|
|
|
|
// Additional headers for OpenVidu
|
2021-04-07 17:29:51 +02:00
|
|
|
xhr.setRequestHeader('OV-Connection-Id', btoa(connectionId));
|
|
|
|
xhr.setRequestHeader('OV-Session-Id', btoa(sessionId));
|
|
|
|
xhr.setRequestHeader('OV-Token', btoa(token));
|
2021-03-31 17:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const customAppender: any = JL.createAjaxAppender("openvidu-browser-logs-appender-" + connectionId);
|
|
|
|
customAppender.setOptions({
|
|
|
|
beforeSend: openViduJSNLogHeaders,
|
2021-04-07 17:29:51 +02:00
|
|
|
maxBatchSize: 1000,
|
2021-03-31 17:12:37 +02:00
|
|
|
batchSize: this.instance.MAX_JSNLOG_BATCH_LOG_MESSAGES,
|
|
|
|
batchTimeout: this.instance.MAX_MSECONDS_BATCH_MESSAGES
|
|
|
|
});
|
2021-04-07 17:29:51 +02:00
|
|
|
this.instance.customAppenders.push(customAppender);
|
2021-03-31 17:12:37 +02:00
|
|
|
|
|
|
|
// Avoid circular dependencies
|
|
|
|
const logSerializer = (obj): string => {
|
|
|
|
const getCircularReplacer = () => {
|
|
|
|
const seen = new WeakSet();
|
|
|
|
return (key, value) => {
|
|
|
|
if (typeof value === "object" && value !== null) {
|
|
|
|
if (seen.has(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seen.add(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return JSON.stringify(obj, getCircularReplacer());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize JL to send logs
|
|
|
|
JL.setOptions({
|
|
|
|
defaultAjaxUrl: OpenViduLogger.instance.openvidu.httpUri + this.instance.JSNLOG_URL,
|
|
|
|
serialize: logSerializer
|
|
|
|
});
|
|
|
|
JL().setOptions({
|
|
|
|
appenders: [customAppender]
|
|
|
|
});
|
|
|
|
|
|
|
|
this.instance.isJSNLogSetup = true;
|
|
|
|
this.instance.info("JSNLog configured.");
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error configuring JSNLog: ");
|
|
|
|
console.error(e);
|
|
|
|
this.instance.isJSNLogSetup = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
static getInstance(): OpenViduLogger {
|
|
|
|
if(!OpenViduLogger.instance){
|
|
|
|
OpenViduLogger.instance = new OpenViduLogger();
|
|
|
|
}
|
|
|
|
return OpenViduLogger.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(...args: any[]){
|
|
|
|
if (!this.isProdMode) {
|
|
|
|
this.LOG_FNS[0].apply(this.logger, arguments);
|
|
|
|
}
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.isDebugLogEnabled()) {
|
2021-03-31 17:12:37 +02:00
|
|
|
JL().info(arguments);
|
|
|
|
}
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug(...args: any[]) {
|
|
|
|
if (!this.isProdMode) {
|
|
|
|
this.LOG_FNS[1].apply(this.logger, arguments);
|
|
|
|
}
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.isDebugLogEnabled()) {
|
2021-03-31 17:12:37 +02:00
|
|
|
JL().debug(arguments);
|
|
|
|
}
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
info(...args: any[]) {
|
|
|
|
if (!this.isProdMode) {
|
|
|
|
this.LOG_FNS[2].apply(this.logger, arguments);
|
|
|
|
}
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.isDebugLogEnabled()) {
|
2021-03-31 17:12:37 +02:00
|
|
|
JL().info(arguments);
|
|
|
|
}
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
warn(...args: any[]) {
|
|
|
|
if (!this.isProdMode) {
|
|
|
|
this.LOG_FNS[3].apply(this.logger, arguments);
|
|
|
|
}
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.isDebugLogEnabled()) {
|
2021-03-31 17:12:37 +02:00
|
|
|
JL().warn(arguments);
|
|
|
|
}
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
error(...args: any[]) {
|
|
|
|
this.LOG_FNS[4].apply(this.logger, arguments);
|
2021-04-07 17:29:51 +02:00
|
|
|
if (this.isDebugLogEnabled()) {
|
2021-03-31 17:12:37 +02:00
|
|
|
JL().error(arguments);
|
|
|
|
}
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:29:51 +02:00
|
|
|
flush() {
|
|
|
|
if(this.isDebugLogEnabled()) {
|
|
|
|
for(const appender of this.customAppenders) {
|
|
|
|
if (appender.sendBatch) appender.sendBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
enableProdMode(){
|
|
|
|
this.isProdMode = true;
|
|
|
|
}
|
2021-03-31 17:12:37 +02:00
|
|
|
|
|
|
|
disableBrowserLogsMonitoring() {
|
|
|
|
this.isJSNLogEnabled = false;
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:29:51 +02:00
|
|
|
private isDebugLogEnabled() {
|
2021-03-31 17:12:37 +02:00
|
|
|
return this.isJSNLogEnabled && this.isJSNLogSetup;
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
}
|