Skip to content

Commit

Permalink
docs(logger): formatter (#3835)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Jul 22, 2019
1 parent 383f3cf commit 79dbb14
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/source/en/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ module.exports = appInfo => {

Now, you can get loggers via `app.getLogger('xxLogger')` or `ctx.getLogger('xxLogger')`, and the logs printed from those loggers are similar to the ones from `coreLogger`.

### Custom logger formatter

```js
// config/config.${env}.js
const path = require('path');

module.exports = appInfo => {
return {
customLogger: {
xxLogger: {
file: path.join(appInfo.root, 'logs/xx.log'),
formatter(meta) {
return `[${meta.date}] ${meta.message}`;
},
// ctx logger
contextFormatter(meta) {
return `[${meta.date}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
},
},
},
};
};
```

### Advanced

Logs will be written into files by default. Further, they will also be printed into terminal in development. But what if we need to print those into another place? Creating customized transport can take you there.
Expand Down
24 changes: 24 additions & 0 deletions docs/source/zh-cn/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ module.exports = appInfo => {

可通过 `app.getLogger('xxLogger')` / `ctx.getLogger('xxLogger')` 获取,最终的打印结果和 coreLogger 类似。

### 自定义日志格式

```js
// config/config.${env}.js
const path = require('path');

module.exports = appInfo => {
return {
customLogger: {
xxLogger: {
file: path.join(appInfo.root, 'logs/xx.log'),
formatter(meta) {
return `[${meta.date}] ${meta.message}`;
},
// ctx logger
contextFormatter(meta) {
return `[${meta.date}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
},
},
},
};
};
```

### 高级自定义日志

日志默认是打印到日志文件中,当本地开发时同时会打印到终端。
Expand Down

0 comments on commit 79dbb14

Please sign in to comment.