Skip to content

Commit

Permalink
Add server.forwardClientLogs config option (#1192)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1192

Add an option to disable reporting of forwarded client logs (via `WebsocketServiceInterface.onClientMessage`). In React Native, this enables user opt out from console logs in `metro.config.js`.

Resolves #484, react-native-community/cli#794.

Changelog: **[Feature]** Add `server.forwardClientLogs` config option

Reviewed By: robhogan

Differential Revision: D52768531

fbshipit-source-id: 772483e84df8e733a85540a9ecda4b977cdd2559
  • Loading branch information
huntie authored and facebook-github-bot committed Jan 23, 2024
1 parent 98b05c2 commit c892bf7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/Configuration.md
Expand Up @@ -661,6 +661,12 @@ Type: `string => string`
A function that will be called every time Metro processes a URL, after normalization of non-standard query-string delimiters using [`jsc-safe-url`](https://www.npmjs.com/package/jsc-safe-url). Metro will use the return value of this function as if it were the original URL provided by the client. This applies to all incoming HTTP requests (after any custom middleware), as well as bundle URLs in `/symbolicate` request payloads and within the hot reloading protocol.
#### `forwardClientLogs`
Type: `boolean`
Enable forwarding of `client_log` events (when client logs are [configured](https://github.com/facebook/metro/blob/614ad14a85b22958129ee94e04376b096f03ccb1/packages/metro/src/lib/createWebsocketServer.js#L20)) to the reporter. Defaults to `true`.
---
### Watcher Options
Expand Down
Expand Up @@ -100,6 +100,7 @@ Object {
},
"server": Object {
"enhanceMiddleware": [Function],
"forwardClientLogs": true,
"port": 8081,
"rewriteRequestUrl": [Function],
"unstable_serverRoot": null,
Expand Down Expand Up @@ -280,6 +281,7 @@ Object {
},
"server": Object {
"enhanceMiddleware": [Function],
"forwardClientLogs": true,
"port": 8081,
"rewriteRequestUrl": [Function],
"unstable_serverRoot": null,
Expand Down Expand Up @@ -460,6 +462,7 @@ Object {
},
"server": Object {
"enhanceMiddleware": [Function],
"forwardClientLogs": true,
"port": 8081,
"rewriteRequestUrl": [Function],
"unstable_serverRoot": null,
Expand Down Expand Up @@ -640,6 +643,7 @@ Object {
},
"server": Object {
"enhanceMiddleware": [Function],
"forwardClientLogs": true,
"port": 8081,
"rewriteRequestUrl": [Function],
"unstable_serverRoot": null,
Expand Down
1 change: 1 addition & 0 deletions packages/metro-config/src/configTypes.flow.js
Expand Up @@ -167,6 +167,7 @@ type MetalConfigT = {
type ServerConfigT = {
/** @deprecated */
enhanceMiddleware: (Middleware, MetroServer) => Middleware | Server,
forwardClientLogs: boolean,
port: number,
rewriteRequestUrl: string => string,
unstable_serverRoot: ?string,
Expand Down
1 change: 1 addition & 0 deletions packages/metro-config/src/defaults/index.js
Expand Up @@ -75,6 +75,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({

server: {
enhanceMiddleware: (middleware, _) => middleware,
forwardClientLogs: true,
port: 8081,
rewriteRequestUrl: url => url,
unstable_serverRoot: null,
Expand Down
1 change: 1 addition & 0 deletions packages/metro-config/types/configTypes.d.ts
Expand Up @@ -165,6 +165,7 @@ export interface ServerConfigT {
metroMiddleware: Middleware,
metroServer: MetroServer,
) => Middleware | Server;
forwardClientLogs: boolean;
port: number;
rewriteRequestUrl: (url: string) => string;
unstable_serverRoot: string | null;
Expand Down
14 changes: 8 additions & 6 deletions packages/metro/src/HmrServer.js
Expand Up @@ -228,12 +228,14 @@ class HmrServer<TClient: Client> {
),
);
case 'log':
this._config.reporter.update({
type: 'client_log',
level: data.level,
data: data.data,
mode: data.mode,
});
if (this._config.server.forwardClientLogs) {
this._config.reporter.update({
type: 'client_log',
level: data.level,
data: data.data,
mode: data.mode,
});
}
break;
case 'log-opt-in':
client.optedIntoHMR = true;
Expand Down

0 comments on commit c892bf7

Please sign in to comment.