4

I am using log4j2 with this 2 dependencies:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.6.2</version>
    </dependency>

When I try to log for example an error with a throwable like:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

public class Test {

private static final Logger logger = LogManager.getLogger(Test.class);

@Test
public void testSendMessage() throws Exception {
    Exception exception = new Exception("some exception");
    logger.error("error with exception", exception);
}
}

using patternlayout:

<Configuration>
<properties>
    <property name="filters">org.testng,org.apache.maven,sun.reflect,java.lang.reflect</property>
</properties>

<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" direct="true">
            <PatternLayout pattern="%maxLen{%d{DEFAULT} [%p]  %c{-3}:%L - %enc{%m} %xEx{filters(${filters})}%n}{200}"/>
        </Console>
</Appenders>

<Loggers>
    <logger name="my.test.class.path" level="trace" additivity="false">
        <AppenderRef ref="ConsoleAppender" />
    </logger>
</Loggers>
</Configuration>

Then the filtered packages won't disappear from the stacktrace, I can't even manipulate the stacktrace in any way like maximizing the lines:

%xEx{5}

Highlightning also don't work in eclipse nor in Kibana(ELK environment).

Can anybody help?

Zoltan
  • 162
  • 1
  • 14

3 Answers3

2

Could it be that the %xEx PatternLayout converter doesn't support property substitution in its options?

What if you put the packages you want to filter directly in the filters list?

It may be worth raising a Jira ticket on the Log4j 2 issue tracker for this.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • The example is absolutely copied from the log4j2 documentation site: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout Filtered Throwables section direct packge list doesn't work either %xEx{filters(org.testng,org.apache.maven,sun.reflect,java.lang.reflect)} – Zoltan Sep 28 '16 at 13:49
  • Then you may have found a bug. Please raise this on the Log4j 2 issue tracker. – Remko Popma Sep 28 '16 at 14:00
0

try to remove the ending{200}. i think there is an issue using more sub parameters.

here is a snippet which works. please note if you add additional things like separator(|) it will stop working [tested in version 2.8.1 and 2.9.1]

<Properties>
   <Property name="exfilters">org.jboss,java.lang.reflect,sun.reflect</Property>
   <Property name="log-pattern">%d %-5p %m%n%xEx{filters(${exfilters})}/Property>
</Properties>
...
<PatternLayout pattern="${sys:log-pattern}"/>
tcmj
  • 1
  • 1
0

Works for me on Log4j2 v 2.17 (and also for rThrowable):

<Properties>
    <Property name="PACKAGE_FILTER">org.jboss,java.lang.reflect,sun.reflect</Property>
    <Property name="LOG_PATTERN">%xThrowable{filters(${PACKAGE_FILTER})}</Property>
</Properties>

<Appenders>
   <Console name="STDOUT" target="SYSTEM_OUT">
       <PatternLayout pattern="${LOG_PATTERN}"/>
   </Console>
</Appenders>
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53