logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From WangMaple <maple.feng.w...@hotmail.com>
Subject how to suppress stacktrace for PatternLayout in ConsoleAppender in Log4j 2.x
Date Fri, 18 Mar 2016 14:34:46 GMT
Hi,

 

I'm trying to realize a feature which can programmatically
suppress exception's stacktrace for PatternLayout in ConsoleAppender in Log4j
2.x.  Actually it’s very easy to do that in Log4j 1.x, I just extend the
PatternLayout like below:

 

public class MyPatternLayout extends PatternLayout {

…

        Public void
format(LoggingEvent event) {

               
// according to event to decide if ignores throwable

        }

 

        Public Boolean
ignoresThrowable() {

               
Return ignoresThrowable;

        } 

…

}

 

Then replace old PatternLayout with MyPatternLayout.

 

But in Log4j 2.x(mine is log4j 2.5), PatternLayout has no
such interfaces for usage anymore, the pattern like “%throwable{none}” can
suppress the stacktrace, but it’s not programmatic way I expect. 

 

I guess ThrowablePatternConverter is used to handle the
throwables instead here, but after several attempts, I come up with some
questions:

 

1 intially, I thought handlesThorwable() in
ThrowablePatternConverter is similar with ignoresThrowable(), but I’m wrong,
it’s not working as I expect, the customized converter as below:

@Plugin(name = "MyThrowablePatternConverter",
category = "Converter")

@ConverterKeys({ "myThrowable" })

public class MyThrowablePatternConverter extends ThrowablePatternConverter
{

 

    protected MyPatternConverter(String name,
String style, String[] options) {

        super(name,
style, options);

    }

 

    public static MyPatternConverter
newInstance(final String[] options) {

        return new
MyPatternConverter("MyThrowable", "throwable", options);

    }

 

    @Override

    public void format(LogEvent event,
StringBuilder buffer) {

       
super.format(event, buffer);

    }

 

    @Override

    public boolean handlesThrowable() {

        return false;

    }

 

}

I let handlesThrowable() return false and use
“%myThrowable{full}” in my configuration file, but when I insert the exception
in logging API, the stacktrace is still printed. What should I do to suppress
it?

 

2 I understand that the Pattern converter must come up with
converter key, but is it possible for user to modify the behavior of built-in
PatternConverter? There are three built-in throwable-related PatternConverter
as I know:   ThrowablePatternConverter and its subclass ExtendedThrowablePatternConverter
and RootThrowablePatternConverter, the according keys are like throwable,
xThrowable and rThrowable, I want to reuse these keys but works in my expected
way. Is that possible?

 

Thanks in advance.

 

Best regards. 		 	   		  
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message