Package org.slf4j.instrumentation
Class LogTransformer
- java.lang.Object
-
- org.slf4j.instrumentation.LogTransformer
-
- All Implemented Interfaces:
java.lang.instrument.ClassFileTransformer
public class LogTransformer extends java.lang.Object implements java.lang.instrument.ClassFileTransformer
LogTransformer does the work of analyzing each class, and if appropriate add log statements to each method to allow logging entry/exit.
This class is based on the article Add Logging at Class Load Time with Java Instrumentation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LogTransformer.Builder
Builder provides a flexible way of configuring some of many options on the parent class instead of providing many constructors.
-
Field Summary
Fields Modifier and Type Field Description private boolean
addEntryExit
private java.lang.String[]
ignore
private java.lang.String
level
private java.lang.String
levelEnabled
private java.lang.String
loggerName
private boolean
verbose
-
Constructor Summary
Constructors Modifier Constructor Description private
LogTransformer(LogTransformer.Builder builder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private byte[]
doClass(java.lang.String name, java.lang.Class<?> clazz, byte[] b)
doClass() process a single class by first creates a class description from the byte codes.private void
doMethod(javassist.CtBehavior method)
process a single method - this means add entry/exit logging if requested.byte[]
transform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<?> clazz, java.security.ProtectionDomain domain, byte[] bytes)
private byte[]
transform0(java.lang.String className, java.lang.Class<?> clazz, java.security.ProtectionDomain domain, byte[] bytes)
transform0 sees if the className starts with any of the namespaces to ignore, if so it is returned unchanged.
-
-
-
Constructor Detail
-
LogTransformer
private LogTransformer(LogTransformer.Builder builder)
-
-
Method Detail
-
transform
public byte[] transform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<?> clazz, java.security.ProtectionDomain domain, byte[] bytes)
- Specified by:
transform
in interfacejava.lang.instrument.ClassFileTransformer
-
transform0
private byte[] transform0(java.lang.String className, java.lang.Class<?> clazz, java.security.ProtectionDomain domain, byte[] bytes)
transform0 sees if the className starts with any of the namespaces to ignore, if so it is returned unchanged. Otherwise it is processed by doClass(...)- Parameters:
className
-clazz
-domain
-bytes
-- Returns:
-
doClass
private byte[] doClass(java.lang.String name, java.lang.Class<?> clazz, byte[] b)
doClass() process a single class by 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.- Parameters:
name
- class name (slashes separate, not dots)clazz
-b
-- Returns:
-
doMethod
private void doMethod(javassist.CtBehavior method) throws javassist.NotFoundException, javassist.CannotCompileException
process a single method - this means add entry/exit logging if requested. It is only called for methods with a body.- Parameters:
method
- method to work on- Throws:
javassist.NotFoundException
javassist.CannotCompileException
-
-