Skip to content

Commit

Permalink
Use prettyFormat for Metro logging
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpojer authored and facebook-github-bot committed Jul 25, 2019
1 parent e42009b commit abd7faf
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const Platform = require('./Platform');
const invariant = require('invariant');
const prettyFormat = require('pretty-format');

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

Expand Down Expand Up @@ -101,16 +102,25 @@ const HMRClient: HMRClientNativeInterface = {
},

log(level: LogLevel, data: Array<mixed>) {
let message;
try {
message = JSON.stringify({type: 'log', level, data});
} catch (error) {
message = JSON.stringify({type: 'log', level, data: [error.message]});
}

try {
if (hmrClient) {
hmrClient.send(message);
hmrClient.send(
JSON.stringify({
type: 'log',
level,
data: data.map(message =>
typeof message === 'string'
? message
: prettyFormat(message, {
escapeString: true,
highlight: true,
maxDepth: 3,
min: true,
plugins: [prettyFormat.plugins.ReactElement],
}),
),
}),
);
}
} catch (error) {
// If sending logs causes any failures we want to silently ignore them
Expand Down

0 comments on commit abd7faf

Please sign in to comment.