SLF4J Binding for ADFLogger – the Missing Piece

For reasons best left untold, in my day job, I was expected to provide an SLF4J Adapter for ADF Logger Oracle ADF. Not surprisingly, slf4j does not have an adapter for ADFLogger but since ADFLogger was just a gentle wrapper over Java Util Logging, it took a little over an hour to fill that gap.

The testcases (more like main programs) in the repository will confirm that the adapter framework plays well with the Oracle Diagnostic Logging –without breaking the format of the log messages. (which is more or less the only advantage which the ADFLogger provides).
 
 
 
ODL

You could download the entire codebase from the repository at github. Optionally, if you are interested only in the binding jar, please download it here.

Log Levels :

Considering there are many log levels on the ADF Logger (7) and fewer levels in SLF4J (5), a compromise was made on certain levels on the ADF Logger. The mapping is made as follows :

LogLevelMapping

NOTE : Support for Log levels FINER and CONFIG were dropped during this adaptation.

Usage :

Just go ahead and replace your ADFLogger with SLF4J instantiations.

private static final Logger slfLogger = LoggerFactory.getLogger(LoggingChecker.class);

instead of

private static final ADFLogger adfLogger= ADFLogger.createADFLogger(LoggingChecker.class);

Logging methods

a. Logging to various levels

@Test
  public void testLoggerLevels(){

        adfLogger.finest("finest Message from ADF Logger");
        slfLogger.trace("finest Message from SLF Logger");

        adfLogger.fine("fine Message from ADF Logger");
        slfLogger.debug("fine Message from SLF Logger");

        adfLogger.info("info Message from ADF Logger");
        slfLogger.info("info Message from SLF Logger");

        adfLogger.warning("warning Message from ADF Logger");
        slfLogger.warn("warning Message from SLF Logger");

        adfLogger.severe("severe Message from ADF Logger");
        slfLogger.error("severe Message from SLF Logger");
     }

b. Logging exceptions

ExceptionTesting

where dummyException inherits a Throwable

c. Logging parameters

ParameterTesting

ParameterPassing

Development Notes :

  1. The ADF Logger Adapter framework composes of two core classes (the ADFLoggerFactory.java and the ADFLoggerAdapter.java) and three other helper classes.
  2. The Adapter can support multiple loggers including the ROOT logger of the ADFLogger (which is the default and has an empty string as its name).
  3. It is no surprise that SLF4J uses the ADFLoggerFactory.java to instantiate the ADFLoggerAdapter.
  4. For each Logger instance, the ADFLoggerAdapter.java composes an instance of ADFLogger (bound to the logger name) and delegates the call to the ADFLogger itself.
  5. The fillCallerData method of the ADFLoggerAdapter.java filters the stackframes of the ADFLoggerAdapter from the entire logging stack so that the caller class and method name is retained as the one from the host application and not of the Adapter framework classes.

Testcase (well, sort of)

public class TestClass {

    private static final ADFLogger adfLogger= ADFLogger.createADFLogger(TestClass.class);
    private static final Logger slfLogger = LoggerFactory.getLogger(TestClass.class);

    private List<String> dummyList1=null;
    private List<Integer> dummyList2=null;

    private Exception dummyException=null;

    @Before
    public void setUp(){

      dummyList1=new ArrayList<String>();
      dummyList1.add("Rock");
      dummyList1.add("Paper");
      dummyList1.add("Scissors");

      dummyList2=new ArrayList<Integer>();
      dummyList2.add(21);
      dummyList2.add(22);
      dummyList2.add(23);

      dummyException=new Exception("Mind blowing Exception");

    }

    @Test
  public void testLoggerLevels(){

        adfLogger.finest("finest Message from ADF Logger");
        slfLogger.trace("finest Message from SLF Logger");

        adfLogger.fine("fine Message from ADF Logger");
        slfLogger.debug("fine Message from SLF Logger");

        adfLogger.info("info Message from ADF Logger");
        slfLogger.info("info Message from SLF Logger");

        adfLogger.warning("warning Message from ADF Logger");
        slfLogger.warn("warning Message from SLF Logger");

        adfLogger.severe("severe Message from ADF Logger");
        slfLogger.error("severe Message from SLF Logger");
  }

    @Test
      public void testException(){

          adfLogger.severe("severe Message from ADF Logger", dummyException);
          slfLogger.error("severe Message from SLF Logger", dummyException);

      }

    @Test
      public void testParameters(){

          adfLogger.severe("severe Message from ADF Logger Param :{0}", dummyList1 );
          slfLogger.error("severe Message from SLF Logger Param :{}", dummyList1);

          adfLogger.severe("severe Message from ADF Logger Param 1 :[{0}] \n [{1}]", new Object[]{dummyList1, dummyList2} );
          slfLogger.error("severe Message from SLF Logger Param :{} \n {} ", dummyList1, dummyList2);
          slfLogger.error("severe Message from SLF Logger Param :[{}] \n [{}] ", new Object[]{dummyList1, dummyList2});

      }

Output :

Logger levels :

Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
INFO: info Message from ADF Logger
Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
INFO: info Message from SLF Logger
Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
WARNING: warning Message from ADF Logger
Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
WARNING: warning Message from SLF Logger
Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
SEVERE: severe Message from ADF Logger
Jul 01, 2013 4:21:18 PM org.slf4j.test.TestClass testLoggerLevels
SEVERE: severe Message from SLF Logger

Testing Parameters :

Jul 01, 2013 4:32:04 PM org.slf4j.test.TestClass testParameters
SEVERE: severe Message from ADF Logger Param :[Rock, Paper, Scissors]
Jul 01, 2013 4:32:04 PM org.slf4j.test.TestClass testParameters
SEVERE: severe Message from SLF Logger Param :[Rock, Paper, Scissors]
Jul 01, 2013 4:32:04 PM org.slf4j.test.TestClass testParameters
SEVERE: severe Message from ADF Logger Param 1 :[[Rock, Paper, Scissors]]
 [[21, 22, 23]]
Jul 01, 2013 4:32:04 PM org.slf4j.test.TestClass testParameters
SEVERE: severe Message from SLF Logger Param :[Rock, Paper, Scissors]
 [21, 22, 23]
Jul 01, 2013 4:32:04 PM org.slf4j.test.TestClass testParameters
SEVERE: severe Message from SLF Logger Param :[[Rock, Paper, Scissors]]
 [[21, 22, 23]]

Testing Exception :

Jul 01, 2013 4:32:28 PM org.slf4j.test.TestClass testException
SEVERE: severe Message from ADF Logger
java.lang.Exception: Mind blowing Exception
  at org.slf4j.test.TestClass.setUp(TestClass.java:38)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Jul 01, 2013 4:32:28 PM org.slf4j.test.TestClass testException
SEVERE: severe Message from SLF Logger
java.lang.Exception: Mind blowing Exception
  at org.slf4j.test.TestClass.setUp(TestClass.java:38)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 

Reference: SLF4J Binding for ADFLogger – the Missing Piece from our JCG partner Arun Manivannan at the Rerun.me blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


four × 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books