Express uses the debug module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle.Express在内部使用debug模块记录有关路由匹配、正在使用的中间件功能、应用程序模式和请求-响应周期流的信息。
debug
is like an augmented version of console.log
, but unlike console.log
, you don’t have to comment out debug
logs in production code. debug
类似于console.log
的扩展版本,但与console.log
不同,您不必在生产代码中注释掉调试日志。Logging is turned off by default and can be conditionally turned on by using the 默认情况下,日志记录是关闭的,并且可以使用DEBUG
environment variable.DEBUG
环境变量有条件地打开日志记录。
To see all the internal logs used in Express, set the 要查看Express中使用的所有内部日志,请在启动应用程序时将DEBUG
environment variable to express:*
when launching your app.DEBUG
环境变量设置为express:*
。
$ DEBUG=express:* node index.js
On Windows, use the corresponding command.在Windows上,使用相应的命令。
> set DEBUG=express:* & node index.js
Running this command on the default app generated by the express generator prints the following output:在express generator生成的默认应用程序上运行此命令将打印以下输出:
$ DEBUG=express:* node ./bin/www
express:router:route new / +0ms
express:router:layer new / +1ms
express:router:route get / +1ms
express:router:layer new / +0ms
express:router:route new / +1ms
express:router:layer new / +0ms
express:router:route get / +0ms
express:router:layer new / +0ms
express:application compile etag weak +1ms
express:application compile query parser extended +0ms
express:application compile trust proxy false +0ms
express:application booting in development mode +1ms
express:router use / query +0ms
express:router:layer new / +0ms
express:router use / expressInit +0ms
express:router:layer new / +0ms
express:router use / favicon +1ms
express:router:layer new / +0ms
express:router use / logger +0ms
express:router:layer new / +0ms
express:router use / jsonParser +0ms
express:router:layer new / +1ms
express:router use / urlencodedParser +0ms
express:router:layer new / +0ms
express:router use / cookieParser +0ms
express:router:layer new / +0ms
express:router use / stylus +90ms
express:router:layer new / +0ms
express:router use / serveStatic +0ms
express:router:layer new / +0ms
express:router use / router +0ms
express:router:layer new / +1ms
express:router use /users router +0ms
express:router:layer new /users +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
When a request is then made to the app, you will see the logs specified in the Express code:当向应用程序发出请求时,您将看到Express代码中指定的日志:
express:router dispatching GET / +4h
express:router query : / +2ms
express:router expressInit : / +0ms
express:router favicon : / +0ms
express:router logger : / +1ms
express:router jsonParser : / +0ms
express:router urlencodedParser : / +1ms
express:router cookieParser : / +0ms
express:router stylus : / +0ms
express:router serveStatic : / +2ms
express:router router : / +2ms
express:router dispatching GET / +1ms
express:view lookup "index.pug" +338ms
express:view stat "/projects/example/views/index.pug" +0ms
express:view render "/projects/example/views/index.pug" +1ms
To see the logs only from the router implementation set the value of 要仅从路由器实现查看日志,请将DEBUG
to express:router
. DEBUG
的值设置为express:router
。Likewise, to see logs only from the application implementation set the value of 同样,要仅从应用程序实现中查看日志,请将DEBUG
to express:application
, and so on.DEBUG
的值设置为express:application
,依此类推。
express
express
生成的应用程序An application generated by the express
command also uses the debug
module and its debug namespace is scoped to the name of the application.express
命令生成的应用程序也使用debug
模块,其调试命名空间的作用域为应用程序的名称。
For example, if you generated the app with 例如,如果使用$ express sample-app
, you can enable the debug statements with the following command:$ express sample-app
生成应用程序,则可以使用以下命令启用调试语句:
$ DEBUG=sample-app:* node ./bin/www
You can specify more than one debug namespace by assigning a comma-separated list of names:通过指定逗号分隔的名称列表,可以指定多个调试命名空间:
$ DEBUG=http,mail,express:* node index.js
When running through Node.js, you can set a few environment variables that will change the behavior of the debug logging:在运行Node.js时,可以设置一些环境变量,这些变量将更改调试日志记录的行为:
DEBUG |
|
DEBUG_COLORS |
|
DEBUG_DEPTH |
|
DEBUG_FD |
|
DEBUG_SHOW_HIDDEN |
Note: The environment variables beginning with 以DEBUG_
end up being converted into an Options object that gets used with %o
/%O
formatters. DEBUG_
开头的环境变量最终被转换为一个Options对象,该对象将与%o
/%O
格式化程序一起使用。See the Node.js documentation for 有关完整列表,请参阅util.inspect()
for the complete list.util.inspect()
的Node.js文档。
For more information about 有关debug
, see the debug.debug
的更多信息,请参阅debug。