Log4j日志记录不工作

ars1skjm  于 2023-08-05  发布在  其他
关注(0)|答案(3)|浏览(120)

我在构建路径中添加了来自herelog4j。写了一个简单的方法来测试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

qxgroojn

qxgroojn1#

您的配置在log4j2 format中,这是有效的,但您需要在log4j2.xml中声明配置。

fxnxkyjh

fxnxkyjh2#

我认为你的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>

xoshrz7s

xoshrz7s3#

你来参加派对已经很晚了。具体关于错误:

log4j:ERROR DOM element is - not a <log4j:configuration> element.

字符串
我发现这种变化:

-Dlog4j2.configuration=...


致:

-Dlog4j2.configurationFile=...


解决了问题。

相关问题