spring3中使用SLF4J
在SPRING中使用SLF4J进行日志记录,在非WEB的模式下可以通过如下方法进行:
?private static Logger log = LoggerFactory.getLogger(TestSlf4jLog.class);
?public static void main(String[] args) throws Exception {
??String logbackCfg = "resources/logback.xml";
??URL logURL = new ClassPathResource(logbackCfg).getURL();
??ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
??LoggerContext loggerContext = (LoggerContext) loggerFactory;
??JoranConfigurator configurator = new JoranConfigurator();
??configurator.setContext(loggerContext);
??loggerContext.stop();
??configurator.doConfigure(logURL);
??log.debug("debug");
??String str = "this is info";
??log.info("{}", str);
??String str2 = "警告";
??log.warn("这是一个{}", str2);
?}
?
而在WEB模式下,可以通过增加一个LISTENER来实现在WEB.XML中增加如下配置:
?
<context-param>
??<param-name>logbackConfigLocation</param-name>
??<param-value>classpath:resources/logback.xml</param-value>
?</context-param>
?
?
<listener>
??<listener-class>ch.qos.logback.classic.servlet.LogbackConfigListener</listener-class>
?</listener>
?
以上LISTENER的类在附件中可以找到!
?
?