Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add core/logger.md #204

Merged
merged 1 commit into from
Jan 8, 2017
Merged

docs: add core/logger.md #204

merged 1 commit into from
Jan 8, 2017

Conversation

shaoshuai0102
Copy link
Contributor

No description provided.

@mention-bot
Copy link

@shaoshuai0102, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fengmk2, @popomore and @dead-horse to be potential reviewers.

## 日志路径

- 所有日志文件默认都放在 `${appInfo.root}/logs/${appInfo.name}` 路径下,例如 `/home/admin/logs/example-app`。
- 在本地开发环境(env: local) 和单元测试环境(env: unittest),为了避免冲突以及集中管理,日志会打印在项目目录下的 logs 目录,例如 `/path/to/example-app/logs/example-app`。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 serverEnv

如果想自定义日志路径:

```js
// config/config.${env}.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- config.${env}.js
+ config.${serverEnv}.js

};
```

可通过 `app.loggers.xxLogger` 获取,最终的打印结果和 coreLogger 类似。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以用 app.getLogger('xxLogger')

ctx.logger.warn('WARNNING!!!!');

// 错误日志记录,直接会将错误日志完整堆栈信息记录下来,并且输出到 errorLog 中
ctx.logger.error(someErrorObj);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加个 new Error,然后�强调下为何用 error 实例

ctx.logger.error(someErrorObj);
```

对于框架开发者和插件开发者会使用到的 ctx logger 还有 `ctx.coreLogger`。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 ctx logger 要不要用 context,只有在代码里叫 ctx.xxx

app.coreLogger.info('启动耗时 %d ms', Date.now() - start);
```

### agent logger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent 感觉这里不用说明

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里简单提了下,给 plugin 编写减少点负担吧


默认只会输出 `INFO` 及以上(`WARN` 和 `ERROR`)的日志到终端中。

- `logger.consoleLevel` (可选): 输出到终端日志的级别,默认为 `INFO`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应用启动后会关闭。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不提了,太细节了

### 高级自定义日志

日志默认是打印到日志文件中,当本地开发时同时会打印到终端。
但是,有时候我们会有需求把日志打印到其他媒介上,这时候我们就需要自定义日志的 transport。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个没有空行就接着上面写吧

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以介绍下 transport,transport 是一种传输通道,一个 logger 可包含多个传输通道。比如默认的 logger 就有 fileTransport 和 consoleTransport

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加了


### 按照文件大小切割

我们也可以按照文件大小进行切割。例如,当文件超过 50G 时进行切割。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

50G 好大




更多详细配置请参考 [egg-logger](https://github.com/eggjs/egg-logrotator)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个链接是不是错了

@popomore popomore mentioned this pull request Jan 8, 2017
31 tasks
Copy link
Member

@popomore popomore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -134,7 +134,6 @@ module.exports = appInfo => {
* logger options
* @member Config#logger
* @property {String} dir - 日志存储目录
* @property {String} rotateLogDirs - 自动按日切割的目录
* @property {String} encoding - 日志文件编码,预发和生产环境默认是 gbk,其他环境是 utf8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些注释都改成英文?

encoding 默认都是 utf8的,不会有 gbk

app.logger.warn('warning!');

// 错误日志记录,直接会将错误日志完整堆栈信息记录下来,并且输出到 errorLog 中
// 为了保证异常可追踪,必须保证所有抛出的异常都是 Error 类型,因为只有 Error 类型才会带上堆栈信息,定位到问题。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句重复的应该不需要写了,上面有说过,读者会知道的。

agent.logger.warn('warning!');

// 错误日志记录,直接会将错误日志完整堆栈信息记录下来,并且输出到 errorLog 中
// 为了保证异常可追踪,必须保证所有抛出的异常都是 Error 类型,因为只有 Error 类型才会带上堆栈信息,定位到问题。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,去掉

agent.logger.error(someErrorObj);
```

详细了解 agent 进程,请参考插件开发文档。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“如需详细了解 agent 进程,请参考插件开发文档。”


日志对于 web 开发的重要性毋庸置疑,它对于监控应用的运行状态、问题排查等都有非常重要的意义。

框架内置了强大的企业级日志支持,由 [egg-logger](https://github.com/eggjs/egg-logger) 插件提供。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模块


### 文件日志级别

默认只会输出 `INFO` 及以上(`WARN` 和 `ERROR`)的日志到文件中。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用中文的()


### 终端日志级别

默认只会输出 `INFO` 及以上(`WARN` 和 `ERROR`)的日志到终端中。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

let log;
if (args[0] instanceof Error) {
const err = args[0];
log util.format('%s: %s\n%s\npid: %s\n', err.name, err.message, err.stack, process.pid);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log =

const err = args[0];
log util.format('%s: %s\n%s\npid: %s\n', err.name, err.message, err.stack, process.pid);
} else {
log = util.format.apply(null, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log = util.format(...args);

log = util.format.apply(null, args);
}
co(function* () {
yield app.curl('http://url/to/remote/error/log/service/logs', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app 怎么来的?




更多详细配置请参考 [egg-logger](https://github.com/eggjs/egg-logger) 和 [egg-logrotator](https://github.com/eggjs/egg-logrotator)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更多详细信息请参考,不要说配置。

@codecov-io
Copy link

codecov-io commented Jan 8, 2017

Current coverage is 97.65% (diff: 100%)

No coverage report found for master at b72e9df.

Powered by Codecov. Last update b72e9df...b6482fd

@shaoshuai0102 shaoshuai0102 force-pushed the doc-core-logger branch 2 times, most recently from 15eb433 to e4919ed Compare January 8, 2017 15:46
@shaoshuai0102
Copy link
Contributor Author

done 再看看

@@ -1,4 +1,4 @@
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要删除?

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fengmk2 fengmk2 merged commit cbf1ee8 into master Jan 8, 2017
@fengmk2 fengmk2 deleted the doc-core-logger branch January 8, 2017 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants