日志

我们为什么、记录什么以及如何记录

选择 CLI 版本:

描述

Description

npm CLI 具有多种机制,可针对某些命令、配置和环境向终端用户显示不同级别的信息。

The npm CLI has various mechanisms for showing different levels of information back to end-users for certain commands, configurations & environments.

设置日志文件位置

Setting Log File Location

所有日志都写入调试日志,如果命令执行失败,则打印该文件的路径。

All logs are written to a debug log, with the path to that file printed if the execution of a command fails.

日志目录的默认位置是 npm 缓存中名为 _logs 的目录。这可以通过 logs-dir 配置选项进行更改。

The default location of the logs directory is a directory named _logs inside the npm cache. This can be changed with the logs-dir config option.

例如,如果你想将所有日志写入当前工作目录,你可以运行:npm install --logs-dir=.。这对于调试特定的 npm 问题特别有用,因为你可以使用不同的配置值多次运行命令,然后比较所有日志文件。

For example, if you wanted to write all your logs to the current working directory, you could run: npm install --logs-dir=.. This is especially helpful in debugging a specific npm issue as you can run a command multiple times with different config values and then diff all the log files.

当日志文件数量超过 logs-max 时,日志文件将从 logs-dir 中删除,最旧的日志首先被删除。

Log files will be removed from the logs-dir when the number of log files exceeds logs-max, with the oldest logs being deleted first.

要完全关闭日志,请设置 --logs-max=0

To turn off logs completely set --logs-max=0.

设置日志级别

Setting Log Levels

loglevel

loglevel 是一个全局参数/配置,可以设置它来确定要显示的信息类型。

loglevel is a global argument/config that can be set to determine the type of information to be displayed.

loglevel 的默认值为 "notice",但有多种级别/类型的日志可用,包括:

The default value of loglevel is "notice" but there are several levels/types of logs available, including:

  • "silent"

  • "error"

  • "warn"

  • "notice"

  • "http"

  • "info"

  • "verbose"

  • "silly"

将显示与进行当前设置的级别有关的所有日志。

All logs pertaining to a level proceeding the current setting will be shown.

别名

Aliases

上面列出的日志级别有各种对应的别名,包括:

The log levels listed above have various corresponding aliases, including:

  • -d--loglevel info

  • --dd--loglevel verbose

  • --verbose--loglevel verbose

  • --ddd--loglevel silly

  • -q--loglevel warn

  • --quiet--loglevel warn

  • -s--loglevel silent

  • --silent--loglevel silent

foreground-scripts

v7 开始,npm CLI 开始隐藏 npm install 的生命周期脚本的输出。值得注意的是,这意味着你将看不到可能使用 "安装脚本" 向你显示信息的包的日志/输出,或者你自己在 package.json 中定义的项目脚本的日志/输出。如果你想更改此行为并记录此输出,你可以将 foreground-scripts 设置为 true

The npm CLI began hiding the output of lifecycle scripts for npm install as of v7. Notably, this means you will not see logs/output from packages that may be using "install scripts" to display information back to you or from your own project's scripts defined in package.json. If you'd like to change this behavior & log this output you can set foreground-scripts to true.

时间信息

Timing Information

可以设置 --timing 配置 来做一些事情:

The --timing config can be set which does a few things:

  1. 无论命令退出状态如何,始终显示调试日志的完整路径

    Always shows the full path to the debug log regardless of command exit status

  2. 将时序信息写入缓存或 logs-dir 中的进程特定时序文件

    Write timing information to a process specific timing file in the cache or logs-dir

  3. 向终端输出时序信息

    Output timing information to the terminal

该文件包含一个 timers 对象,其中键是正在计时的进程部分的标识符,值是完成所需的毫秒数。

This file contains a timers object where the keys are an identifier for the portion of the process being timed and the value is the number of milliseconds it took to complete.

有时在不向终端输出任何内容的情况下获取计时信息会很有帮助。例如,写入终端可能会影响性能。在这种情况下,你可以使用 --timing --silent,它仍然会写入计时文件,但在运行时不会向终端输出任何内容。

Sometimes it is helpful to get timing information without outputting anything to the terminal. For example, the performance might be affected by writing to the terminal. In this case you can use --timing --silent which will still write the timing file, but not output anything to the terminal while running.

注册表响应标头

Registry Response Headers

npm-notice

npm CLI 读取并记录从配置的注册表返回的任何 npm-notice 标头。第三方注册机构可以使用此机制在发生网络相关请求时提供有用的信息。

The npm CLI reads from & logs any npm-notice headers that are returned from the configured registry. This mechanism can be used by third-party registries to provide useful information when network-dependent requests occur.

此标头未缓存,如果请求是从缓存中提供的,则不会记录。

This header is not cached, and will not be logged if the request is served from the cache.

日志和敏感信息

Logs and Sensitive Information

npm CLI 尽最大努力从终端输出和日志文件中编辑以下内容:

The npm CLI makes a best effort to redact the following from terminal output and log files:

  • 基本身份验证 URL 中的密码

    Passwords inside basic auth URLs

  • npm 令牌

    npm tokens

但是,不应依赖此行为来编辑所有可能的敏感信息。如果你担心日志文件或终端输出中的秘密,你可以使用 --loglevel=silent--logs-max=0 来确保没有日志写入你的终端或文件系统。

However, this behavior should not be relied on to keep all possible sensitive information redacted. If you are concerned about secrets in your log file or terminal output, you can use --loglevel=silent and --logs-max=0 to ensure no logs are written to your terminal or filesystem.

也可以看看

See also

npm 中文网 - 粤ICP备13048890号