From 28480ac891c4a2e1d53dd4f68f966947b026715c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 19 Mar 2021 03:59:56 -0700 Subject: [PATCH] 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 --- desktop/app/src/devices/MetroDevice.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/desktop/app/src/devices/MetroDevice.tsx b/desktop/app/src/devices/MetroDevice.tsx index c9e1ead48..848c0b747 100644 --- a/desktop/app/src/devices/MetroDevice.tsx +++ b/desktop/app/src/devices/MetroDevice.tsx @@ -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);