Chapter 5. Debugging Eclipse Vert.x based application
This sections contains information about debugging your Eclipse Vert.x–based application both in local and remote deployments.
5.1. Remote debugging Copy linkLink copied to clipboard!
To remotely debug an application, you must first configure it to start in a debugging mode, and then attach a debugger to it.
5.1.1. Starting your application locally in debugging mode Copy linkLink copied to clipboard!
One of the ways of debugging a Maven-based project is manually launching the application while specifying a debugging port, and subsequently connecting a remote debugger to that port. This method is applicable at least to the following deployments of the application:
-
When launching the application manually using the
mvn vertx:debuggoal. This starts the application with debugging enabled.
Prerequisites
- A Maven-based application
Procedure
- In a console, navigate to the directory with your application.
Launch your application and specify the debug port using the
-Ddebug.portargument:$ mvn vertx:debug -Ddebug.port=$PORT_NUMBERHere,
$PORT_NUMBERis an unused port number of your choice. Remember this number for the remote debugger configuration.Use the
-Ddebug.suspend=trueargument to make the application wait until a debugger is attached to start.
5.1.2. Starting your application on OpenShift in debugging mode Copy linkLink copied to clipboard!
To debug your Eclipse Vert.x-based application on OpenShift remotely, you must set the JAVA_DEBUG environment variable inside the container to true 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 dcSet the
JAVA_DEBUGenvironment variable in the deployment configuration of your application totrue, which configures the JVM to open the port number5005for debugging. For example:$ oc set env dc/MY_APP_NAME JAVA_DEBUG=trueRedeploy the application if it is not set to redeploy automatically on configuration change. For example:
$ oc rollout latest dc/MY_APP_NAMEConfigure port forwarding from your local machine to the application pod:
List the currently running pods and find one containing your application:
$ oc get pod NAME READY STATUS RESTARTS AGE MY_APP_NAME-3-1xrsp 0/1 Running 0 6s ...Configure port forwarding:
$ oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5005Here,
$LOCAL_PORT_NUMBERis an unused port number of your choice on your local machine. Remember this number for the remote debugger configuration.
When you are done debugging, unset the
JAVA_DEBUGenvironment variable in your application pod. For example:$ oc set env dc/MY_APP_NAME JAVA_DEBUG-
Additional resources
You can also set the JAVA_DEBUG_PORT environment variable if you want to change the debug port from the default, which is 5005.
5.1.3. Attaching a remote debugger to the application Copy linkLink copied to clipboard!
When your application is configured for debugging, attach a remote debugger of your choice to it. In this guide, Red Hat CodeReady Studio is covered, but the procedure is similar when using other programs.
Prerequisites
- The application running either locally or on OpenShift, and configured for debugging.
- The port number that your application is listening on for debugging.
- Red Hat CodeReady Studio installed on your machine. You can download it from the Red Hat CodeReady Studio download page.
Procedure
- Start Red Hat CodeReady Studio.
Create a new debug configuration for your application:
- Click Run→Debug Configurations.
- In the list of configurations, double-click Remote Java application. This creates a new remote debugging configuration.
- Enter a suitable name for the configuration in the Name field.
- Enter the path to the directory with your application into the Project field. You can use the Browse… button for convenience.
- Set the Connection Type field to Standard (Socket Attach) if it is not already.
- Set the Port field to the port number that your application is listening on for debugging.
- Click Apply.
Start debugging by clicking the Debug button in the Debug Configurations window.
To quickly launch your debug configuration after the first time, click Run→Debug History and select the configuration from the list.
Additional resources
Debug an OpenShift Java Application with JBoss Developer Studio on Red Hat Knowledgebase.
Red Hat CodeReady Studio was previously called JBoss Developer Studio.
- A Debugging Java Applications On OpenShift and Kubernetes article on OpenShift Blog.
5.2. Debug logging Copy linkLink copied to clipboard!
Eclipse Vert.x provides a built-in logging API. The default logging implementation for Eclipse Vert.x uses the java.util.logging library that is provided with the Java JDK. Alternatively, Eclipse Vert.x allows you to use a different logging framework, for example, Log4J (Eclipse Vert.x supports Log4J v1 and v2) or SLF4J.
5.2.1. Configuring logging for your Eclipse Vert.x application using java.util.logging Copy linkLink copied to clipboard!
To configure debug logging for your Eclipse Vert.x application using java.util.logging:
-
Set the
java.util.logging.config.filesystem property in theapplication.propertiesfile. The value of this variable must correspond to the name of yourjava.util.loggingconfiguration file. This ensures thatLogManagerinitializesjava.util.loggingat application startup. -
Alternatively, add a
java.util.loggingconfiguration file with thevertx-default-jul-logging.propertiesname to the classpath of your Maven project. Eclipse Vert.x will use that file to configurejava.util.loggingon application startup.
Eclipse Vert.x allows you to specify a custom logging backend using the LogDelegateFactory that provides pre-built implementations for the Log4J, Log4J2 and SLF4J libraries. Unlike java.util.logging, which is included with Java by default, the other backends require that you specify their respective libraries as dependencies for your application.
5.2.2. Adding log output to your Eclipse Vert.x application. Copy linkLink copied to clipboard!
To add logging to your application, create a
io.vertx.core.logging.Logger:Logger logger = LoggerFactory.getLogger(className); logger.info("something happened"); logger.error("oops!", exception); logger.debug("debug message"); logger.warn("warning");ImportantLogging backends use different formats to represent replaceable tokens in parameterized messages. If you rely on parameterized logging methods, you will not be able to switch logging backends without changing your code.
5.2.3. Specifying a custom logging framework for your application Copy linkLink copied to clipboard!
If you do not want Eclipse Vert.x to use java.util.logging, configure io.vertx.core.logging.Logger to use a different logging framework, for example, Log4J or SLF4J:
Set the value of the
vertx.logger-delegate-factory-class-namesystem property to the name of the class that implements theLogDelegateFactoryinterface. Eclipse Vert.x provides the pre-built implementations for the following libraries with their corresponding pre-defined classnames listed below:Expand Library Class name Log4Jv1io.vertx.core.logging.Log4jLogDelegateFactoryLog4Jv2io.vertx.core.logging.Log4j2LogDelegateFactorySLF4Jio.vertx.core.logging.SLF4JLogDelegateFactoryWhen implementing logging using a custom library, ensure that the relevant
Log4JorSLF4Jjars are included among the dependencies for your application.ImportantThe Log4J v1 delegate provided with Eclipse Vert.x does not support parameterized messages. The delegates for Log4J v2 and SLF4J both use the
{}syntax. Thejava.util.loggingdelegate relies onjava.text.MessageFormatthat uses the{n}syntax.
5.2.4. Configuring Netty logging for your Eclipse Vert.x application. Copy linkLink copied to clipboard!
Netty is a library used by VertX to manage asynchronous network communication in applications.
Netty:
- Allows quick and easy development of network applications, such as protocol servers and clients.
- Simplifies and streamlines network programming, such as TCP and UDP socket server development.
- Provides a unified API for managing blocking and non-blocking connections.
Netty does not rely on an external logging configuration using system properties. Instead, it implements a logging configuration based on logging libraries visible to Netty classes in your project. Netty tries to use the libraries in the following order:
-
SLF4J -
Log4J -
java.util.loggingas a fallback option
You can set io.netty.util.internal.logging.InternalLoggerFactory directly to a particular logger by adding the following code at the beginning of the main method of your application:
// Force logging to Log4j
InternalLoggerFactory.setDefaultFactory(Log4JLoggerFactory.INSTANCE);
5.2.5. Accessing debug logs on OpenShift Copy linkLink copied to clipboard!
Start your application and interact with it to see the debugging statements in OpenShift.
Prerequisites
-
The
ocCLI client installed and authenticated. - A Maven-based application with debug logging enabled.
Procedure
Deploy your application to OpenShift:
$ mvn clean oc:deploy -PopenshiftView the logs:
Get the name of the pod with your application:
$ oc get podsStart watching the log output:
$ oc logs -f pod/MY_APP_NAME-2-aaaaaKeep the terminal window displaying the log output open so that you can watch the log output.
Interact with your application:
For example, if you had debug logging in the REST API Level 0 example to log the
messagevariable in the/api/greetingmethod:Get the route of your application:
$ oc get routesMake an HTTP request on the
/api/greetingendpoint of your application:$ curl $APPLICATION_ROUTE/api/greeting?name=Sarah
Return to the window with your pod logs and inspect debug logging messages in the logs.
... Feb 11, 2017 10:23:42 AM io.openshift.MY_APP_NAME INFO: Greeting: Hello, Sarah ...-
To disable debug logging, update your logging configuration file, for example
src/main/resources/vertx-default-jul-logging.properties, remove the logging configuration for your class and redeploy your application.