0
votes

I am using log4j2 in my Java project and would like to filter for error logs on the linux console.

log4j2 is configured as followed:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Properties>
    <Property name="pattern">[%d{yyyy-MM-dd HH:mm:ss.SSS}][%highlight{%-5level}][%t][%logger] %msg%n</Property>
  </Properties>
  <Appenders>
    <Console name="stdout" target="SYSTEM_OUT"> <!-- 1 -->
      <PatternLayout pattern="${pattern}"/>
      <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/> <!-- 3 -->
    </Console>
    <Console name="stderr" target="SYSTEM_ERR"> <!-- 2 -->
      <PatternLayout pattern="${pattern}"/>
      <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <!-- 3 -->
    </Console>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="stdout"/> <!-- 4 -->
      <AppenderRef ref="stderr"/> <!-- 4 -->
    </Root>
  </Loggers>
</Configuration>

(found here: https://rmannibucau.metawerx.net/log4j2-error-log-event-on-stderr.html)

When I run my program via command line

java -jar program.jar 2>&1 >/dev/null

stdout is redirected to /dev/null while stderr is printed onscreen. (OK)

Strangely my LOGGER.error("...") trace is also redirected to /dev/null (which means it is logged on linux stdout) (NOK) while displayed in red (error) on my eclipse console (OK).

Am I missing something or why is my log4j2 stderr logger not logging to the linux stderr?

1
Redirects are read from right to left, so in your case stdout will be redirected to dev/null and stderr to stdout.To redirect only stderr to dev null use java -jar program.jar 2>/dev/null - LMC
I would like to redirect stdout to /dev/null and only print stderr But the program itself should print every log (info and higher) - Terel

1 Answers

0
votes

My logj42.xml was not included in the classpath. That's why the output of all log messages went to linux's stdout.