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(cn): translate src/content/api/logging.md #754

Merged
merged 3 commits into from
Jun 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/content/api/logging.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
---
title: Logger Interface
title: Logger 接口
sort: 6
contributors:
- EugeneHlushko
- wizardofhogwarts
---

T> Available since webpack 4.39.0
T> 本 API 从 v4.39.0 开始可用

Logging output is an additional way to display messages to the end users.
使用 Logger 输出消息是一种向用户展示信息的有效方式。

webpack logger is available to [loaders](/loaders/) and [plugins](/api/plugins/#logging). Emitting as part of the [Stats](/api/stats/) and configured by the user in [webpack configuration](/configuration/).
Webpack Logger 可以用在 [loader](/loaders/) 和 [plugin](/api/plugins/#logging)。生成的 Logger 将作为 [统计信息](/api/stats/) 的一部分进行输出,并且用户可以在 [webpack 配置文件](/configuration/) 中对 Logger 进行配置。

Benefits of custom logging API in webpack:
在 Webpack 中使用自定义 Logger API 的优点:

- Common place to [configure the logging](/configuration/stats/#statslogging) display level
- Logging output exportable as part of the `stats.json`
- Stats presets affect logging output
- Plugins can affect logging capturing and display level
- When using multiple plugins and loaders they use a common logging solution
- CLI, UI tools for webpack may choose different ways to display logging
- webpack core can emit logging output, e.g. timing data
- 可以 [配置](/configuration/stats/#statslogging) 日志的展示级别
- 日志内容可作为 `stats.json` 的一部分输出
- 统计预设会影响日志的输出
- 使用 plugin 可以影响日志的捕获与展示级别
- 当使用多个 plugin 和 loader 时,提供更加通用的日志记录解决方案
- 基于 Webpack 开发的 CLI、UI 工具等可能会选择不同的方式来展示日志
- Webpack 核心可以触发日志输出,例如:timing data

By introducing webpack logging API we hope to unify the way webpack plugins and loaders emit logs and allow better ways to inspect build problems. Integrated logging solution supports plugins and loaders developers by improving their development experience. Paves the way for non-CLI webpack solutions like dashboards or other UIs.
通过引入 Webpack Logger API,我们希望统一 Webpack plugins loaders 生成日志的方式,并提供更好的方法来检查构建问题。 集成的 Logging 解决方案可以帮助 plugins 和 loader 的开发人员提升他们的开发经验。同时为非 CLI 形式的 Webpack 解决方案构建铺平了道路,例如 dashboard 或其他 UI。

W> __Avoid noise in the log!__ Keep in mind that multiple plugins and loaders are used together. Loaders are usually processing multiple files and are invoked for every file. Choose a logging level as low as possible to keep the log output informative.
W> __避免在日志中输出无效信息!__请记住,多个 plugin 和 loader 经常一起使用。loader 通常处理多个文件,并且每个文件都会调用,所以尽可能选择较低的日志级别以保证 log 的信息量。

## Logger methods

- `logger.error(...)`: for error messages
- `logger.warn(...)`: for warnings
- `logger.info(...)`: for __important__ information messages. These messages are displayed by default. Only use this for messages that the user really needs to see
- `logger.log(...)`: for __unimportant__ information messages. These messages are displayed only when user had opted-in to see them
- `logger.debug(...)`: for debugging information. These messages are displayed only when user had opted-in to see debug logging for specific modules
- `logger.trace()`: to display a stack trace. Displayed like `logger.debug`
- `logger.group(...)`: to group messages. Displayed collapsed like `logger.log`
- `logger.groupEnd()`: to end a logging group
- `logger.groupCollapsed(...)`: to group messages together. Displayed collapsed like `logger.log`. Displayed expanded when logging level is set to `'verbose'` or `'debug'`.
- `logger.status`: writes a temporary message, setting a new status, overrides the previous one
- `logger.clear()`: to print a horizontal line. Displayed like `logger.log`
- `logger.profile(...)`, `logger.profileEnd(...)`: to capture a profile. Delegated to `console.profile` when supported
- `logger.error(...)`:用于输出错误信息
- `logger.warn(...)`:用于输出警告信息
- `logger.info(...)`:用于输出__重要__信息。默认情况下会显示这些信息,所以仅用于输出用户真正需要查看的消息
- `logger.log(...)`:用于输出__不重要__的信息。只有当用户选择查看时,才会显示
- `logger.debug(...)`:用于输出调试信息。只有当用户选择查看特定模块的调试日志时,才会显示
- `logger.trace()`:显示堆栈跟踪信息,展示形式类似于 `logger.debug`
- `logger.group(...)`:将消息进行分组,展示形式类似于 `logger.log`
- `logger.groupEnd()`:结束消息分组
- `logger.groupCollapsed(...)`:将消息进行分组。默认显示为折叠 `logger.log` 日志,当日志记录级别设置为 `'verbose'` `'debug'` 时,显示展开的日志
- `logger.status`:写入一条临时消息,并且设置新状态,覆盖上一个状态
- `logger.clear()`:打印水平线。展示形式类似于 `logger.log`
- `logger.profile(...)``logger.profileEnd(...)`:捕获配置文件。当支持 `console.profile` API 时,使用其进行输出

## Runtime Logger API

Runtime logger API is only intended to be used as a development tool, it is not intended to be included in [production mode](/configuration/mode/#mode-production).
Runtime logger API 仅应该用作开发工具,不应该包含在 [生产模式](/configuration/mode/#mode-production)中。

- `const logging = require('webpack/lib/logging/runtime')`: to use the logger in runtime, require it directly from webpack
- `logging.getLogger('name')`: to get individual logger by name
- `logging.configureDefaultLogger(...)`: to override the default logger.
- `const logging = require('webpack/lib/logging/runtime')`:直接从 Webpack 中引入即可使用 logger API
- `logging.getLogger('name')`:根据名称获取一个 logger 的实例
- `logging.configureDefaultLogger(...)`:重写 logger 的默认配置

```javascript
const logging = require('webpack/lib/logging/runtime');
Expand All @@ -57,4 +57,4 @@ logging.configureDefaultLogger({
});
```

- `logging.hooks.log`: to apply Plugins to the runtime logger
- `logging.hooks.log`:向 logger 中添加 Plugins