0

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.

Kenneth Clarke
  • 119
  • 1
  • 1
  • 6
  • 1
    Hmm. Shouldn't all XML elements be lower case? And setting the root level to TRACE should usually suffice. If this is a Spring application, check your `application.properties` file too (and try `logging.level.root=TRACE` there) – knittl Sep 26 '22 at 14:45
  • Adding logging.level.root=TRACE caused a lot more spring related output to the console, but still didn't output my trace. – Kenneth Clarke Sep 26 '22 at 15:02
  • 1
    `logging.level.root` works **until** Spring reads your configuration. To output all log messages after that point you need to modify your ``'s level. – Piotr P. Karwasz Sep 26 '22 at 15:25
  • I've tried adding logging.level.root=TRACE to the spring properties and changing the root's level to all. Again I get a lot of trace outputs from spring itself but none from my code. – Kenneth Clarke Sep 26 '22 at 15:43

1 Answers1

1

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.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Log4j2 appenders, unlike their Log4j 1.x predecessors do not have a `level` property. That part of the configuration is ignored (causes a warning on the status logger). Log4j2 also does not have a `FINE*` level (those names are specific to `java.util.logging`) – Piotr P. Karwasz Sep 26 '22 at 15:34
  • Seems the level on appenders has changed into filters. https://stackoverflow.com/questions/18958614/union-to-jpa-query – Queeg Sep 26 '22 at 15:45
  • The closest equivalent of an appender's level is the `level` setting for `AppenderRef`s. – Piotr P. Karwasz Sep 26 '22 at 17:36