spring集成 log4j + slf4j
Posted 花月世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring集成 log4j + slf4j相关的知识,希望对你有一定的参考价值。
以maven web项目为例,
首先、在pom文件引入相关依赖,如下(spring官网文档有介绍):
<dependencies> <!-- spring 相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.2.RELEASE</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies>
spring 内部默认使用的日志框架是: commons-logging(需排除该依赖), 引入“jcl-over-slf4j”依赖,使spring对common-logging的调用转为对slf4j的调用,
需要注意的是: 排除commons-logging依赖后,若不配置其他替代依赖(jcl-over-slf4j),容器初始化时会报错:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
接着编写log4j配置文件log4j.xml,位于classpath路径下(src/main/resources)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Appenders --> <appender name="FILE" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="${project.home}/logs/dispatchmanage.log" /> <param name="append" value="true" /> <param name="maxFileSize" value="100MB" /> <param name="maxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name=\'ConversionPattern\' value=\'[%-5p][%d{yyyy/MM/dd HH:mm:ss}][%l][%m]%n\' /> </layout> </appender> <appender name="Console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name=\'ConversionPattern\' value=\'[%-5p][%d{yyyy/MM/dd HH:mm:ss}][%l][%m]%n\' /> </layout> </appender> <!-- Application Loggers --> <logger name="com.hgs"> <level value="debug" /> <appender-ref ref="FILE" /> </logger> <!-- 3rdparty Loggers --> <logger name="org.springframework.core"> <level value="info" /> </logger> <logger name="org.springframework.beans"> <level value="info" /> </logger> <logger name="org.springframework.context"> <level value="info" /> </logger> <logger name="org.springframework.web"> <level value="info" /> </logger> <!-- SQL Loggers --> <logger name="java.sql.Statement" additivity="true"> <level value="debug" /> </logger> <logger name="java.sql.PreparedStatement" additivity="true"> <level value="debug" /> </logger> <logger name="com.ibatis"> <level value="info" /> </logger> <logger name="com.ibatis.common.jdbc"> <level value="info" /> </logger> <logger name="com.ibatis.sqlmap.engine.impl"> <level value="info" /> </logger> <logger name="java.sql.Connection"> <level value="info" /> </logger> <!-- Root Logger --> <root> <level value="debug" /> <appender-ref ref="Console" /> </root> </log4j:configuration>
到此所有的配置工作就完成了,紧接着就可以在实际代码中编程使用了,如下:
package com.yinz.tool.quartz.test; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @description:调度任务要执行的具体操作, * 该类必须要有一个无参构造器 * @author yinz * 2016-7-7 */ public class SimpleJob implements Job { private Logger logger = LoggerFactory.getLogger(SimpleJob.class); public void execute(JobExecutionContext context) throws JobExecutionException { //JobDataMap dataMap = context.getJobDetail().getJobDataMap(); //JobDetail中的JobDataMap JobDataMap dataMap = context.getMergedJobDataMap(); //trigger和JobDetail的中的JobDataMap的合并 String jobMsg = dataMap.getString("jobMsg"); String triggerMsg = dataMap.getString("triggerMsg"); logger.info(new Date() + " : < " + jobMsg + " >, < " + triggerMsg + " >" ); } }
代码中可以看出,我们是面向slf4j 的api编程,这样就可以方便实现在不同的日志框架间切换而不需修改代码。
以上是关于spring集成 log4j + slf4j的主要内容,如果未能解决你的问题,请参考以下文章