在使用slf4j+log4j时,发现输出的日志总是在DEBUG级别,而且格式非logback.xml中给出的格式。
logback.xml内容如下:
HERE %d{HH:mm:ss.SSS} [%thread] %-5level %C{36} %L - %msg%n
UTF-8
信息: Initializing Spring root WebApplicationContext
14:20:53.084 [localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
14:20:53.136 [localhost-startStop-1] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution
14:20:53.206 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
14:20:53.206 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
14:20:53.212 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [jndiProperties] PropertySource with lowest search precedence
14:20:53.213 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
14:20:53.217 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
14:20:53.217 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
14:20:53.227 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
14:20:53.228 [localhost-startStop-1] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Fri Jul 29 14:20:53 CST 2016]; root of context hierarchy
14:20:53.300 [localhost-startStop-1] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
14:20:53.300 [localhost-startStop-1] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
14:20:53.301 [localhost-startStop-1] DEBUG org.springframework.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
14:20:53.317 [localhost-startStop-1] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatch-servlet.xml]
14:20:53.320 [localhost-startStop-1] DEBUG org.springframework.beans.factory.xml.DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
14:20:53.354 [localhost-startStop-1] DEBUG org.springframework.beans.factory.xml.PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
14:20:53.360 [localhost-startStop-1] DEBUG org.springframework.beans.factory.xml.PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd=org/springframework/jdbc/config/spring-jdbc-4.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/security/spring-security-3.0.3.xsd=org/springframework/security/config/spring-security-3.0.3.xsd, http://www.springframework.org/schema/security/spring-security-2.0.1.xsd=org/springframework/security/config/spring-security-2.0.1.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd, http://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-4.1.xsd=org/springframework/beans/factory/xml/spring-tool-4.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd=org/springframework/jdbc/config/spring-jdbc-3.1.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd, http://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd, http://www.springframework.org/schema/security/spring-security-2.0.xsd=org/springframework/security/config/spring-security-2.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd=org/springframework/jdbc/config/spring-jdbc-4.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd, http://www.springframework.org/schema/beans/spring-beans-4.1.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-4.0.xsd=org/springframework/beans/factory/xml/spring-tool-4.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.2.xsd=org/springframework/transaction/config/spring-tx-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc.xsd=org/springframework/jdbc/config/spring-jdbc-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/tx/spring-tx-4.1.xsd=org/springframework/transaction/config/spring-tx-4.1.xsd, http://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-4.0.xsd=org/springframework/beans/factory/xml/spring-beans-4.0.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd, http://www.springframework.org/schema/security/spring-security-2.0.4.xsd=org/springframework/security/config/spring-security-2.0.4.xsd, http://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx-4.0.xsd=org/springframework/transaction/config/spring-tx-4.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/security/spring-security-3.2.xsd=org/springframework/security/config/spring-security-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/util/spring-util-4.1.xsd=org/springframework/beans/factory/xml/spring-util-4.1.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/security/spring-security-3.1.xsd=org/springframework/security/config/spring-security-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/util/spring-util-4.0.xsd=org/springframework/beans/factory/xml/spring-util-4.0.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-4.1.xsd, http://www.springframework.org/schema/security/spring-security-2.0.2.xsd=org/springframework/security/config/spring-security-2.0.2.xsd, http://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-3.2.xsd, http://www.alibaba.com/schema/stat.xsd=META-INF/stat.xsd, http://www.springframework.org/schema/security/spring-security-3.0.xsd=org/springframework/security/config/spring-security-3.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd=org/springframework/jdbc/config/spring-jdbc-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.1.xsd}
1
从日志输出结果可以看到:即便在logback.xml配置文件中设置了输入级别为info,却仍然有DEBUG级别的日志输出,而且,日志应以“HERE”前缀开头,因为输出格式中有配置:
HERE %d{HH:mm:ss.SSS} [%thread] %-5level %C{36} %L - %msg%n
但结果却没有HERE字样的日志输出,这说明logback.xml并未生效,系统使用的是其他的配置文件或缺省的配置(缺省配置级别为DEBUG)。
通过反复的测试,发现竟然是类的加载先后顺序问题。在web.xml中配置了logback.xml加载的文件即加载方式(监听器),见下面的文件:
Archetype Created Web Application
webAppRootKey
Workflow
contextConfigLocation
/WEB-INF/dispatch-servlet.xml,/WEB-INF/conf/spring/*.xml,classpath*:META-INF/conf/spring/*.xml
logbackConfigLocation
/WEB-INF/conf/logback.xml
org.springframework.web.context.ContextLoaderListener
ch.qos.logback.ext.spring.web.LogbackConfigListener
dispatch
org.springframework.web.servlet.DispatcherServlet
1
dispatch
/api/*
解决的办法竟然很简单,那就是将spring 启动的监听器(org.springframework.web.context.ContextLoaderListener)和logback日志配置监听器(ch.qos.logback.ext.spring.web.LogbackConfigListener)掉过个,正确的配置顺序为:
(1)ch.qos.logback.ext.spring.web.LogbackConfigListener
(2)org.springframework.web.context.ContextLoaderListener
修改后的web.xml为:
Archetype Created Web Application
webAppRootKey
Workflow
contextConfigLocation
/WEB-INF/dispatch-servlet.xml,/WEB-INF/conf/spring/*.xml,classpath*:META-INF/conf/spring/*.xml
logbackConfigLocation
/WEB-INF/conf/logback.xml
ch.qos.logback.ext.spring.web.LogbackConfigListener
org.springframework.web.context.ContextLoaderListener
dispatch
org.springframework.web.servlet.DispatcherServlet
1
dispatch
/api/*
正常输出样例:
HERE 14:48:33.510 [localhost-startStop-1] INFO o.s.web.context.ContextLoader 285 - Root WebApplicationContext: initialization started
HERE 14:48:33.643 [localhost-startStop-1] INFO o.s.c.s.AbstractApplicationContext 510 - Refreshing Root WebApplicationContext: startup date [Fri Jul 29 14:48:33 CST 2016]; root of context hierarchy
HERE 14:48:33.720 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader 317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatch-servlet.xml]
HERE 14:48:36.290 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader 317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/conf/spring/px-hessian-client.xml]
HERE 14:48:36.327 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader 317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/conf/spring/px-common.xml]
HERE 14:48:37.545 [localhost-startStop-1] INFO o.s.b.f.s.DefaultListableBeanFactory 822 - Overriding bean definition for bean 'beanFactory': replacing [Generic bean: class [com.nariit.pi6000.framework.platform.PXBeanFactory]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in ServletContext resource [/WEB-INF/dispatch-servlet.xml]] with [Generic bean: class [com.nariit.pi6000.framework.platform.PXBeanFactory]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in ServletContext resource [/WEB-INF/conf/spring/px-common.xml]]
HERE 14:48:37.548 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader 317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/conf/spring/px-ds.xml]
HERE 14:48:37.599 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader 317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/conf/spring/px-bpm.xml]
HERE 14:48:37.972 [localhost-startStop-1] INFO o.s.c.i.s.PropertiesLoaderSupport 172 - Loading properties file from ServletContext resource [/WEB-INF/conf/jdbc.properties]
HERE 14:48:37.973 [localhost-startStop-1] INFO o.s.c.i.s.PropertiesLoaderSupport 172 - Loading properties file from ServletContext resource [/WEB-INF/conf/props/platform.properties]
slf4j+logback需要用到的包:
slf4j1个包:
(1)slf4j-api-1.7.6.jar(必选)
下载地址:http://www.slf4j.org/download.html
Spring slf4j 1个包:
(1)logback-ext-spring-0.1.2.jar(必选)
二进制包下载地址:http://maven.outofmemory.cn/org.logback-extensions/logback-ext-spring/0.1.2/
源码:https://github.com/qos-ch/logback-extensions
loback3个包:
(2)logback-core-1.1.7.jar(必选)
(3)logback-classic-1.1.7.jar(必选)
(4)logback-access-1.1.7.jar(可选)
下载地址:http://logback.qos.ch/download.html
附:
输出格式参考:http://logback.qos.ch/manual/layouts.html
简要参考:
转换符 | 作用 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
c {length } lo {length } logger {length } |
输出日志的logger名,可有一个整形参数,功能是缩短logger名,设置为0表示只输入logger最右边点符号之后的字符串。
|
||||||||||||||||||||||||
C {length } class {length } |
输出执行记录请求的调用者的全限定名。参数与上面的一样。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
contextName cn |
输出上下文名称。 | ||||||||||||||||||||||||
d {pattern } date {pattern } |
输出日志的打印日志,模式语法与java.text.SimpleDateFormat 兼容。
|
||||||||||||||||||||||||
F / file | 输出执行记录请求的java源文件名。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
caller{depth}caller{depth, evaluator-1, ... evaluator-n} | 输出生成日志的调用者的位置信息,整数选项表示输出信息深度。 例如, %caller{2} 输出为: 0 [main] DEBUG - logging statement Caller+0 at mainPackage.sub.sample.Bar.sampleMethodName(Bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest(Bar.java:17) 例如, %caller{3} 输出为: 16 [main] DEBUG - logging statement Caller+0 at mainPackage.sub.sample.Bar.sampleMethodName(Bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest(Bar.java:17) Caller+2 at mainPackage.ConfigTester.main(ConfigTester.java:38) |
||||||||||||||||||||||||
L / line | 输出执行日志请求的行号。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
m / msg / message | 输出应用程序提供的信息。 |
||||||||||||||||||||||||
M / method | 输出执行日志请求的方法名。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
n | 输出平台先关的分行符“\n”或者“\r\n”。 | ||||||||||||||||||||||||
p / le / level | 输出日志级别。 | ||||||||||||||||||||||||
r / relative | 输出从程序启动到创建日志记录的时间,单位是毫秒 | ||||||||||||||||||||||||
t / thread | 输出产生日志的线程名。 | ||||||||||||||||||||||||
replace(p ){r, t} | p 为日志内容,r 是正则表达式,将p 中符合r 的内容替换为t 。 例如, "%replace(%msg){'\s', ''}" |