Format Metro log messages using printf format

Summary:
In browsers and Node.js, console methods accept a printf format string as a first argument:

```
console.log('Hello %s', 'world');
//> Hello world
```

This is not currently supported in Flipper, which just renders the log messages as they come from Metro (as an array of values, most often strings).

This adds support for it in Flipper by using the `util.format` function from Node.js (which is the same method used by the console under the hood).

It is implemented in Flipper and not in Metro so we have the flexibility to format the values as we want in the future (e.g.: numbers with a specific color).

Reviewed By: yungsters, mweststrate

Differential Revision: D27154864

fbshipit-source-id: e807b67900ddaf3a7e8cd86795589bed088beecd
This commit is contained in:
Rubén Norte
2021-03-19 03:59:56 -07:00
committed by Facebook GitHub Bot
parent bb827f14ea
commit 28480ac891

View File

@@ -10,6 +10,7 @@
import {LogLevel} from 'flipper-plugin';
import BaseDevice from './BaseDevice';
import {EventEmitter} from 'events';
import util from 'util';
// From xplat/js/metro/packages/metro/src/lib/reporting.js
export type BundleDetails = {
@@ -161,11 +162,11 @@ export default class MetroDevice extends BaseDevice {
tid: 0,
type,
tag: message.type,
message: message.data
.map((v) =>
message: util.format(
...message.data.map((v) =>
v && typeof v === 'object' ? JSON.stringify(v, null, 2) : v,
)
.join(' '),
),
),
});
} else {
const level = getLoglevelFromMessageType(message.type);