我在构建路径中添加了来自here的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
答案 0 :(得分:0)
我认为您的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>
答案 1 :(得分:0)
您的配置位于log4j2 format中,这是有效的,但是您需要在log4j2.xml
中声明您的配置。