Skip to content

Commit d0f5b70

Browse files
committed
Idempotent logging on browser
Certain modules do not exist in a browser environment. In that environment, file-based appenders are effectively disabled by this commit, without modifying the source directly.
1 parent 66337c1 commit d0f5b70

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/appenders/ignoreBrowser.js

Whitespace-only changes.

lib/clusteringBrowser.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* istanbul ignore file */
2+
// This is used in browsers only and is designed to allow the rest of
3+
// log4js to continue as if `clustering.js` is in use.
4+
const isMaster = () => true;
5+
6+
const listeners = [];
7+
8+
const sendToListeners = (logEvent) => {
9+
listeners.forEach((l) => l(logEvent));
10+
};
11+
12+
module.exports = {
13+
onlyOnMaster: (fn, notMaster) => (isMaster() ? fn() : notMaster),
14+
isMaster,
15+
send: sendToListeners,
16+
onMessage: (listener) => {
17+
listeners.push(listener);
18+
},
19+
};

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@
7373
"typescript": "^4.9.5"
7474
},
7575
"browser": {
76-
"os": false
76+
"os": false,
77+
"streamroller": false,
78+
"./lib/clustering.js": "./lib/clusteringBrowser.js",
79+
"./lib/appenders/dateFile.js": "./lib/appenders/ignoreBrowser.js",
80+
"./lib/appenders/file.js": "./lib/appenders/ignoreBrowser.js",
81+
"./lib/appenders/fileSync.js": "./lib/appenders/ignoreBrowser.js",
82+
"./lib/appenders/multiFile.js": "./lib/appenders/ignoreBrowser.js"
7783
},
7884
"prettier": {
7985
"trailingComma": "es5",

0 commit comments

Comments
 (0)