相关文章推荐
帅气的面包  ·  oracle 11G ...·  1 年前    · 
茫然的筷子  ·  「Python数据分析」 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a class that extends LayoutBase. In the doLayout method, I add a key to my logging called MSG and the value is set to ILoggingEvent event.getMessage().

I'm seeing values added to my logging but they're not consistent; some logging messages and some exception stack traces.

Can anyone tell me where ILoggingEvent event.getMessage() gets its value from?

Please refer to Logback architecture

It mentions that each log will go through series of step

Get Filter Chain Decision
If it exists, the TurboFilter chain is invoked. Turbo filters can set a context-wide threshold, or filter out certain events based on information such as Marker , Level , Logger , message , or the Throwable that are associated with each logging request.

Apply the selection rule
At this step, logback compares the effective level of the logger with the level of the request. If the logging request is disabled according to this test, then logback will drop the request without further processing.

Create a LoggingEvent object
If the request survived the previous filters, logback will create a LoggingEvent object containing all the relevant parameters of the request

Invoking appenders
After the creation of a LoggingEvent object, logback will invoke the doAppend() methods of all the applicable appenders , that is, the appenders inherited from the logger context.

Formatting the output It is the responsibility of the invoked appender to format the logging event. However, some (but not all) appenders delegate the task of formatting the logging event to a layout.

Sending out the LoggingEvent
After the logging event is fully formatted it is sent to its destination by each appender.

I'm seeing values added to my logging but they're not consistent; some logging messages and some exception stack traces.

Now, coming to your query, it seems that you are receiving events which have exception information ( Throwable ) during the formatting step .

You can create a CustomFilter to filter out such events. All you have to do is extends Filter<ILoggingEvent>

public class DenyExceptionFilter extends Filter<ILoggingEvent> {
    @Override
    public FilterReply decide(ILoggingEvent iLoggingEvent) {
        final IThrowableProxy throwableProxy = iLoggingEvent.getThrowableProxy();
        if (throwableProxy != null && throwableProxy instanceof ThrowableProxy) 
            return FilterReply.DENY;
        return FilterReply.ACCEPT;

This can be much more powerful, can filter specific types of exception. You can take this as your homework :P

Then you can add this Custom Filter to your appender as

<appender name="APPENDER_NAME" class="ch.qos.logback.classic.AsyncAppender">
    <filter class="com.stackoverflow.DenyExceptionFilter" />
</appender>

Of course, add your layout as well after the filter.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.