Browse Source

ref(LogCollector): extract JitsiMeetLogStorage

master
paweldomas 9 years ago
parent
commit
7c8ca45d9a
2 changed files with 61 additions and 20 deletions
  1. 2
    20
      app.js
  2. 59
    0
      modules/util/JitsiMeetLogStorage.js

+ 2
- 20
app.js View File

@@ -21,6 +21,7 @@ window.toastr = require("toastr");
21 21
 
22 22
 const Logger = require("jitsi-meet-logger");
23 23
 const LogCollector = Logger.LogCollector;
24
+import JitsiMeetLogStorage from "./modules/util/JitsiMeetLogStorage";
24 25
 
25 26
 import URLProcessor from "./modules/config/URLProcessor";
26 27
 import RoomnameGenerator from './modules/util/RoomnameGenerator';
@@ -163,26 +164,7 @@ const APP = {
163 164
         configureLoggingLevels();
164 165
         // Start the LogCollector and register it as the global log transport
165 166
         if (!this.logCollector && !loggingConfig.disableLogCollector) {
166
-            this.logCollector = new LogCollector({
167
-                storeLogs: (logJSON) => {
168
-                    // Try catch was used, because there are many variables
169
-                    // on the way that could be uninitialized if the storeLogs
170
-                    // attempt would be made very early (which is unlikely)
171
-                    try {
172
-                        // Currently it makes sense to store the log only
173
-                        // if CallStats is enabled
174
-                        if (APP.logCollectorStarted
175
-                                && APP.conference
176
-                                && APP.conference.isCallstatsEnabled()) {
177
-                            APP.conference.logJSON(logJSON);
178
-                        }
179
-                    } catch (error) {
180
-                        // NOTE console is intentional here
181
-                        console.error(
182
-                            "Failed to store the logs: ", logJSON, error);
183
-                    }
184
-                }
185
-            });
167
+            this.logCollector = new LogCollector(new JitsiMeetLogStorage());
186 168
             Logger.addGlobalTransport(this.logCollector);
187 169
             JitsiMeetJS.addGlobalLogTransport(this.logCollector);
188 170
         }

+ 59
- 0
modules/util/JitsiMeetLogStorage.js View File

@@ -0,0 +1,59 @@
1
+/* global APP */
2
+
3
+/**
4
+ * Implements logs storage through the CallStats.
5
+ */
6
+export default class JitsiMeetLogStorage {
7
+
8
+    /**
9
+     * Creates new <tt>JitsiMeetLogStorage</tt>
10
+     */
11
+    constructor() {
12
+        /**
13
+         * Counts each log entry, increases on every batch log entry stored.
14
+         * @type {number}
15
+         */
16
+        this.counter = 1;
17
+    }
18
+
19
+    /**
20
+     * Called by the <tt>LogCollector</tt> to store a series of log lines into
21
+     * batch.
22
+     * @param {string|object[]}logEntries an array containing strings
23
+     * representing log lines or aggregated lines objects.
24
+     */
25
+    storeLogs(logEntries) {
26
+
27
+        let logJSON = '{"log' + this.counter + '":"\n';
28
+        for (let i = 0, len = logEntries.length; i < len; i++) {
29
+            let logEntry = logEntries[i];
30
+            if (typeof logEntry === 'object') {
31
+                // Aggregated message
32
+                logJSON += '(' + logEntry.count + ') ' + logEntry.text + '\n';
33
+            } else {
34
+                // Regular message
35
+                logJSON += logEntry + '\n';
36
+            }
37
+        }
38
+        logJSON += '"}';
39
+
40
+        this.counter += 1;
41
+
42
+        // Try catch was used, because there are many variables
43
+        // on the way that could be uninitialized if the storeLogs
44
+        // attempt would be made very early (which is unlikely)
45
+        try {
46
+            // Currently it makes sense to store the log only
47
+            // if CallStats is enabled
48
+            if (APP.logCollectorStarted
49
+                    && APP.conference
50
+                    && APP.conference.isCallstatsEnabled()) {
51
+                APP.conference.logJSON(logJSON);
52
+            }
53
+        } catch (error) {
54
+            // NOTE console is intentional here
55
+            console.error(
56
+                "Failed to store the logs: ", logJSON, error);
57
+        }
58
+    }
59
+}

Loading…
Cancel
Save