1

Is it possible to configure log4j2 in such a way that the stacktraces don't show the source and maybe not the line numbers?
In case you are wondering why I would want that, I need this for unit tests, that need to test if exceptions get logged correctly. So if for example the JRE version is not the same as the one I copied the original stacktrace, from the tests fail. So if the source wasn't included in the stacktrace it would be a lot more version independend.

Example:

Currently a line from the stacktrace looks like this:

    at java.lang.Object.wait(Object.java:502) ~[?:1.8.0_144]

I would like it to look like this (but only for log4j2):

    at java.lang.Object.wait(Object.java:502)

Maybe even like this:

    at java.lang.Object.wait(Object.java)

Or even shorter:

    at java.lang.Object.wait

(Any of the shortened versions would be acceptable. As currently only the source (~[?:1.8.0_144] vs ~[?:1.8.0_151] for example) differs.)

Update:

I just found this in the offical documentation: https://logging.apache.org/log4j/2.x/manual/layouts.html#Patterns I think using %ex{full} should be what I want. Will test it.

There are also several other available formats.

BrainStone
  • 3,028
  • 6
  • 32
  • 59
  • I'm not sure what your exact requirements are, but I would think that as long as you test for the exception class name and message together with the basic format of stack trace items that should be enough. I'm not sure why you would want to compare the entire stack trace if all you care about is that the exception was logged with its trace. – D.B. Nov 16 '17 at 16:57

2 Answers2

1

To achieve the wanted effect, the pattern needs to be changed.

For example if it was

... %msg%n

It needs to be

... %msg%n%ex{full}

(Read https://logging.apache.org/log4j/2.x/manual/layouts.html#Patterns for more info)

BrainStone
  • 3,028
  • 6
  • 32
  • 59
0

I don't think it's possible. The stacktrace comes from the JVM, log4j just prints whatever it is given.

pecks
  • 318
  • 1
  • 2
  • 9