Skip to content

Commit

Permalink
chore: unify logger (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 authored Oct 28, 2024
1 parent 86b4cdc commit ad605d2
Show file tree
Hide file tree
Showing 39 changed files with 229 additions and 199 deletions.
17 changes: 17 additions & 0 deletions .changeset/fast-apples-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@module-federation/bridge-shared': patch
'@module-federation/bridge-react': patch
'@module-federation/bridge-vue3': patch
'@module-federation/rsbuild-plugin': patch
'@module-federation/data-prefetch': patch
'@module-federation/retry-plugin': patch
'@module-federation/dts-plugin': patch
'@module-federation/enhanced': patch
'@module-federation/managers': patch
'@module-federation/manifest': patch
'@module-federation/modern-js': patch
'@module-federation/runtime': patch
'@module-federation/sdk': patch
---

chore: unified logger
1 change: 1 addition & 0 deletions packages/bridge/bridge-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"@loadable/component": "^5.16.4",
"@module-federation/bridge-shared": "workspace:*",
"@module-federation/sdk": "workspace:*",
"react-error-boundary": "^4.0.13"
},
"peerDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions packages/bridge/bridge-react/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { Logger } from '@module-federation/bridge-shared';
import { createLogger } from '@module-federation/sdk';

export const LoggerInstance = new Logger('bridge-react');
export const LoggerInstance = createLogger(
'[ Module Federation Bridge React ]',
);

type typeReact = typeof React;

Expand Down
1 change: 0 additions & 1 deletion packages/bridge/bridge-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export type { RenderFnParams, ProviderParams } from './type';
export { Logger } from './logger';
export { dispatchPopstateEnv } from './env';
52 changes: 0 additions & 52 deletions packages/bridge/bridge-shared/src/logger.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/bridge/vue3-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"vue-router": "=3"
},
"dependencies": {
"@module-federation/bridge-shared": "workspace:*"
"@module-federation/bridge-shared": "workspace:*",
"@module-federation/sdk": "workspace:*"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge/vue3-bridge/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Logger } from '@module-federation/bridge-shared';
import { createLogger } from '@module-federation/sdk';

export const LoggerInstance = new Logger('vue3-bridge');
export const LoggerInstance = createLogger('[ Module Federation Bridge Vue3 ]');
6 changes: 4 additions & 2 deletions packages/data-prefetch/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger } from '@module-federation/sdk';
import { createLogger } from '@module-federation/sdk';

export default new Logger('[Module Federation Data Prefetch]');
const logger = createLogger('[ Module Federation Data Prefetch ]');

export default logger;
10 changes: 4 additions & 6 deletions packages/dts-plugin/src/core/lib/DTSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
REMOTE_ALIAS_IDENTIFIER,
HOST_API_TYPES_FILE_NAME,
} from '../constant';
import { fileLog } from '../../server';
import { fileLog, logger } from '../../server';
import { axiosGet, cloneDeepOptions, isDebugMode } from './utils';
import { UpdateMode } from '../../server/constant';

Expand Down Expand Up @@ -163,12 +163,10 @@ class DTSManager {
console.error(err);
}
}
console.log(ansiColors.green('Federated types created correctly'));
logger.success('Federated types created correctly');
} catch (error) {
if (this.options.remote?.abortOnError === false) {
console.error(
ansiColors.red(`Unable to compile federated types, ${error}`),
);
logger.error(`Unable to compile federated types, ${error}`);
} else {
throw error;
}
Expand Down Expand Up @@ -406,7 +404,7 @@ class DTSManager {
this.consumeAPITypes(hostOptions);
}

console.log(ansiColors.green('Federated types extraction completed'));
logger.success('Federated types extraction completed');
} catch (err) {
if (this.options.host?.abortOnError === false) {
fileLog(
Expand Down
21 changes: 11 additions & 10 deletions packages/dts-plugin/src/plugins/DevPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WEB_CLIENT_OPTIONS_IDENTIFIER,
WebClientOptions,
getIPV4,
logger,
} from '../server';
import type { Compiler, WebpackPluginInstance } from 'webpack';
import path from 'path';
Expand Down Expand Up @@ -56,32 +57,32 @@ export class DevPlugin implements WebpackPluginInstance {

private _stopWhenSIGTERMOrSIGINT(): void {
process.on('SIGTERM', () => {
console.log(
chalk`{cyan ${this._options.name} Process(${process.pid}) SIGTERM, mf server will exit...}`,
logger.info(
`${this._options.name} Process(${process.pid}) SIGTERM, mf server will exit...`,
);
this._exit(PROCESS_EXIT_CODE.SUCCESS);
});

process.on('SIGINT', () => {
console.log(
chalk`{cyan ${this._options.name} Process(${process.pid}) SIGINT, mf server will exit...}`,
logger.info(
`${this._options.name} Process(${process.pid}) SIGINT, mf server will exit...`,
);
this._exit(PROCESS_EXIT_CODE.SUCCESS);
});
}

private _handleUnexpectedExit(): void {
process.on('unhandledRejection', (error) => {
console.error('Unhandled Rejection Error: ', error);
console.log(
chalk`{cyan ${this._options.name} Process(${process.pid}) unhandledRejection, mf server will exit...}`,
logger.error(error);
logger.error(
`Process(${process.pid}) unhandledRejection, mf server will exit...`,
);
this._exit(PROCESS_EXIT_CODE.FAILURE);
});
process.on('uncaughtException', (error) => {
console.error('Unhandled Rejection Error: ', error);
console.log(
chalk`{cyan ${this._options.name} Process(${process.pid}) uncaughtException, mf server will exit...}`,
logger.error(error);
logger.error(
`Process(${process.pid}) uncaughtException, mf server will exit...`,
);
this._exit(PROCESS_EXIT_CODE.FAILURE);
});
Expand Down
5 changes: 1 addition & 4 deletions packages/dts-plugin/src/server/WebClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import WebSocket from 'isomorphic-ws';
import {
DEFAULT_WEB_SOCKET_PORT,
WEB_SOCKET_CONNECT_MAGIC_ID,
} from './constant';
import { DEFAULT_WEB_SOCKET_PORT } from './constant';
import { Message } from './message/Message';
import { AddWebClientAction } from './message/Action';
import { APIKind, ReloadWebClientAPI } from './message/API';
Expand Down
8 changes: 5 additions & 3 deletions packages/dts-plugin/src/server/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { logger } from '@module-federation/sdk';
import { createLogger } from '@module-federation/sdk';
import * as log4js from 'log4js';
import chalk from 'chalk';
import { MF_SERVER_IDENTIFIER } from '../constant';
import { ActionKind } from '../message/Action';

const logger = createLogger(`[ ${MF_SERVER_IDENTIFIER} ]`);

function log(msg: string): void {
logger.info(chalk`{bold {greenBright [ ${MF_SERVER_IDENTIFIER} ]} ${msg}}`);
logger.info(msg);
}

function fileLog(msg: string, module: string, level: string) {
Expand Down Expand Up @@ -35,4 +37,4 @@ function error(error: unknown, action: ActionKind, from: string): string {
return err.toString();
}

export { log, fileLog, error };
export { log, fileLog, error, logger };
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { StatsPlugin } from '@module-federation/manifest';
import {
composeKeyWithSeparator,
type moduleFederationPlugin,
logger,
} from '@module-federation/sdk';
import { PrefetchPlugin } from '@module-federation/data-prefetch/cli';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
Expand Down Expand Up @@ -110,7 +111,7 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
if (err instanceof Error) {
err.message = `[ ModuleFederationPlugin ]: Manifest will not generate, because: ${err.message}`;
}
console.warn(err);
logger.warn(err);
disableManifest = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import path from 'path';
import { ConcatSource } from 'webpack-sources';
import { transformSync } from '@swc/core';
import { logger } from '@module-federation/sdk';

const { RuntimeModule, Template, RuntimeGlobals } = require(
normalizeWebpackPath('webpack'),
Expand Down Expand Up @@ -142,7 +143,7 @@ class CustomRuntimePlugin {
childCompiler.options.devtool = undefined;
childCompiler.options.optimization.splitChunks = false;
childCompiler.options.optimization.removeAvailableModules = true;
console.log('Creating child compiler for', this.bundlerRuntimePath);
logger.log('Creating child compiler for', this.bundlerRuntimePath);

childCompiler.hooks.thisCompilation.tap(
this.constructor.name,
Expand Down Expand Up @@ -174,7 +175,7 @@ class CustomRuntimePlugin {
onceForCompilationMap.set(compilation, source);
onceForCompilationMap.set(compiler, source);
fs.writeFileSync(outputPath, source);
console.log('got compilation asset');
logger.log('got compilation asset');
childCompilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
childCompilation.deleteAsset(file);
Expand All @@ -195,7 +196,7 @@ class CustomRuntimePlugin {
}

if (!childCompilation) {
console.warn(
logger.warn(
'Embed Federation Runtime: Child compilation is undefined',
);
return callback();
Expand All @@ -205,7 +206,7 @@ class CustomRuntimePlugin {
return callback(childCompilation.errors[0]);
}

console.log('Code built successfully');
logger.log('Code built successfully');

callback();
},
Expand Down Expand Up @@ -233,7 +234,7 @@ class CustomRuntimePlugin {
);

compilation.addRuntimeModule(chunk, runtimeModule);
console.log(`Custom runtime module added to chunk: ${chunk.name}`);
logger.log(`Custom runtime module added to chunk: ${chunk.name}`);
};
compilation.hooks.runtimeRequirementInTree
.for(federationGlobal)
Expand Down
4 changes: 2 additions & 2 deletions packages/managers/src/PKGJsonManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import finder from 'find-pkg';
import fs from 'fs';
import { MFModuleType } from '@module-federation/sdk';
import { MFModuleType, logger } from '@module-federation/sdk';

export class PKGJsonManager {
private _pkg?: Record<string, any>;
Expand All @@ -27,7 +27,7 @@ export class PKGJsonManager {
this._pkg = pkg;
return pkg;
} catch (err) {
console.error(err);
logger.error(err);
return {};
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/manifest/src/ManifestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {
Stats,
Manifest,
ManifestExpose,
StatsExpose,
StatsShared,
ManifestShared,
ManifestRemote,
StatsRemote,
moduleFederationPlugin,
encodeName,
MFPrefetchCommon,
} from '@module-federation/sdk';
import { getFileName, isDev } from './utils';
import logger from './logger';
import type { Compilation, Compiler } from 'webpack';
import { PLUGIN_IDENTIFIER } from './constants';
import { ManifestInfo } from './types';
Expand Down Expand Up @@ -145,10 +143,12 @@ class ManifestManager {
}

if (isDev()) {
console.log(
chalk`{bold {greenBright [ ${PLUGIN_IDENTIFIER} ]} {greenBright Manifest Link:} {cyan ${
publicPath === 'auto' ? '{auto}/' : publicPath
}${manifestFileName}}}`,
logger.info(
`Manifest Link: ${chalk.cyan(
`${
publicPath === 'auto' ? '{auto}/' : publicPath
}${manifestFileName}`,
)} `,
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/manifest/src/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable @typescript-eslint/member-ordering */
/* eslint-disable max-depth */

import chalk from 'chalk';
import {
StatsRemote,
StatsBuildInfo,
Expand Down Expand Up @@ -31,6 +30,7 @@ import {
getFileName,
getTypesMetaInfo,
} from './utils';
import logger from './logger';
import {
ContainerManager,
RemoteManager,
Expand Down Expand Up @@ -490,13 +490,13 @@ class StatsManager {
} = compiler.options;

if (typeof publicPath !== 'string') {
console.warn(
chalk`{bold {yellow [ ${PLUGIN_IDENTIFIER} ]: Manifest will not generate, because publicPath can only be string, but got '${publicPath}' }}`,
logger.warn(
`Manifest will not generate, because publicPath can only be string, but got '${publicPath}'`,
);
return false;
} else if (publicPath === 'auto') {
console.warn(
chalk`{bold {blue [ ${PLUGIN_IDENTIFIER} ]: Manifest will use absolute path resolution via its host at runtime, reason: publicPath='${publicPath}'}}`,
logger.warn(
`Manifest will use absolute path resolution via its host at runtime, reason: publicPath='${publicPath}'`,
);
return true;
}
Expand Down
Loading

0 comments on commit ad605d2

Please sign in to comment.