-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
@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. |
b6482fd
to
645d423
Compare
645d423
to
72a1f6d
Compare
72a1f6d
to
b4fce85
Compare
## 日志路径 | ||
|
||
- 所有日志文件默认都放在 `${appInfo.root}/logs/${appInfo.name}` 路径下,例如 `/home/admin/logs/example-app`。 | ||
- 在本地开发环境(env: local) 和单元测试环境(env: unittest),为了避免冲突以及集中管理,日志会打印在项目目录下的 logs 目录,例如 `/path/to/example-app/logs/example-app`。 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 类似。 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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`。 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agent 感觉这里不用说明
There was a problem hiding this comment.
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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个应用启动后会关闭。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不提了,太细节了
### 高级自定义日志 | ||
|
||
日志默认是打印到日志文件中,当本地开发时同时会打印到终端。 | ||
但是,有时候我们会有需求把日志打印到其他媒介上,这时候我们就需要自定义日志的 transport。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个没有空行就接着上面写吧
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加了
|
||
### 按照文件大小切割 | ||
|
||
我们也可以按照文件大小进行切割。例如,当文件超过 50G 时进行切割。 |
There was a problem hiding this comment.
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)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个链接是不是错了
b4fce85
to
b24326e
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 类型才会带上堆栈信息,定位到问题。 |
There was a problem hiding this comment.
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 类型才会带上堆栈信息,定位到问题。 |
There was a problem hiding this comment.
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 进程,请参考插件开发文档。 |
There was a problem hiding this comment.
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) 插件提供。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
模块
|
||
### 文件日志级别 | ||
|
||
默认只会输出 `INFO` 及以上(`WARN` 和 `ERROR`)的日志到文件中。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用中文的()
|
||
### 终端日志级别 | ||
|
||
默认只会输出 `INFO` 及以上(`WARN` 和 `ERROR`)的日志到终端中。 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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', { |
There was a problem hiding this comment.
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)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
更多详细信息请参考,不要说配置。
Current coverage is 97.65% (diff: 100%)
|
15eb433
to
e4919ed
Compare
done 再看看 |
@@ -1,4 +1,4 @@ | |||
'use strict'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥要删除?
e4919ed
to
e7c4ce7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.