Log4j 2 API
流程跟踪
Logger 类提供了一些日志记录方法,这些方法对于跟踪应用程序的执行路径非常有用。这些方法会生成日志记录事件,这些事件可以与其他调试日志记录分开过滤。鼓励大量使用这些方法,因为已发现输出有助于
- 在开发中进行问题诊断,而无需进行调试会话
- 在无法进行调试的生产环境中进行问题诊断
- 帮助新开发人员学习应用程序。
最常用的方法是 entry() 或 traceEntry() 和 exit() 或 traceExit() 方法。entry() 或 traceEntry() 应放置在方法的开头,除了可能简单的 getter 和 setter。entry() 可以调用传递 0 到 4 个参数。通常这些将是传递给方法的参数。traceEntry() 可以传递一个格式字符串和一个可变参数列表,或一个 Message。entry() 和 traceEntry() 方法使用 TRACE 级别进行日志记录,并使用名为“ENTER”的标记,该标记也是“FLOW”标记,所有消息字符串都将以“event”开头,即使使用格式字符串或 Message 也是如此。
entry 和 traceEntry 方法的主要区别在于 entry 方法接受一个可变的对象列表,其中每个对象可能都是一个方法参数。traceEntry 方法接受一个格式字符串,然后是一个可变的对象列表,可能包含在格式字符串中。不可能有一个包含这两个方法的单一方法,因为这将导致第一个字符串是参数还是格式字符串模棱两可。
exit() 或 traceExit() 方法应放置在任何 return 语句之前,或者作为没有 return 语句的方法的最后一条语句。exit() 和 traceExit() 可以带或不带参数调用。通常,返回 void 的方法将使用 exit() 或 traceExit(),而返回 Object 的方法将使用 exit(Object obj) 或 traceExit(object, new SomeMessage(object))。exit() 和 traceExit() 方法使用 TRACE 级别进行日志记录,并使用名为“EXIT”的标记,该标记也是“FLOW”标记,所有消息字符串都将以“exit”开头,即使使用格式字符串或 Message 也是如此。
throwing() 方法可以在应用程序抛出不太可能被处理的异常(例如 RuntimeException)时使用。这将确保在需要时提供适当的诊断信息。生成的日志记录事件将具有 ERROR 级别,并将具有名为“THROWING”的关联标记,该标记也是“EXCEPTION”标记。
catching() 方法可以在应用程序捕获它不会重新抛出的异常(无论是显式抛出还是附加到另一个异常)时使用。生成的日志记录事件将具有 ERROR 级别,并将具有名为“CATCHING”的关联标记,该标记也是“EXCEPTION”标记。
以下示例显示了一个以相当典型的方式使用这些方法的简单应用程序。由于没有显式抛出和未处理的异常,因此没有 throwing()。
package com.test; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import java.util.Random; public class TestService { private Logger logger = LogManager.getLogger(TestService.class.getName()); private String[] messages = new String[] { "Hello, World", "Goodbye Cruel World", "You had me at hello" }; private Random rand = new Random(1); public void setMessages(String[] messages) { logger.traceEntry(new JsonMessage(messages)); this.messages = messages; logger.traceExit(); } public String[] getMessages() { logger.traceEntry(); return logger.traceExit(messages, new JsonMessage(messages)); } public String retrieveMessage() { logger.entry(); String testMsg = getMessage(getKey()); return logger.exit(testMsg); } public void exampleException() { logger.entry(); try { String msg = messages[messages.length]; logger.error("An exception should have been thrown"); } catch (Exception ex) { logger.catching(ex); } logger.exit(); } public String getMessage(int key) { logger.entry(key); String value = messages[key]; return logger.exit(value); } private int getKey() { logger.entry(); int key = rand.nextInt(messages.length); return logger.exit(key); } }
此测试应用程序使用前面的服务生成日志记录事件。
package com.test; public class App { public static void main( String[] args ) { TestService service = new TestService(); service.retrieveMessage(); service.retrieveMessage(); service.exampleException(); } }
以下配置将导致所有输出路由到 target/test.log。FileAppender 的模式包括类名、行号和方法名。在模式中包含这些对于日志的价值至关重要。
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="error"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <!-- Flow tracing is most useful with a pattern that shows location. Below pattern outputs class, line number and method name. --> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </Console> <File name="log" fileName="target/test.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="log"/> </Root> </Loggers> </Configuration>
以下是上述 Java 类和配置产生的输出。
19:08:07.056 TRACE com.test.TestService 19 retrieveMessage - entry 19:08:07.060 TRACE com.test.TestService 46 getKey - entry 19:08:07.060 TRACE com.test.TestService 48 getKey - exit with (0) 19:08:07.060 TRACE com.test.TestService 38 getMessage - entry parms(0) 19:08:07.060 TRACE com.test.TestService 42 getMessage - exit with (Hello, World) 19:08:07.060 TRACE com.test.TestService 23 retrieveMessage - exit with (Hello, World) 19:08:07.061 TRACE com.test.TestService 19 retrieveMessage - entry 19:08:07.061 TRACE com.test.TestService 46 getKey - entry 19:08:07.061 TRACE com.test.TestService 48 getKey - exit with (1) 19:08:07.061 TRACE com.test.TestService 38 getMessage - entry parms(1) 19:08:07.061 TRACE com.test.TestService 42 getMessage - exit with (Goodbye Cruel World) 19:08:07.061 TRACE com.test.TestService 23 retrieveMessage - exit with (Goodbye Cruel World) 19:08:07.062 TRACE com.test.TestService 27 exampleException - entry 19:08:07.077 DEBUG com.test.TestService 32 exampleException - catching java.lang.ArrayIndexOutOfBoundsException: 3 at com.test.TestService.exampleException(TestService.java:29) [classes/:?] at com.test.App.main(App.java:9) [classes/:?] at com.test.AppTest.testApp(AppTest.java:15) [test-classes/:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_29] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_29] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_29] at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_29] at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) [junit-4.3.1.jar:?] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) [junit-4.3.1.jar:?] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) [junit-4.3.1.jar:?] at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) [surefire-junit4-2.7.2.jar:2.7.2] at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115) [surefire-junit4-2.7.2.jar:2.7.2] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) [surefire-junit4-2.7.2.jar:2.7.2] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_29] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_29] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_29] at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_29] at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) [surefire-booter-2.7.2.jar:2.7.2] at $Proxy0.invoke(Unknown Source) [?:?] at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) [surefire-booter-2.7.2.jar:2.7.2] at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) [surefire-booter-2.7.2.jar:2.7.2] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) [surefire-booter-2.7.2.jar:2.7.2] 19:08:07.087 TRACE com.test.TestService 34 exampleException - exit
只需将上述示例中的根记录器级别更改为 DEBUG,即可大幅减少输出。
19:13:24.963 DEBUG com.test.TestService 32 exampleException - catching java.lang.ArrayIndexOutOfBoundsException: 3 at com.test.TestService.exampleException(TestService.java:29) [classes/:?] at com.test.App.main(App.java:9) [classes/:?] at com.test.AppTest.testApp(AppTest.java:15) [test-classes/:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_29] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_29] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_29] at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_29] at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) [junit-4.3.1.jar:?] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) [junit-4.3.1.jar:?] at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) [junit-4.3.1.jar:?] at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) [junit-4.3.1.jar:?] at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) [surefire-junit4-2.7.2.jar:2.7.2] at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115) [surefire-junit4-2.7.2.jar:2.7.2] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) [surefire-junit4-2.7.2.jar:2.7.2] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_29] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_29] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_29] at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_29] at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) [surefire-booter-2.7.2.jar:2.7.2] at $Proxy0.invoke(Unknown Source) [?:?] at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) [surefire-booter-2.7.2.jar:2.7.2] at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) [surefire-booter-2.7.2.jar:2.7.2] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) [surefire-booter-2.7.2.jar:2.7.2]