Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a logback message field be truncated/trimmed to n characters?

Tags:

logback

Sometimes see huge log messages and do not always have the ability to (easily) turn of word wrapping.

Is there a way to truncate %message to, say, 80 characters via logback.xml?

like image 404
Sridhar Sarnobat Avatar asked Sep 21 '15 20:09

Sridhar Sarnobat


People also ask

What is root level in Logback?

The possible levels are, in order of precedence: TRACE, DEBUG, INFO, WARN and ERROR. Each level has a corresponding method that we use to log a message at that level. If a Logger isn't explicitly assigned a level, it inherits the level of its closest ancestor. The root logger defaults to DEBUG.

How does Logback XML work?

Logback executes an async appender in a separate thread to decouple the logging overhead from the thread executing your code. Using the async appender is incredibly easy. Refer the appender that should be asynchronously invoked within an <appender> element.

What is Logback in Java?

Logback is a logging framework for Java applications, created as a successor to the popular log4j project. In fact, both of these frameworks were created by the same developer.


1 Answers

Have a look at the format modifiers section:

From http://logback.qos.ch/manual/layouts.html#formatModifiers:

Format modifiers

By default the relevant information is output as-is. However, with the aid of format modifiers it is possible to change the minimum and maximum width and the justifications of each data field.

...

Truncation from the end is possible by appending a minus character right after the period. In that case, if the maximum field width is eight and the data item is ten characters long, then the last two characters of the data item are dropped.

like image 133
Adrian Lynch Avatar answered Sep 21 '22 21:09

Adrian Lynch