簡體   English   中英

Log4j加入配置文件並將所有內容打印到控制台中

[英]Log4j ingores configuration files and prints everything into console

我正在嘗試使用Log4j記錄信息。 我創建了log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file, stdout
# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\logging.log
log4j.appender.file.MaxFileSize=12MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# configuration to print on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

包括記錄器:

private static final Logger logger = LogManager.getLogger(UserController.class);

並試圖記錄:

   logger.debug("Debugging log");
    logger.info("Info log");
    logger.warn("Hey, This is a warning!");
    logger.error("Oops! We have an Error. OK");
    logger.fatal("Damn! Fatal error. Please fix me.");

然而一切都記錄在控制台中(並忽略調試)。

我的屬性文件位於\\src\\main\\resources文件夾中,我嘗試過使用基於xml的配置,但結果是一樣的,將它打印到文件中沒有運氣。

我在配置上有任何錯誤嗎? 或者問題的根源在哪里?

感謝幫助

編輯:

我正在使用maven和spring-boot,我有這個依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

請閱讀此文檔以獲取自定義文件命名https://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-custom-log-levels

只是為了確定,我假設您使用的是Log4j而不是Log4j2

首先,根日志記錄級別INFO不正確。 在這種情況下應該是DEBUG 關於級別層次結構,這里有一個很好的SO帖子log4j日志記錄層次結構順序

因此,根記錄器應如下所示

log4j.rootLogger=DEBUG, file, stdout

最后,似乎排除XML不正確。 您將在此處找到配置Log4j for logging doc https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html請注意,在本文檔中,引用的是Log4j2。 對於我們的情況,我們需要添加Log4J依賴項。 這是我的XML:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM