spring中使用logback日志组件替换log4j
logback比log4j的强大之处,请到logback的主页去看,我就不啰嗦了,你懂、或者不懂,logback就在那里,无比强大,傲视绝伦。
复制log4j-over-slf4j.jar,logback-classic.jar,logback-core.jar,jcl-over-slf4j.jar到lib目录,删除原有的log4j.jar。
创建一个新类
package com.boaotech.util; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; /** * @author Kiven Lee * @version 1.0 */ public class LogbackConfigListener implements ServletContextListener { private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class); private static final String CONFIG_LOCATION = "logbackConfigLocation"; @Override public void contextInitialized(ServletContextEvent event) { //从web.xml中加载指定文件名的日志配置文件 String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION); String fn = event.getServletContext().getRealPath(logbackConfigLocation); try { LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory(); loggerContext.reset(); JoranConfigurator joranConfigurator = new JoranConfigurator(); joranConfigurator.setContext(loggerContext); joranConfigurator.doConfigure(fn); logger.debug("loaded slf4j configure file from {}", fn); } catch (JoranException e) { logger.error("can loading slf4j configure file from " + fn, e); } } @Override public void contextDestroyed(ServletContextEvent event) { } }
在web.xml中加入
<!--初始化日志配置文件 --> <listener> <listener-class> com.boaotech.util.LogbackConfigListener </listener-class> </listener> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>WEB-INF/logback.xml</param-value> </context-param>
转至用于学习:http://blog.csdn.net/kivenlee/article/details/6284792
相关推荐
chw0 2020-11-04
MrLiar 2020-05-14
sdaq 2020-05-11
JavaWinner 2020-03-04
丨Fanny丨Cri 2020-02-23
MrLiar 2020-02-16
丨Fanny丨Cri 2020-02-01
sdaq 2020-01-19
zhangxiaocc 2020-02-23
sdaq 2020-01-10
dxyadc 2020-01-08
wangruiling 2020-01-07
itmale 2020-01-06
方志朋 2020-01-04
sdaq 2019-12-28
wangruiling 2014-05-18
dongxurr 2019-12-27
JavaWinner 2019-12-12