此内容没有您所选择的语言版本。
Chapter 4. Debugging your Node.js based application
This section contains information about debugging your Node.js–based application and using debug logging in both local and remote deployments.
4.1. Remote debugging 复制链接链接已复制到粘贴板!
To remotely debug an application, you need to start it in a debugging mode and attach a debugger to it.
The native debugger enables you to debug your Node.js–based application using the built-in debugging client.
Prerequisites
- An application you want to debug.
Procedure
Start the application with the debugger enabled.
The native debugger is automatically attached and provides a debugging prompt.
Example application with the debugger enabled
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you have a different entry point for your application, you need to change the command to specify that entry point:
node inspect path/to/entrypoint
$ node inspect path/to/entrypointCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, when using the express generator to create your application, the entry point is set to
./bin/wwwby default. Some of the examples, such as the REST API Level 0 example application, use./bin/wwwas an entry point.- Use the debugger prompt to perform debugging commands.
The V8 inspector enables you to debug your Node.js–based application using other tools, such as Chrome DevTools, that use the Chrome Debugging Protocol.
Prerequisites
- An application you want to debug.
- The V8 inspector installed, such as the one provided in the Google Chrome Browser.
Procedure
Start your application with the V8 inspector integration enabled.
node --inspect app.js
$ node --inspect app.jsCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you have a different entry point for your application, you need to change the command to specify that entry point:
node --inspect path/to/entrypoint
$ node --inspect path/to/entrypointCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, when using the express generator to create your application, the entry point is set to
./bin/wwwby default. Some of the examples, such as the REST API Level 0 example application, use./bin/wwwas an entry point.Attach the V8 inspector and perform debugging commands.
For example, if using Google Chrome:
-
Navigate to
chrome://inspect. - Select your application from below Remote Target.
- You can now see the source of your application and can perform debugging actions.
-
Navigate to
To debug your Node.js-based application on OpenShift remotely, you must set the NODE_ENV environment variable inside the container to development and configure port forwarding so that you can connect to your application from a remote debugger.
Prerequisites
- Your application running on OpenShift.
-
The
ocbinary installed. -
The ability to execute the
oc port-forwardcommand in your target OpenShift environment.
Procedure
Using the
occommand, list the available deployment configurations:oc get dc
$ oc get dcCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
NODE_ENVenvironment variable in the deployment configuration of your application todevelopmentto enable debugging. For example:oc set env dc/MY_APP_NAME NODE_ENV=development
$ oc set env dc/MY_APP_NAME NODE_ENV=developmentCopy to Clipboard Copied! Toggle word wrap Toggle overflow Redeploy the application if it is not set to redeploy automatically on configuration change. For example:
oc rollout latest dc/MY_APP_NAME
$ oc rollout latest dc/MY_APP_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure port forwarding from your local machine to the application pod:
List the currently running pods and find one containing your application:
oc get pod
$ oc get pod NAME READY STATUS RESTARTS AGE MY_APP_NAME-3-1xrsp 0/1 Running 0 6s ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure port forwarding:
oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5858
$ oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5858Copy to Clipboard Copied! Toggle word wrap Toggle overflow Here,
$LOCAL_PORT_NUMBERis an unused port number of your choice on your local machine. Remember this number for the remote debugger configuration.
Attach the V8 inspector and perform debugging commands.
For example, if using Google Chrome:
-
Navigate to
chrome://inspect. - Click Configure.
-
Add
127.0.0.1:$LOCAL_PORT_NUMBER. - Click Done.
- Select your application from below Remote Target.
- You can now see the source of your application and can perform debugging actions.
-
Navigate to
When you are done debugging, unset the
NODE_ENVenvironment variable in your application pod. For example:oc set env dc/MY_APP_NAME NODE_ENV-
$ oc set env dc/MY_APP_NAME NODE_ENV-Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2. Debug logging 复制链接链接已复制到粘贴板!
Debug logging is a way to add detailed information to the application log when debugging. This allows you to:
- Keep minimal logging output during normal operation of the application for improved readability and reduced disk space usage.
- View detailed information about the inner workings of the application when resolving issues.
4.2.1. Add debug logging 复制链接链接已复制到粘贴板!
This example uses the debug package, but there are also other packages available that can handle debug logging.
Prerequisites
- An application you want to debug. For example, an example.
Procedure
Add the
debuglogging definition.const debug = require('debug')('myexample');const debug = require('debug')('myexample');Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add debug statements.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the debug module to
package.json.... "dependencies": { "debug": "^3.1.0" }... "dependencies": { "debug": "^3.1.0" }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Depending on your application, this module may already be included. For example, when using the express generator to create your application, the
debugmodule is already added topackage.json. Some of the example applications, such as the REST API Level 0 example, already have thedebugmodule in thepackage.jsonfile.Install the application dependencies.
npm install
$ npm installCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2.2. Accessing debug logs on localhost 复制链接链接已复制到粘贴板!
Use the DEBUG environment variable when starting your application to enable debug logging.
Prerequisites
- An application with debug logging.
Procedure
Set the
DEBUGenvironment variable when starting your application to enable debug logging.DEBUG=myexample npm start
$ DEBUG=myexample npm startCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
debugmodule can use wildcards to filter debugging messages. This is set using theDEBUGenvironment variable.Test your application to invoke debug logging.
For example, when debug logging in the REST API Level 0 example is set to log the
namevariable in the/api/greetingmethod:curl http://localhost:8080/api/greeting?name=Sarah
$ curl http://localhost:8080/api/greeting?name=SarahCopy to Clipboard Copied! Toggle word wrap Toggle overflow View your application logs to see your debug messages.
myexample name: Sarah +3m
myexample name: Sarah +3mCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2.3. Accessing Node.js debug logs on OpenShift 复制链接链接已复制到粘贴板!
Use the the DEBUG environment variable in your application pod in OpenShift to enable debug logging.
Prerequisites
- An application with debug logging.
-
The
ocCLI client installed.
Procedure
Use the
ocCLI client to log into your OpenShift instance.oc login ...
$ oc login ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy your application to OpenShift.
npm run openshift
$ npm run openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow This runs the
openshiftnpm script, which wraps direct calls to nodeshift.Find the name of your pod and follow the logs to watch it start.
oc get pods oc logs -f pod/POD_NAME
$ oc get pods .... $ oc logs -f pod/POD_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantAfter your pod has started, leave this command running and execute the remaining steps in a new terminal window. This allows you to follow the logs and see new entries made to it.
Test your application.
For example, if you had debug logging in the REST API Level 0 example to log the
namevariable in the/api/greetingmethod:oc get routes curl $APPLICATION_ROUTE/api/greeting?name=Sarah
$ oc get routes ... $ curl $APPLICATION_ROUTE/api/greeting?name=SarahCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Return to your pod logs and notice there are no debug logging messages in the logs.
Set the
DEBUGenvironment variable to enable debug logging.oc get dc oc set env dc DC_NAME DEBUG=myexample
$ oc get dc ... $ oc set env dc DC_NAME DEBUG=myexampleCopy to Clipboard Copied! Toggle word wrap Toggle overflow Return to your pod logs to watch the update roll out.
After the update has rolled out, your pod will stop and you will no longer be following the logs.
Find the name of your new pod and follow the logs.
oc get pods oc logs -f pod/POD_NAME
$ oc get pods .... $ oc logs -f pod/POD_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantAfter your pod has started, leave this command running and execute the remaining steps in a different terminal window. This allows you to follow the logs and see new entries made to it. Specifically, the logs will show your debug messages.
Test the application to invoke debug logging.
oc get routes curl $APPLICATION_ROUTE/api/greeting?name=Sarah
$ oc get routes ... $ curl $APPLICATION_ROUTE/api/greeting?name=SarahCopy to Clipboard Copied! Toggle word wrap Toggle overflow Return to your pod logs to see the debug messages.
... myexample name: Sarah +3m
... myexample name: Sarah +3mCopy to Clipboard Copied! Toggle word wrap Toggle overflow
To disable debug logging, remove the DEBUG environment variable from the pod:
oc set env dc DC_NAME DEBUG-
$ oc set env dc DC_NAME DEBUG-
Additional resources
More details on environment variables are available in the OpenShift documentation.