Skip to content

Commit a2c9afc

Browse files
atian25whxaxes
authored andcommitted
feat: prefer log file to be relative (#2)
1 parent cb70781 commit a2c9afc

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

docs/zh/guide/logger.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,16 @@ module.exports = appInfo => {
226226
// 自定义日志
227227
config.customLogger = {
228228
oneLogger: {
229-
file: path.join(appInfo.root, 'logs', appInfo.name, 'one.log'),
229+
file: 'one.log',
230230
},
231231
};
232232

233233
return config;
234234
};
235235
```
236236

237+
如果配置为文件名,则会自动转换为 `path.join(this.app.config.logger.dir, file)`
238+
237239
然后可通过 `app.getLogger('oneLogger')` / `ctx.getLogger('oneLogger')` 获取,获取到的 logger 会使用对应的 `Logger` 配置,并以 `config.logger` 为默认值。
238240

239241
::: tip 注意
@@ -248,7 +250,7 @@ module.exports = appInfo => {
248250
// config/config.default.js
249251
config.customLogger = {
250252
oneLogger: {
251-
file: path.join(appInfo.root, 'logs', appInfo.name, 'one.log'),
253+
file: 'one.log',
252254
formatter(meta) {
253255
const { level, date, pid, message } = meta;
254256
return `[${date}] [${level}] [${pid}] ${message}`;
@@ -335,7 +337,7 @@ module.exports = appInfo => {
335337

336338
config.logrotator = {
337339
filesRotateBySize: [
338-
path.join(appInfo.root, 'logs', appInfo.name, 'egg-web.log'),
340+
'egg-web.log',
339341
],
340342
maxFileSize: 2 * 1024 * 1024 * 1024,
341343
};
@@ -346,6 +348,8 @@ module.exports = appInfo => {
346348

347349
添加到 `filesRotateBySize` 的日志文件不再按天进行切割。
348350

351+
如果配置为文件名,则会自动转换为 `path.join(this.app.config.logger.dir, file)`
352+
349353
### 按照小时切割
350354

351355
我们也可以选择按照小时进行切割,这和默认的按天切割非常类似,只是时间缩短到每小时。
@@ -360,7 +364,7 @@ module.exports = appInfo => {
360364
return {
361365
logrotator: {
362366
filesRotateByHour: [
363-
path.join(appInfo.root, 'logs', appInfo.name, 'common-error.log'),
367+
'common-error.log',
364368
],
365369
},
366370
};
@@ -369,6 +373,8 @@ module.exports = appInfo => {
369373

370374
添加到 `filesRotateByHour` 的日志文件不再被按天进行切割。
371375

376+
如果配置为文件名,则会自动转换为 `path.join(this.app.config.logger.dir, file)`
377+
372378
## 编写测试
373379

374380
框架提供了 `expectLog()``mockLog()` 来简化测试工作。

0 commit comments

Comments
 (0)