Post date: Mar 17, 2016 10:44:42 AM
During development process / bug fixing process, developers would generally add debugs in Java methods and run the build files to deploy the changes. Then the Java application would be tested to see the new logs. This whole process has 3 problems.
Making changes and deployment process take long time.
The application is running with an older version of the code and Source is available in version control system. It is painful, if one has to take an earlier version.
The developer doesn't have the source code to add the logs.
In such scenarios, we can actually use Java Instrumentation concept and include the log message, while the classes are loaded. Following the steps found in the oracle blog, we had prepared the code for the same.
Steps for Implementation:
Pre-requisites:
Java 1.8 or later
Eclipse Kepler or later
Apache ANT 1.9.6 or later
Prepare the project in Eclipse :
Download the zip file . Note: Please find the "LoggingByInstrumentation.zip" link below this document, to download.
Create a new Java Project in Eclipse and import the contents in the zip file, into it.
Open LoggerAgent.java file. Please read class level comments, written in LoggerAgent.java. You might want to change variable values in the class. Save the file.
Change the properties in RunBuild.xml and run.
Ways To Run:
ANT execution: Run RunBuild.xml ant file, to see the output.
Command line execution: D:\EclipseWorkspaceKepler\LoggingByInstrumentation\bin>java -classpath ../lib/javassist3_18_2.jar;../lib/slf4j-ext-1.7.13.jar; -javaagent:../dist/loggeragent.jar=time com.runjva.demo.HelloWorld
Configuring LoggerAgent at system level: Run the below command in command prompt (with administrative privileges) to set Java agent in JAVA_TOOL_OPTIONS. This way, it will instrument and include logs in any Java application, running on that machine.
setx -m JAVA_TOOL_OPTIONS "-javaagent:D:/EclipseWorkspaceKepler/LoggingByInstrumentation/dist/loggeragent.jar=time,sahi_ui -Xbootclasspath/p:D:/EclipseWorkspaceKepler/LoggingByInstrumentation/lib/javassist3_18_2.jar;D:/EclipseWorkspaceKepler/LoggingByInstrumentation/lib/slf4j-ext-1.7.13.jar -Djava.vendor=\"Sun Microsystems Inc.\""
Note:
Log File will be generated, based on the configurations in Log4j.properties file.
Below exception might be raised if lesser Apache ANT version is used. Install latest ANT,as mentioned in pre-requisites. D:\EclipseWorkspaceKepler\LoggingByInstrumentation\RunBuild.xml:45: Class not found: javac1.8
Source File Image
Log File Image :
References:
Logging By Instrumentation initial source taken from here. Reading this link will be helpful to understand the implementation of Java agent.