logback:
官方文档:https://logback.qos.ch/manual/configuration.html
入口: private final static Logger logger = org.slf4j.LoggerFactory.getLogger(ServiceTest.class);
第一次用到的时候,会从LoggerFactory中拿一个logger
1. org.slf4j.LoggerFactory.getLogger(name) --> ILoggerFactory iLoggerFactory = getILoggerFactory();
初始化一个ILoggerFactory,初始化时会调用org.slf4j.impl.StaticLoggerBinder.getSingleton() 来实现其他日志框架与slf4j的绑定(bind)。
logback-classic-1.1.2.jar包里面就有这个类,这就是 logback 与 slf4j 绑定的方法。
2. StaticLoggerBinder.getSingleton() 会去执行初始化动作
init() --> new ContextInitializer(defaultLoggerContext).autoConfig();
ContextInitializer
加载配置文件顺序:前一个为空,则加载下一个
logback.groovy --> logback-test.xml --> logback.xml
19:02:56,446 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
19:02:56,447 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
19:02:56,447 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/workspace/java-programs/sz-agent/sz-agent-server/target/classes/logback.xml]
30s重载配置:(<configuration scan="true" scanPeriod="30 seconds" debug="false">)
19:02:56,592 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[E:workspacejava-programs z-agent z-agent-server argetclasseslogback.xml]] every 30 seconds.
-----------------------------------------------------------------
异步打日志要加下面标黄的配置,否则 %line 会显示成 ?
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="FILE" /> <!-- add the following line --> <includeCallerData>true</includeCallerData> </appender>
日志格式输出里面的参数是在 PatternLayout.java 里面设定的。
https://logback.qos.ch/manual/layouts.html#PatternLayout
1. %ex的使用
%ex是默认加在输出格式最后的。如果 Packaging data (包信息)是打开的,那么默认加上 %xEx, 如果 Packaging data 是关闭的,默认加上的是 %ex。
Packaging data 是 jar 包版本信息,有时对查错比较有帮助。As of version 1.1.4, packaging data is disabled by default.
Packaging data 输出例子:
14:28:48.835 [btpool0-7] INFO c.q.l.demo.prime.PrimeAction - 99 is not a valid value java.lang.Exception: 99 is invalid at ch.qos.logback.demo.prime.PrimeAction.execute(PrimeAction.java:28) [classes/:na] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) [struts-1.2.9.jar:1.2.9] at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) [struts-1.2.9.jar:1.2.9] at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) [struts-1.2.9.jar:1.2.9] at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) [servlet-api-2.5-6.1.12.jar:6.1.12] at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502) [jetty-6.1.12.jar:6.1.12] at ch.qos.logback.demo.UserServletFilter.doFilter(UserServletFilter.java:44) [classes/:na] at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115) [jetty-6.1.12.jar:6.1.12] at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361) [jetty-6.1.12.jar:6.1.12] at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417) [jetty-6.1.12.jar:6.1.12] at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) [jetty-6.1.12.jar:6.1.12]