开发者

log4j displaying log messages using %p

now for a particular log message for the %p sign i wil get either debug or er开发者_如何学Pythonror or warning. can anyone tel me how to get only the first three letters. eg. only WAR for Warning


In log4j 2.x, you can truncate from the end:

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.

Link: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

So in this case, %.-3p should give WAR instead of WARNING.


The code that executes the substring is in org.apache.log4j.helpers.PatternConverter class, format method:

if(len > max)
    sbuf.append(s.substring(len-max));

If you'd like to print out WAR, you should subclass PatternConverter class changing the substring line into format method to something like this:

if(len > max)
    sbuf.append(s.substring(0, max));

Then you should also write a new version of PatternLayout#createPatternParser method, which has to instantiate your new version of PatternConverter.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜