網頁

2018/8/30

Java 建立簡單的SLF4J + Log4j 2專案

在Eclipse中建立一個簡單的SLF4J搭配Apache Log4j 2專案的範例如下

在Eclipse中建立一個簡單的Maven專案

Maven專案建好後開啟pom.xml

加入slf4j及log4j2的Maven dependency來匯入jar檔。

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.11.0</version>
</dependency>

注意log4j-slf4j-impl.jarlog4j-to-slf4j.jar不能同時加入,否則會造成無限迴圈的錯誤。

Use of the SLF4J adapter (log4j-to-slf4j-2.0.jar) together with the SLF4J bridge (log4j-slf4j-impl-2.0.jar) should never be attempted as it will cause events to endlessly be routed between SLF4J and Log4j 2.

下面為範例的pom.xml設定。

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.abc</groupId>
    <artifactId>simple_maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>simple_maven</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>
</project>

log4j2預設會讀取classpath下的log4j2.properites配置檔。因為我們建立的是Maven專案,而properties檔為靜態檔,依習慣會將靜態檔會放在src/main/resource下,所以們要在src/main/resource下新增一個log4j2.properites

如果你在專案中找不到src/main/resource,可以在專案名稱上按滑鼠右鍵New -> Source Folder。接著在開啟的對話視窗中的[Folder name]欄位填入src/main/resource後按Finish完成。接著在專案目錄下就會多了一個src/main/resource

src/main/resource按滑鼠右鍵 -> New -> Other... -> General > File,然後在[File name]欄位輸入log4j2.properties,然後按Finish完成建立。

log4j2.properties內容設定如下

log4j2.properties

name = myconfig

rootLogger.level = trace
rootLogger.appenderRef.stdout.ref = STDOUT

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:SSS}] [%p] [%c{1}:%L] - %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = info

建立一個main方法來測試。


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {

    private static Logger logger = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        logger.trace("Trace message");
        logger.debug("Debug message");
        logger.info("Info message");
        logger.warn("Warn message");
        logger.error("Error message");
    }
}

main執行後印出結果

[18-Aug-30 22:46:35:315] [INFO] [App:13] - Info message
[18-Aug-30 22:46:35:318] [WARN] [App:14] - Warn message
[18-Aug-30 22:46:35:318] [ERROR] [App:15] - Error message

沒有留言:

張貼留言