Class 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  
    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.instrument.ClassFileTransformer

        transform
    • Field Detail

      • level

        private java.lang.String level
      • levelEnabled

        private java.lang.String levelEnabled
      • addEntryExit

        private boolean addEntryExit
      • verbose

        private boolean verbose
      • ignore

        private java.lang.String[] ignore
      • loggerName

        private java.lang.String loggerName
    • 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 interface java.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