From 7c8ca45d9a461fa57197e02e3c5778a49a056055 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 29 Nov 2016 11:28:42 -0600 Subject: [PATCH] ref(LogCollector): extract JitsiMeetLogStorage --- app.js | 22 +---------- modules/util/JitsiMeetLogStorage.js | 59 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 modules/util/JitsiMeetLogStorage.js diff --git a/app.js b/app.js index 38f19ff61..4b8da6c68 100644 --- a/app.js +++ b/app.js @@ -21,6 +21,7 @@ window.toastr = require("toastr"); const Logger = require("jitsi-meet-logger"); const LogCollector = Logger.LogCollector; +import JitsiMeetLogStorage from "./modules/util/JitsiMeetLogStorage"; import URLProcessor from "./modules/config/URLProcessor"; import RoomnameGenerator from './modules/util/RoomnameGenerator'; @@ -163,26 +164,7 @@ const APP = { configureLoggingLevels(); // Start the LogCollector and register it as the global log transport if (!this.logCollector && !loggingConfig.disableLogCollector) { - this.logCollector = new LogCollector({ - storeLogs: (logJSON) => { - // Try catch was used, because there are many variables - // on the way that could be uninitialized if the storeLogs - // attempt would be made very early (which is unlikely) - try { - // Currently it makes sense to store the log only - // if CallStats is enabled - if (APP.logCollectorStarted - && APP.conference - && APP.conference.isCallstatsEnabled()) { - APP.conference.logJSON(logJSON); - } - } catch (error) { - // NOTE console is intentional here - console.error( - "Failed to store the logs: ", logJSON, error); - } - } - }); + this.logCollector = new LogCollector(new JitsiMeetLogStorage()); Logger.addGlobalTransport(this.logCollector); JitsiMeetJS.addGlobalLogTransport(this.logCollector); } diff --git a/modules/util/JitsiMeetLogStorage.js b/modules/util/JitsiMeetLogStorage.js new file mode 100644 index 000000000..1dfa9f9e4 --- /dev/null +++ b/modules/util/JitsiMeetLogStorage.js @@ -0,0 +1,59 @@ +/* global APP */ + +/** + * Implements logs storage through the CallStats. + */ +export default class JitsiMeetLogStorage { + + /** + * Creates new JitsiMeetLogStorage + */ + constructor() { + /** + * Counts each log entry, increases on every batch log entry stored. + * @type {number} + */ + this.counter = 1; + } + + /** + * Called by the LogCollector to store a series of log lines into + * batch. + * @param {string|object[]}logEntries an array containing strings + * representing log lines or aggregated lines objects. + */ + storeLogs(logEntries) { + + let logJSON = '{"log' + this.counter + '":"\n'; + for (let i = 0, len = logEntries.length; i < len; i++) { + let logEntry = logEntries[i]; + if (typeof logEntry === 'object') { + // Aggregated message + logJSON += '(' + logEntry.count + ') ' + logEntry.text + '\n'; + } else { + // Regular message + logJSON += logEntry + '\n'; + } + } + logJSON += '"}'; + + this.counter += 1; + + // Try catch was used, because there are many variables + // on the way that could be uninitialized if the storeLogs + // attempt would be made very early (which is unlikely) + try { + // Currently it makes sense to store the log only + // if CallStats is enabled + if (APP.logCollectorStarted + && APP.conference + && APP.conference.isCallstatsEnabled()) { + APP.conference.logJSON(logJSON); + } + } catch (error) { + // NOTE console is intentional here + console.error( + "Failed to store the logs: ", logJSON, error); + } + } +}