Skip to content

Commit abd7faf

Browse files
cpojerfacebook-github-bot
authored andcommitted
Use prettyFormat for Metro logging
Summary: Right now we are using `JSON.stringify` which is very lossy with regards to JavaScript data types like functions, `undefined`, NaN and others. This diff switches the logging on the client side to use `prettyFormat` which is part of Jest. It allows to handle much richer log messages. Reviewed By: gaearon Differential Revision: D16458775 fbshipit-source-id: e1d2c125eb8357a9508521aa15510cb4f30a7fa9
1 parent e42009b commit abd7faf

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Libraries/Utilities/HMRClient.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const Platform = require('./Platform');
1313
const invariant = require('invariant');
14+
const prettyFormat = require('pretty-format');
1415

1516
const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');
1617

@@ -101,16 +102,25 @@ const HMRClient: HMRClientNativeInterface = {
101102
},
102103

103104
log(level: LogLevel, data: Array<mixed>) {
104-
let message;
105-
try {
106-
message = JSON.stringify({type: 'log', level, data});
107-
} catch (error) {
108-
message = JSON.stringify({type: 'log', level, data: [error.message]});
109-
}
110-
111105
try {
112106
if (hmrClient) {
113-
hmrClient.send(message);
107+
hmrClient.send(
108+
JSON.stringify({
109+
type: 'log',
110+
level,
111+
data: data.map(message =>
112+
typeof message === 'string'
113+
? message
114+
: prettyFormat(message, {
115+
escapeString: true,
116+
highlight: true,
117+
maxDepth: 3,
118+
min: true,
119+
plugins: [prettyFormat.plugins.ReactElement],
120+
}),
121+
),
122+
}),
123+
);
114124
}
115125
} catch (error) {
116126
// If sending logs causes any failures we want to silently ignore them

0 commit comments

Comments
 (0)