Hi I am try to add debug points to my javaagent. I am having two separate classes for premain method and transform method. Logs added for Agent class prints as expected. But in ClassFileTransformer class it prints some log lines and ignore some others. (Eg: logs in catch block, within a method in the same class)
InstClassTransformer.java
public class InstClassTransformer implements ClassFileTransformer {
private static final Log log = LogFactory.getLog(InstClassTransformer.class);
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if(log.isDebugEnabled()){
log.debug("Loading class : "+className.replace('/','.')); //log 1
}
ByteArrayInputStream currentClass = null;
CtClass ctClass = null;
byte[] transformedBytes = classfileBuffer;
try {
ClassPool classPool = ClassPool.getDefault();
currentClass = new ByteArrayInputStream(classfileBuffer);
ctClass = classPool.makeClass(currentClass);
instrumentClass(ctClass, baseClass.getName());
}
transformedBytes = ctClass.toBytecode();
} catch (NotFoundException e) {
if(log.isDebugEnabled()){
log.debug("Unable to find "+ className.replace('/','.') + "for instrumentation : "+ e.getMessage()); //log2
}
} catch (CannotCompileException | IOException e) {
if(log.isDebugEnabled()){
log.debug("Intrumentation of " + className.replace('/', '.') + "failed : " + e.getMessage()); //log3
}
} finally {
if (currentClass != null) {
try {
currentClass.close();
} catch (IOException e) {
if(log.isDebugEnabled()){
log.debug("Failed to close the connection : " + e.getMessage());
}
}
}
if(ctClass != null){
ctClass.detach();
}
}
return transformedBytes;
}
public void instrumentClass(CtClass ctClass, String name) throws NotFoundException, CannotCompileException {
if(log.isDebugEnabled()){
log.debug("Instrumenting " + ctClass.getName()); //log4
}
CtMethod[] method = ctClass.getDeclaredMethods();
...
}
When I start the agent it prints the log1 for each class it loads and log 2 for some classes it is unable to find. But it doesn't print log3. I can gurantee that catch block is being reached because there are two situations where it get hit. When I add e.printStackTrace it prints the trace, but same time it doesn't print the log. It doesn't print log4 also. But if I add print statement for that instrumentClass() method, it gets printed on the console, but not the log.
Another thing is, even though it prints the logs from ClassFileTransformer it says that No appenders can be found for the same class.
log4j:WARN No appenders could be found for logger (org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I am using log4j for the logging purpose. My log4j.properties file,
log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.append=true
log4j.appender.file.file=/path/to/testLog/log4j-application-1.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss},%r] %-5p {%c} - %m%n
log4j.logger.org.javaagent.instrumentation=DEBUG
Is this some kind of behavior of javaagent or am I missing something.