Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit max message size in log4j2 pattern

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?

like image 921
dux2 Avatar asked Dec 16 '14 10:12

dux2


People also ask

What is pattern layout in Log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

What is rolling file Appender in Log4j2?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

Which is better Log4j or Log4j2?

Conclusion. Log4j, Logback, and Log4j2 are good logging frameworks that are broadly used. So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks.


1 Answers

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>
like image 147
Steve S. Avatar answered Oct 23 '22 04:10

Steve S.