Kenneth Clarke
Kenneth Clarke

Reputation: 139

How to configure log4j to output all levels. (trace not being output)

I'm currently working on a maven application that uses log4j and I want to be able to output all log levels to the console. I've configured as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="ERROR">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" level="TRACE">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%7pid] %-5p --- myapp              %-42.42c{1.} : %m%n" />
            <LevelRangeFilter maxLevel="ALL" onMatch="ACCEPT" onMismatch="DENY"/>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="INFO">
            <!-- Console can still output logs from higher log levels. -->
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

The I have a simple rest call that outputs each level to the console.

@RequestMapping(value = "/ping", method = RequestMethod.GET)
    public ResponseEntity<?> processPing() throws Exception {

        log.trace("trace");
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        log.error("error");

        return new ResponseEntity<>(HttpStatus.OK);
    }

For the output I get everything except trace. When I change the LevelRangeFilter maxlevel value I get get the expected output but not when I set it to trace or all.

I've been up able to get trace to output. I've tried changing the root logger level, creating a custom logger, and removing the levelRangeFilter completely. None of it has worked.

Update: I found that if I call log.isTraceEnabled() it returns false. I'm currently trying to figure out how to enable it.

Upvotes: 0

Views: 847

Answers (1)

queeg
queeg

Reputation: 9473

You code emits log messages on all different levels. That is good.

Now the log4j configuration has a filter that still applies and it is on the category.

While you configured the appender's level to be TRACE, that setting is log4j1 history and ignored in log4j2. But the root logger's level is set to INFO. With that setting you will only see INFO, WARN, ERROR and FATAL.

-> Set the root logger to ALL or at least TRACE.

Upvotes: 1

Related Questions