paul
paul

Reputation: 4487

Log4j logging not working

I have added log4j from here in build path. Wrote a simple method to test log levels but it doesn't print any thing console.

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>

Actual output:

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.

Expected Output:

Warn Message!
Error Message!
Fatal Message!

Above Example link: http://www.tutorialspoint.com/log4j/log4j_logging_levels.htm

Upvotes: 5

Views: 14813

Answers (3)

user2248785
user2248785

Reputation: 425

Quite late to the party here. Specifically on the error:

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

I found that changing:

-Dlog4j2.configuration=...

to:

-Dlog4j2.configurationFile=...

fixed the issue.

Upvotes: 1

zwessels
zwessels

Reputation: 637

Your config is in log4j2 format, which is valid, but you need to declare your configuration in log4j2.xml.

Upvotes: 1

Dams
Dams

Reputation: 997

I think your xml is not valid ... your Actual output :

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

I think you should define your xml like this :

<?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>

Upvotes: 0

Related Questions