开源SDK:
<dependency><groupId>io.github.mingyang66</groupId><artifactId>oceansky-logger</artifactId><version>4.3.6</version>
</dependency>
<!-- 基于logback的日志组件SDK -->
<dependency><groupId>io.github.mingyang66</groupId><artifactId>emily-spring-boot-logger</artifactId><version>4.3.6</version>
</dependency>
在logback日志的xml配置文件中经常可以看到如下配置:
<configuration scan="true" scanPeriod="60000" debug="false" packagingData="false">
其中packagingData的默认值是false,这个属性会有何作用,对日志打印有何影戏?
一、logback-classic包中的ConfigurationAction类对Configuration配置属性进行读取
static final String INTERNAL_DEBUG_ATTR = "debug";static final String PACKAGING_DATA_ATTR = "packagingData";static final String SCAN_ATTR = "scan";static final String SCAN_PERIOD_ATTR = "scanPeriod";static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback.debug";long threshold = 0;public void begin(InterpretationContext ic, String name, Attributes attributes) {...LoggerContext lc = (LoggerContext) context;//读取packagingData属性配置boolean packagingData = OptionHelper.toBoolean(ic.subst(attributes.getValue(PACKAGING_DATA_ATTR)), LoggerContext.DEFAULT_PACKAGING_DATA);//设置属性lc.setPackagingDataEnabled(packagingData);if (EnvUtil.isGroovyAvailable()) {ContextUtil contextUtil = new ContextUtil(context);contextUtil.addGroovyPackages(lc.getFrameworkPackages());}// the context is turbo filter attachable, so it is pushed on top of the// stackic.pushObject(getContext());}
此处会将从配置文件中读取到的属性值设置到Context上下文之中;而setPackagingDataEnabled方法又是LoggerContext类中的一个方法,LoggerContext类中还存在一个读取packingData属性的方法isPackagingDataEnabled;
二、isPackagingDataEnabled方法调用处理类
经追踪可以发现isPackagingDataEnabled方法会被EnsureExceptionHandling类的process方法调用
public void process(Context context, Converter<ILoggingEvent> head) {if (head == null) {// this should never happenthrow new IllegalArgumentException("cannot process empty chain");}if (!chainHandlesThrowable(head)) {Converter<ILoggingEvent> tail = ConverterUtil.findTail(head);Converter<ILoggingEvent> exConverter = null;LoggerContext loggerContext = (LoggerContext) context;if (loggerContext.isPackagingDataEnabled()) {exConverter = new ExtendedThrowableProxyConverter();} else {exConverter = new ThrowableProxyConverter();}tail.setNext(exConverter);}}
我们会发现如果packingData属性被设置为true的时候会new一个ExtendedThrowableProxyConverter类的实例对象,否则就是ThrowableProxyConverter类的实例对象;
三、ExtendedThrowableProxyConverter和ThrowableProxyConverter的区别
public class ExtendedThrowableProxyConverter extends ThrowableProxyConverter {@Overrideprotected void extraData(StringBuilder builder, StackTraceElementProxy step) {ThrowableProxyUtil.subjoinPackagingData(builder, step);}protected void prepareLoggingEvent(ILoggingEvent event) {}}
看过ExtendedThrowableProxyConverter的源码后你会清楚的了解到ExtendedThrowableProxyConverter类是ThrowableProxyConverter的子类;其唯一不同点就是实现了extraData方法的实现;
ThrowableProxyConverter类中extraData方法调用入口:
private void printStackLine(StringBuilder buf, int ignoredCount, StackTraceElementProxy element) {buf.append(element);extraData(buf, element); // allow other data to be addedif (ignoredCount > 0) {printIgnoredCount(buf, ignoredCount);}}
看到这里清除ExtendedThrowableProxyConverter类实现方法的不同点了吗?它会在每一行打印的堆栈后面添加上自己指定的字符串,具体添加什么字符串后面我们接着看。
四、ExtendedThrowableProxyConverter类在每行堆栈后面添加了哪些内容
public class ExtendedThrowableProxyConverter extends ThrowableProxyConverter {@Overrideprotected void extraData(StringBuilder builder, StackTraceElementProxy step) {ThrowableProxyUtil.subjoinPackagingData(builder, step);}protected void prepareLoggingEvent(ILoggingEvent event) {}}
在每行堆栈后面添加额外信息的实现是在ThrowableProxyUtil.subjoinPackagingData方法中实现;
public static void subjoinPackagingData(StringBuilder builder, StackTraceElementProxy step) {if (step != null) {ClassPackagingData cpd = step.getClassPackagingData();if (cpd != null) {if (!cpd.isExact()) {builder.append(" ~[");} else {builder.append(" [");}builder.append(cpd.getCodeLocation()).append(':').append(cpd.getVersion()).append(']');}}}
看到这段代码就明白了了吧,会在堆栈每一行后面添加一个左中括号和右中括号,内部填充类所属的包;
五、打印案例
- packingData为true
2023-07-17 13:17:04.483 ERROR default --- [tp-nio-8080-exec-2] c.e.i.t.controller.LogbackController:35 : -----error test----
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is nullat com.emily.infrastructure.test.controller.LogbackController.debug(LogbackController.java:33) ~[classes/:na]at com.emily.infrastructure.test.controller.LogbackController$$FastClassBySpringCGLIB$$2de19373.invoke(<generated>) ~[classes/:na]at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.28.jar:5.3.28]at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:70) ~[druid-1.2.18.jar:na]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.28.jar:5.3.28]at com.emily.infrastructure.autoconfigure.request.interceptor.DefaultRequestMethodInterceptor.invoke(DefaultRequestMethodInterceptor.java:64) ~[classes/:na]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.28.jar:5.3.28]at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.28.jar:5.3.28]at com.emily.infrastructure.test.controller.LogbackController$$EnhancerBySpringCGLIB$$5379665.debug(<generated>) ~[classes/:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.28.jar:5.3.28]at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.28.jar:5.3.28]at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.28.jar:5.3.28]at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.28.jar:5.3.28]
- packingData为false
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is nullat com.emily.infrastructure.test.controller.LogbackController.debug(LogbackController.java:33)at com.emily.infrastructure.test.controller.LogbackController$$FastClassBySpringCGLIB$$2de19373.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:70)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)at com.emily.infrastructure.autoconfigure.request.interceptor.DefaultRequestMethodInterceptor.invoke(DefaultRequestMethodInterceptor.java:64)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)at com.emily.infrastructure.test.controller.LogbackController$$EnhancerBySpringCGLIB$$3879bcba.debug(<generated>)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base/java.lang.reflect.Method.invoke(Method.java:568)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)at javax.servlet.http.HttpServlet.service(HttpServlet.java:529)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)at javax.servlet.http.HttpServlet.service(HttpServlet.java:623)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:114)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at com.emily.infrastructure.core.servlet.filter.RequestChannelFilter.doFilter(RequestChannelFilter.java:26)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390)at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791)at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.base/java.lang.Thread.run(Thread.java:833)
GitHub源码:https://github.com/mingyang66/spring-parent