diff --git a/packages/web/src/logger.ts b/packages/web/src/logger.ts index 43a1e5c01cb7..d0bd8ae24d3c 100644 --- a/packages/web/src/logger.ts +++ b/packages/web/src/logger.ts @@ -104,7 +104,8 @@ class MidwayLoggers extends Map { // 这里属于 hack 了,cluster 模式下会先走这里,找不到默认值 // 先合并一遍默认配置 configService.addObject( - loggers.getDefaultMidwayLoggerConfig(configService.getAppInfo()) + loggers.getDefaultMidwayLoggerConfig(configService.getAppInfo()), + true ); loggerConfig = configService.getConfiguration('midwayLogger'); diff --git a/site/docs/logger.md b/site/docs/logger.md index 69cc7332beab..01f45c7493d0 100644 --- a/site/docs/logger.md +++ b/site/docs/logger.md @@ -34,6 +34,8 @@ Midway 为不同场景提供了一套统一的日志接入方式。通过 `@midw 在大部分场景下,两个版本是兼容的,但是在配置中,会有一定的差异性,为此我们提供了一些方法来尽可能兼容老逻辑,完整的 Breaking Change 变化,请查看 [变更文档](https://github.com/midwayjs/logger/blob/main/BREAKING-3.md)。 +如果你的配置文件中有老的日志配置,可以参考 **常见问题** 进行转换。 + ## 日志路径和文件 @@ -936,3 +938,25 @@ const newLoggerConfig = formatLegacyLoggerOptions({ 注意,这个方法只能转换老的配置,如果配置中包含新老配置则新配置不会生效。 ::: + +比如,你的 `src/config/config.default.ts` 中如果有日志使用的是老配置,可以使用这个方法做兼容。 + +```typescript +import { MidwayConfig, MidwayAppInfo } from '@midwayjs/core'; +import { formatLegacyLoggerOptions } from '@midwayjs/logger'; + +export default (appInfo: MidwayAppInfo) => { + return { + midwayLogger: { + clients: { + abc: logger.formatLegacyLoggerOptions({ + fileLogName: 'abc.log', + }), + } + }, + // ... + } as MidwayConfig; +}; + +``` + diff --git a/site/i18n/en/docusaurus-plugin-content-docs/current/logger.md b/site/i18n/en/docusaurus-plugin-content-docs/current/logger.md index 74b18875e9d3..2648f51bec3e 100644 --- a/site/i18n/en/docusaurus-plugin-content-docs/current/logger.md +++ b/site/i18n/en/docusaurus-plugin-content-docs/current/logger.md @@ -34,6 +34,8 @@ Upgrade the dependency versions in `package.json`, pay attention to the `depende In most scenarios, the two versions are compatible, but there will be certain differences in configuration. For this reason, we have provided some methods to be as compatible with the old logic as possible. For complete Breaking Change changes, please view [the change document](https://github.com/midwayjs/logger/blob/main/BREAKING-3.md). +If you have old log configuration in your configuration file, you can refer to **Common Problem** to convert it. + ## Logger path and file @@ -897,7 +899,7 @@ export class MainConfiguration { -## Common problem +## Common Problem @@ -917,4 +919,48 @@ Generally speaking, the server console log (console) is closed and will only be ### 3. Some Docker environments fail to start -Check whether the user who started the current application in the directory where the log is written has permissions. \ No newline at end of file +Check whether the user who started the current application in the directory where the log is written has permissions. + + + +### 4. How to convert if there is an old configuration? + +The log library provides a conversion method to assist users in converting old configurations into new configurations. + +```typescript +import { formatLegacyLoggerOptions } from '@midwayjs/logger'; + +const newLoggerConfig = formatLegacyLoggerOptions({ + level: 'info', + enableFile: false, + disableConsole: true, + enableJSON: true, +}); +``` + +:::caution + +Note that this method can only convert the old configuration. If the configuration contains the old and new configurations, the new configuration will not take effect. + +::: + +For example, if there are logs in your `src/config/config.default.ts` that use old configurations, you can use this method for compatibility. + +```typescript +import { MidwayConfig, MidwayAppInfo } from '@midwayjs/core'; +import { formatLegacyLoggerOptions } from '@midwayjs/logger'; + +export default (appInfo: MidwayAppInfo) => { + return { + midwayLogger: { + clients: { + abc: logger.formatLegacyLoggerOptions({ + fileLogName: 'abc.log', + }), + } + }, + // ... + } as MidwayConfig; +}; + +``` \ No newline at end of file