Skip to content

Commit c544a26

Browse files
authored
Add option to control browser log level (#579)
* Add `clientLogLevel` option and `--client-log-level` flag. This option can be `error`, `warning`, `info` or `none`. It defaults to `info`. It controls the log messages shown in the browser. * Prevent disconnected client message from appearing every two seconds
1 parent e3d9747 commit c544a26

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

bin/webpack-dev-server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ yargs.options({
6363
group: DISPLAY_GROUP,
6464
describe: "Quiet"
6565
},
66+
"client-log-level": {
67+
type: "string",
68+
group: DISPLAY_GROUP,
69+
default: "info",
70+
describe: "Log level in the browser (info, warning, error or none)"
71+
},
6672
"https": {
6773
type: "boolean",
6874
group: SSL_GROUP,
@@ -179,6 +185,9 @@ function processOptions(wpOpt) {
179185
if(!options.hotOnly)
180186
options.hotOnly = argv["hot-only"];
181187

188+
if(!options.clientLogLevel)
189+
options.clientLogLevel = argv["client-log-level"];
190+
182191
if(argv["content-base"]) {
183192
options.contentBase = argv["content-base"];
184193
if(/^[0-9]$/.test(options.contentBase))

client/index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,58 @@ if(typeof __resourceQuery === "string" && __resourceQuery) {
3030
var sock = null;
3131
var hot = false;
3232
var initial = true;
33+
var connected = false;
3334
var currentHash = "";
35+
var logLevel = "info";
36+
37+
function log(level, msg) {
38+
if(logLevel === "info" && level === "info")
39+
return console.log(msg);
40+
if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
41+
return console.warn(msg);
42+
if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
43+
return console.error(msg);
44+
}
3445

3546
var onSocketMsg = {
3647
hot: function() {
3748
hot = true;
38-
console.log("[WDS] Hot Module Replacement enabled.");
49+
log("info", "[WDS] Hot Module Replacement enabled.");
3950
},
4051
invalid: function() {
41-
console.log("[WDS] App updated. Recompiling...");
52+
log("info", "[WDS] App updated. Recompiling...");
4253
},
4354
hash: function(hash) {
4455
currentHash = hash;
4556
},
4657
"still-ok": function() {
47-
console.log("[WDS] Nothing changed.")
58+
log("info", "[WDS] Nothing changed.")
59+
},
60+
"log-level": function(level) {
61+
logLevel = level;
4862
},
4963
ok: function() {
5064
if(initial) return initial = false;
5165
reloadApp();
5266
},
5367
warnings: function(warnings) {
54-
console.log("[WDS] Warnings while compiling.");
68+
log("info", "[WDS] Warnings while compiling.");
5569
for(var i = 0; i < warnings.length; i++)
5670
console.warn(stripAnsi(warnings[i]));
5771
if(initial) return initial = false;
5872
reloadApp();
5973
},
6074
errors: function(errors) {
61-
console.log("[WDS] Errors while compiling.");
75+
log("info", "[WDS] Errors while compiling.");
6276
for(var i = 0; i < errors.length; i++)
6377
console.error(stripAnsi(errors[i]));
6478
if(initial) return initial = false;
6579
reloadApp();
6680
},
6781
"proxy-error": function(errors) {
68-
console.log("[WDS] Proxy error.");
82+
log("info", "[WDS] Proxy error.");
6983
for(var i = 0; i < errors.length; i++)
70-
console.error(stripAnsi(errors[i]));
84+
log("error", stripAnsi(errors[i]));
7185
if(initial) return initial = false;
7286
}
7387
};
@@ -82,7 +96,10 @@ var newConnection = function() {
8296
}));
8397

8498
sock.onclose = function() {
85-
console.error("[WDS] Disconnected!");
99+
if(connected)
100+
log("error", "[WDS] Disconnected!");
101+
102+
connected = false;
86103

87104
// Try to reconnect.
88105
sock = null;
@@ -92,6 +109,7 @@ var newConnection = function() {
92109
};
93110

94111
sock.onmessage = function(e) {
112+
connected = true;
95113
// This assumes that all data sent via the websocket is JSON.
96114
var msg = JSON.parse(e.data);
97115
onSocketMsg[msg.type](msg.data);
@@ -102,15 +120,15 @@ newConnection();
102120

103121
function reloadApp() {
104122
if(hot) {
105-
console.log("[WDS] App hot update...");
123+
log("info", "[WDS] App hot update...");
106124
var hotEmitter = require("webpack/hot/emitter");
107125
hotEmitter.emit("webpackHotUpdate", currentHash);
108126
if(typeof window !== "undefined") {
109127
// broadcast update to window
110128
window.postMessage("webpackHotUpdate" + currentHash, "*");
111129
}
112130
} else {
113-
console.log("[WDS] App updated. Reloading...");
131+
log("info", "[WDS] App updated. Reloading...");
114132
window.location.reload();
115133
}
116134
}

lib/Server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function Server(compiler, options) {
2121

2222
this.hot = options.hot || options.hotOnly;
2323
this.headers = options.headers;
24+
this.clientLogLevel = options.clientLogLevel;
2425
this.sockets = [];
2526

2627
// Listening for events
@@ -325,6 +326,9 @@ Server.prototype.listen = function() {
325326
}.bind(this));
326327

327328
if(this.hot) this.sockWrite([conn], "hot");
329+
330+
if(this.clientLogLevel)
331+
this.sockWrite([conn], "log-level", this.clientLogLevel);
328332
if(!this._stats) return;
329333
this._sendStats([conn], this._stats.toJson(), true);
330334
}.bind(this));

0 commit comments

Comments
 (0)