LogBack是由Log4j的同一作者创建的用于记录日志的API(较新的实现,它类似于新版本),在本文中,我将展示如何在Spring项目中对其进行集成和使用。
在本教程中,我假设您正在使用一个简单的Spring ROO项目,该项目将为您准备项目的所有结构,有关更多信息,请参见: http : //www.springsource.org/spring-roo 。
<?xml version="1.0" encoding="UTF-8"?><configuration><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern></encoder></appender><logger name="test.myapp.repos"><level value="INFO" /></logger><logger name="org.springframework"><level value="INFO" /></logger><root><level value="INFO" /><appender-ref ref="CONSOLE" /></root></configuration>
第二步是配置Maven依赖项并添加LogBack所需的库:
<-- Properties Settings --><properties><roo.version>1.1.0.RELEASE</roo.version><spring.version>3.0.5.RELEASE</spring.version><aspectj.version>1.6.10</aspectj.version><slf4j.version>1.6.1</slf4j.version><logback.version>0.9.26</logback.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><-- Dependencies Settings --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>${logback.version}</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>${logback.version}</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-access</artifactId><version>${logback.version}</version></dependency>
您需要摆脱由Spring ROO生成的Maven pom.xml中的所有Log4j依赖关系,在添加提供的用于设置LogBack的代码之前,清除与日志记录相关的每个依赖关系。
为了在正在开发的类上使用记录器,您需要创建它的静态实例并在使用Log4J时正常使用,唯一的区别是LogBack与Log4j的实现和配置代码。 在Logback.xml上,必须扫描您的类以使记录器正常工作。
package test.myapp.repos; /*This package figures on LogBack.xml*/import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class MyTestClass {static Logger logger = LoggerFactory.getLogger(ItemController.class);...public void create(String args) {logger.debug("My Args Is => " + args);}...}
/** Custom @Logger annotation **/@Retention(RUNTIME)@Target(FIELD)@Documentedpublic @interface Log { }/** LoggerPostProcessor => Custom Spring BeanPostProcessor **/public class LoggerPostProcessor implements BeanPostProcessor {public Object postProcessAfterInitialization(Object bean, String beanName) throwsBeansException {return bean;}public Object postProcessBeforeInitialization(final Object bean, String beanName)throws BeansException {ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {@SuppressWarnings("unchecked")public void doWith(Field field) throws IllegalArgumentException,IllegalAccessException {ReflectionUtils.makeAccessible(field);//Check if the field is annoted with @Logif (field.getAnnotation(Log.class) != null) {Log logAnnotation = field.getAnnotation(Log.class);Logger logger = LoggerFactory.getLogger(bean.getClass());field.set(bean, logger);}}});return bean;}}/** Usage on a Spring Bean **/@Componentpublic class MyBeanImpl implements MyBean {/** Not manual set up code **/@Log Logger myLogger;...}
最后要做的是在您的applicationContext.xml文件中声明这个新的BeanPostProcessor:
<!-- The Logger Injector --><bean id="LogginInjector" class="ma.oncf.achat.utils.LoggerPostProcessor" />
有关为什么切换到LogBack的更多信息,请参阅: 为什么切换到LogBack 。
参考:来自Fancy UI博客的JCG合作伙伴 Idriss Mrabti使用Spring配置LogBack Logging 。
相关文章 :
- Spring 3.1和Hibernate的持久层
- 使用Spring和Java泛型简化数据访问层
- Spring陷阱:事务测试被认为是有害的
- Log4j,Stat4j,SMTPAppender集成–汇总错误日志以发送太多电子邮件
- Java日志混乱
- 正确记录应用程序的10个技巧
- 首先记录异常的根本原因
翻译自: https://www.javacodegeeks.com/2011/12/configure-logback-logging-with-spring.html