Eclipse maven工程中如何配置slf4j+junit
阿新 • • 發佈:2019-01-03
Author: kagula
Last Update Date: 2016-9-7
環境:
[1]Eclipse Mars.2
[2]JDK 1.7
[3]slf4j-log4j12 1.7.2
[4]junit 4.11
Introduction
這裡記下免得每次為web app配log4j又得摸索一遍。
第一步:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nuoke</groupId> <artifactId>icbc_gbk</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>icbc_gbk Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>GBK</project.build.sourceEncoding> <project.reporting.outputEncoding>GBK</project.reporting.outputEncoding> <spring.version>3.1.2.RELEASE</spring.version> <cxf.version>3.0.9</cxf.version> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> <build> <finalName>icbc_gbk</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>GBK</encoding> </configuration> </plugin> </plugins> </build> </project>
第二步:web.xml
<?xml version="1.0" encoding="GBK"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>ICBC</display-name> <!-- Log4j--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- CXF --> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
第三步:log4j.xml
放在[Project Explorer]->[...]->[Java Resources]
->[src/main/resources]節點下面
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="appendConsole" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss:SSS} %l %m%n" /> </layout> <!--限制輸出級別 --> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMax" value="FATAL" /> <param name="LevelMin" value="DEBUG" /> </filter> </appender> <appender name="myFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="/log/escortcashbox.log" /><!-- 設定日誌輸出檔名 --> <!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 --> <param name="Append" value="true" /> <param name="MaxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /> </layout> </appender> <logger name="org.springframework"> <level value="warn" /> <appender-ref ref="appendConsole"/> </logger> <root> <priority value="debug" /> <appender-ref ref="appendConsole" /> <appender-ref ref="myFile"/> </root> </log4j:configuration>
第三步:junit測試
在...[src/test/java]節點下面
package com.nuoke.test;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyTest {
@Test
public void TestSlf4j()
{
//junit app預設會從classes路徑下找log4j.xml
//為了讓web app用同一個log4j.xml檔案
//web app的 web.xml裡指定log4j.xml位置也在classes路徑下。
Logger logger = LoggerFactory.getLogger(MyTest.class);
logger.info("Hello World");
int a = 1000;
String b = "test";
logger.debug("a = {}. b = {}.", a, b);
}// end function
}//end class