How to display Just D, I, E, F instead of DEBUG, INFO, ERRO, FATAL in log4j

Viewed 29

We are trying to optimize the size of files generated by log4j2. So I was thinking if we can reduce the number of characters that get printed on the final .log file. Below is the current pattern layout

log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %p [%F] %m%n

Which generates the below logs

07:37:31,416 DEBUG [FileUtils.java] bla bla bla

What I want is the below to get displayed instead

07:37:31,416 D [FileUtils.java] bla bla bla

What can I try to resolve this?

1 Answers

Have a look at the log4j documentation: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

It mentions two ways to achieve your desired output. Include one of the following strings in you pattern configuration instead of the %p:

%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}
%level{length=1}

The first one will map the levels explicitly, while the second will cut the label length to 1. Either one will work. Note, that %p is just short-hand notation for %level.

Your full configuration property will look as follow using the second option with the abbreviation %p instead of %level:

log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %p{length=1} [%F] %m%n
Related