[slf4j-dev] svn commit: r1152 - in slf4j/trunk/slf4j-ext/src/main/java/org/slf4j: agent instrumentation

ravn at slf4j.org ravn at slf4j.org
Mon Sep 29 01:03:51 CEST 2008


Author: ravn
Date: Mon Sep 29 01:03:51 2008
New Revision: 1152

Modified:
   slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/agent/AgentMain.java
   slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/AddEntryExitLoggingTransformer.java

Log:
changed to use message formatting

Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/agent/AgentMain.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/agent/AgentMain.java	(original)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/agent/AgentMain.java	Mon Sep 29 01:03:51 2008
@@ -1,6 +1,5 @@
 package org.slf4j.agent;
 
-import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.Instrumentation;
 import java.util.Arrays;
 import java.util.Date;

Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/AddEntryExitLoggingTransformer.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/AddEntryExitLoggingTransformer.java	(original)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/AddEntryExitLoggingTransformer.java	Mon Sep 29 01:03:51 2008
@@ -3,8 +3,11 @@
  */
 package org.slf4j.instrumentation;
 
+import static org.slf4j.helpers.MessageFormatter.format;
+
 import java.io.ByteArrayInputStream;
 import java.lang.instrument.ClassFileTransformer;
+import java.security.ProtectionDomain;
 
 import javassist.CannotCompileException;
 import javassist.ClassPool;
@@ -13,32 +16,42 @@
 import javassist.CtField;
 import javassist.NotFoundException;
 
+public class AddEntryExitLoggingTransformer implements ClassFileTransformer {
 
-public class AddEntryExitLoggingTransformer implements
-		ClassFileTransformer {
-	
-	final String def = "private static org.slf4j.Logger " + "_log" + ";";
-	final String ifLog = "if (_log.isDebugEnabled())";
+	private static final String _LOG = "_log";
 	String[] ignore = new String[] { "sun/", "java/", "javax/", "org/slf4j/",
 			"ch/qos/logback/" };
 
-	public byte[] transform(ClassLoader loader, String className, Class<?> clazz,
-			java.security.ProtectionDomain domain, byte[] bytes) {
+	public byte[] transform(ClassLoader loader, String className,
+			Class<?> clazz, ProtectionDomain domain, byte[] bytes) {
 
 		for (int i = 0; i < ignore.length; i++) {
 			if (className.startsWith(ignore[i])) {
 				return bytes;
 			}
 		}
-		//System.out.println("Adding to " + className);
+		// System.out.println("Adding to " + className);
 		return doClass(className, clazz, bytes);
 	}
 
-	//
-	// The transform(...) method calls doClass(...) if the class name does not
-	// start with any of the prefixes it has been told to ignore (note that the
-	// separators are slashes, not dots).
-
+	/**
+	 * The transform(...) method calls doClass(...) if the class name does not
+	 * start with any of the prefixes it has been told to ignore.
+	 * 
+	 * doClass() first creates a class description from the byte codes. If it is
+	 * a class (i.e. not an interface) the methods defined have bodies, and a
+	 * static final logger object is added with the name of this class as an
+	 * argument, and each method then gets processed with doMethod(...) to have
+	 * logger calls added.
+	 * 
+	 * 
+	 * 
+	 * @param name
+	 *            class name (slashes separate, not dots)
+	 * @param clazz
+	 * @param b
+	 * @return
+	 */
 	private byte[] doClass(String name, Class<?> clazz, byte[] b) {
 		ClassPool pool = ClassPool.getDefault();
 		CtClass cl = null;
@@ -46,9 +59,14 @@
 			cl = pool.makeClass(new ByteArrayInputStream(b));
 			if (cl.isInterface() == false) {
 
-				CtField field = CtField.make(def, cl);
-				String getLogger = "org.slf4j.LoggerFactory.getLogger("
-						+ name.replace('/', '.') + ".class);";
+				String pattern1 = "private static org.slf4j.Logger {};";
+				String loggerDefinition = format(pattern1, _LOG);
+				CtField field = CtField.make(loggerDefinition, cl);
+
+				String pattern2 = "org.slf4j.LoggerFactory.getLogger({}.class);";
+				String replace = name.replace('/', '.');
+				String getLogger = format(pattern2, replace);
+
 				cl.addField(field, getLogger);
 				System.out.println(getLogger);
 
@@ -61,8 +79,8 @@
 				b = cl.toBytecode();
 			}
 		} catch (Exception e) {
-			System.err.println("Could not instrument  " + name
-					+ ",  exception : " + e.getMessage());
+			String pattern = "Could not instrument {},  exception : {}";
+			System.err.println(format(pattern, name, e.getMessage()));
 		} finally {
 			if (cl != null) {
 				cl.detach();
@@ -77,9 +95,14 @@
 		String signature = JavassistHelper.getSignature(method);
 		String returnValue = JavassistHelper.returnValue(method);
 
-		method.insertBefore(ifLog + "_log" + ".info(\">> " + signature + ");");
-
-		method.insertAfter(ifLog + "_log" + ".info(\"<< " + signature
-				+ returnValue + ");");
+		String messagePattern = "if ({}.isDebugEnabled()) {}.info(\">> {}\");";
+		Object[] arg1 = new Object[] { _LOG, _LOG, signature };
+		String before = format(messagePattern, arg1);
+		method.insertBefore(before);
+
+		String messagePattern2 = "if ({}.isDebugEnabled()) {}.info(\"<< {}{}\");";
+		Object[] arg2 = new Object[] { _LOG, _LOG, signature, returnValue };
+		String after = format(messagePattern2, arg2);
+		method.insertAfter(after);
 	}
 }
\ No newline at end of file



More information about the slf4j-dev mailing list