@@ -226,14 +226,16 @@ module.exports = appInfo => {
226
226
// 自定义日志
227
227
config .customLogger = {
228
228
oneLogger: {
229
- file: path . join ( appInfo . root , ' logs ' , appInfo . name , ' one.log' ) ,
229
+ file: ' one.log' ,
230
230
},
231
231
};
232
232
233
233
return config;
234
234
};
235
235
```
236
236
237
+ 如果配置为文件名,则会自动转换为 ` path.join(this.app.config.logger.dir, file) ` 。
238
+
237
239
然后可通过 ` app.getLogger('oneLogger') ` / ` ctx.getLogger('oneLogger') ` 获取,获取到的 logger 会使用对应的 ` Logger ` 配置,并以 ` config.logger ` 为默认值。
238
240
239
241
::: tip 注意
@@ -248,7 +250,7 @@ module.exports = appInfo => {
248
250
// config/config.default.js
249
251
config .customLogger = {
250
252
oneLogger: {
251
- file: path . join ( appInfo . root , ' logs ' , appInfo . name , ' one.log' ) ,
253
+ file: ' one.log' ,
252
254
formatter (meta ) {
253
255
const { level , date , pid , message } = meta;
254
256
return ` [${ date} ] [${ level} ] [${ pid} ] ${ message} ` ;
@@ -335,7 +337,7 @@ module.exports = appInfo => {
335
337
336
338
config .logrotator = {
337
339
filesRotateBySize: [
338
- path . join ( appInfo . root , ' logs ' , appInfo . name , ' egg-web.log' ) ,
340
+ ' egg-web.log' ,
339
341
],
340
342
maxFileSize: 2 * 1024 * 1024 * 1024 ,
341
343
};
@@ -346,6 +348,8 @@ module.exports = appInfo => {
346
348
347
349
添加到 ` filesRotateBySize ` 的日志文件不再按天进行切割。
348
350
351
+ 如果配置为文件名,则会自动转换为 ` path.join(this.app.config.logger.dir, file) ` 。
352
+
349
353
### 按照小时切割
350
354
351
355
我们也可以选择按照小时进行切割,这和默认的按天切割非常类似,只是时间缩短到每小时。
@@ -360,7 +364,7 @@ module.exports = appInfo => {
360
364
return {
361
365
logrotator: {
362
366
filesRotateByHour: [
363
- path . join ( appInfo . root , ' logs ' , appInfo . name , ' common-error.log' ) ,
367
+ ' common-error.log' ,
364
368
],
365
369
},
366
370
};
@@ -369,6 +373,8 @@ module.exports = appInfo => {
369
373
370
374
添加到 ` filesRotateByHour ` 的日志文件不再被按天进行切割。
371
375
376
+ 如果配置为文件名,则会自动转换为 ` path.join(this.app.config.logger.dir, file) ` 。
377
+
372
378
## 编写测试
373
379
374
380
框架提供了 ` expectLog() ` 和 ` mockLog() ` 来简化测试工作。
0 commit comments