簡體   English   中英

Log4j 日志記錄不起作用

[英]Log4j logging not working

我在構建路徑中從這里添加了log4j 寫了一個簡單的方法來測試log levels ,但它不打印任何內容控制台。

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LoggerLevelTest {

    private static org.apache.log4j.Logger log = Logger
            .getLogger(LoggerLevelTest.class);

    public static void main(String[] args) {
        log.setLevel(Level.WARN);

        log.trace("Trace Message!");
        log.debug("Debug Message!");
        log.info("Info Message!");
        log.warn("Warn Message!");
        log.error("Error Message!");
        log.fatal("Fatal Message!");
    }

}

Log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="LEVEL">WARN</Property> <!-- default value -->
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="${sys:LEVEL}">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

實際輸出:

log4j:WARN Continuable parsing error 2 and column 30
log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
log4j:WARN Continuable parsing error 2 and column 30
log4j:WARN Document is invalid: no grammar found.
log4j:ERROR DOM element is - not a <log4j:configuration> element.
log4j:WARN No appenders could be found for logger (LoggerLevelTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

預期輸出:

Warn Message!
Error Message!
Fatal Message!

以上示例鏈接: http ://www.tutorialspoint.com/log4j/log4j_logging_levels.htm

您的配置是log4j2 格式,這是有效的,但您需要在log4j2.xml中聲明您的配置。

我認為您的 xml 無效...您的實際輸出

log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
...
log4j:ERROR DOM element is - not a <log4j:configuration> element.

我認為你應該這樣定義你的 xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="xyz" ... />

    <root> 
       ...
    </root>

</log4j:configuration>

暫無
暫無

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

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