由于最好的原因,在我的日常工作中,我希望为ADF Logger Oracle ADF提供一个SLF4J适配器。 毫不奇怪,slf4j没有用于ADFLogger的适配器,但是由于ADFLogger只是Java Util Logging的轻巧包装,因此花了一个多小时来填补这一空白。
存储库中的测试用例(更像是主程序)将确认适配器框架与Oracle Diagnostic Logging配合良好,而不会破坏日志消息的格式。 (这或多或少是ADFLogger提供的唯一优势)。
您可以从github的存储库下载整个代码库 。 或者,如果您只对装订罐感兴趣,请在此处下载 。
日志级别:
考虑到ADF Logger(7)上有许多日志级别,而SLF4J(5)中有更少的日志级别,因此在ADF Logger上的某些级别上做出了折衷。 映射如下:
注意:在此调整期间,已删除对日志级别FINER和CONFIG的支持。
用法:
只需继续,用SLF4J实例替换您的ADFLogger。
private static final Logger slfLogger = LoggerFactory.getLogger(LoggingChecker.class);
代替
private static final ADFLogger adfLogger= ADFLogger.createADFLogger(LoggingChecker.class);
记录方式
一个。 记录到各个级别
@Testpublic 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。 记录异常
其中dummyException
继承了Throwable
C。 记录参数
开发说明:
- ADF Logger Adapter框架由两个核心类(
ADFLoggerFactory.java
和ADFLoggerAdapter.java
)以及其他三个帮助器类组成。 - 适配器可以支持多个记录器,包括ADFLogger的ROOT记录器(这是默认记录,名称为空字符串)。
- SLF4J使用
ADFLoggerFactory.java
实例化ADFLoggerAdapter也就不足为奇了。 - 对于每个Logger实例,
ADFLoggerAdapter.java
组成一个ADFLoggerAdapter.java
实例(绑定到记录器名称),并将调用委托给ADFLogger本身。 -
fillCallerData
方法从整个日志记录堆栈中筛选ADFLoggerAdapter的堆栈框架,以便将调用者类和方法名保留为主机应用程序中的那个,而不是Adapter框架类中的。
测试用例(很好)
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;@Beforepublic 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");}@Testpublic 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");}@Testpublic void testException(){adfLogger.severe("severe Message from ADF Logger", dummyException);slfLogger.error("severe Message from SLF Logger", dummyException);}@Testpublic 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});}
输出:
记录器级别:
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
测试参数:
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]]
测试异常:
Jul 01, 2013 4:32:28 PM org.slf4j.test.TestClass testException
SEVERE: severe Message from ADF Logger
java.lang.Exception: Mind blowing Exceptionat 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 Exceptionat 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)
参考: ADFLogger的SLF4J绑定– Rerun.me博客上我们JCG合作伙伴 Arun Manivannan 的缺失作品 。
翻译自: https://www.javacodegeeks.com/2013/11/slf4j-binding-for-adflogger-the-missing-piece.html