Skip to content

Commit 4ae943d

Browse files
authored
fix: add server.logger to control internal logger (#6812)
1 parent f4f1aef commit 4ae943d

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

.changeset/dry-elephants-promise.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/prod-server': patch
3+
'@modern-js/server-core': patch
4+
---
5+
6+
fix: add `server.logger` to control internal server logger
7+
fix: 添加 `server.logger` 配置来控制 Server 内部日志

packages/server/core/src/plugins/default.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,32 @@ import {
1313
} from './render';
1414

1515
export type CreateDefaultPluginsOptions = InjectRenderHandlerOptions & {
16-
logger?: Logger;
16+
logger?: Logger | false;
1717
};
1818

19+
function createSilenceLogger() {
20+
return new Proxy(
21+
{},
22+
{
23+
get: () => {
24+
return () => {
25+
// do nothing
26+
};
27+
},
28+
},
29+
) as Logger;
30+
}
31+
1932
export function createDefaultPlugins(
2033
options: CreateDefaultPluginsOptions = {},
2134
) {
2235
const plugins: ServerPlugin[] = [
2336
logPlugin(),
2437
initMonitorsPlugin(),
2538
injectRenderHandlerPlugin(options),
26-
injectloggerPluigin(options.logger),
39+
injectloggerPluigin(
40+
options.logger ? options.logger : createSilenceLogger(),
41+
),
2742
injectServerTiming(),
2843
processedByPlugin(),
2944
];

packages/server/core/src/types/config/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface ServerUserConfig {
4444
* @default false
4545
*/
4646
useJsonScript?: boolean;
47+
logger?: boolean | Record<string, unknown>;
4748
}
4849

4950
export type ServerNormalizedConfig = ServerUserConfig;

packages/server/prod-server/src/apply.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import {
1515
loadCacheConfig,
1616
serverStaticPlugin,
1717
} from '@modern-js/server-core/node';
18-
import { createLogger, isProd } from '@modern-js/utils';
18+
import { Logger, createLogger, isProd } from '@modern-js/utils';
1919
import type { ProdServerOptions } from './types';
2020

21-
function getLogger() {
21+
// Now we not use logger options, it can be implemented in the future
22+
function getLogger(_?: boolean | Record<string, unknown>) {
2223
if (process.env.DEBUG || process.env.NODE_ENV === 'production') {
2324
return createLogger({
2425
level: (process.env.MODERN_SERVER_LOG_LEVEL as any) || 'verbose',
@@ -35,7 +36,7 @@ export async function applyPlugins(
3536
options: ProdServerOptions,
3637
nodeServer?: NodeServer,
3738
) {
38-
const { pwd, appContext } = options;
39+
const { pwd, appContext, config } = options;
3940

4041
const loadCachePwd = isProd() ? pwd : appContext.appDirectory || pwd;
4142
const cacheConfig = await loadCacheConfig(loadCachePwd);
@@ -52,12 +53,13 @@ export async function applyPlugins(
5253
return c.html(createErrorHtml(500), 500);
5354
});
5455

56+
const loggerOptions = config.server.logger;
5557
const plugins = [
5658
...(nodeServer ? [injectNodeSeverPlugin({ nodeServer })] : []),
5759
...createDefaultPlugins({
5860
cacheConfig,
5961
staticGenerate: options.staticGenerate,
60-
logger: getLogger(),
62+
logger: loggerOptions === false ? false : getLogger(loggerOptions),
6163
}),
6264
...(options.plugins || []),
6365
injectResourcePlugin(),

packages/server/prod-server/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import type { Reporter } from '@modern-js/types';
77
import type { Logger } from '@modern-js/utils';
88

99
interface ProdServerExtraOptions {
10-
logger?: Logger;
11-
1210
/** compat modern.server-runtime.config.ts */
1311
serverConfigFile?: string;
1412

0 commit comments

Comments
 (0)