7

In log4j 2, I would like to trim the end of messages written to the console appender when size is above a specified threshold.

I looked at the http://logging.apache.org/log4j/2.0/manual/layouts.html#PatternLayout docs but can see no option to truncate the end of the "msg" field.

"%.1000msg" will leave only the last 1000 chars of the message.

This is not good for me because in Java the most inner frames in stack trace are printed at the beginning of the message.

Any idea?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
dux2
  • 1,770
  • 1
  • 21
  • 27

2 Answers2

9

I think you are looking for: %.-1000m

Here is an complete example for a console logger:

<Configuration status="WARN" monitorInterval="60" name="DEVELOPMENT">

  <Properties>
    <Property name="baseDir">logs</Property>
  </Properties>

  <Appenders>
    <Console name="CONSOLE">
      <PatternLayout pattern="%p{length=1} | %-10.-10t | %d{HH:mm:ss,SSS} | %.-1000m (%c{2}:%L) %n"/>
    </Console>    
  </Appenders>

  <Loggers>     
    <Root level="TRACE">
      <AppenderRef ref="CONSOLE" level="DEBUG"/>
    </Root>       
  </Loggers>

</Configuration>
Steve S.
  • 923
  • 2
  • 9
  • 19
2

Interesting. I don't think pattern layout currently supports this. I recommend raising a feature request on the Log4j2 issue tracker: https://issues.apache.org/jira/browse/LOG4J2

Remko Popma
  • 35,130
  • 11
  • 92
  • 114