Skip to content

[perf] Caller data is still extracted when AsyncAppender.includeCallerData = false and layout contains %C, %M or %L #1059

Description

@leeychee

When AsyncAppender.includeCallerData = false and layout contains %C, %M or %L:

  1. Data extraction is skipped in business threads;
  2. However, caller data is still extracted in the logback thread by AsyncAppenderBase.Worker;
  3. Since the caller data is extracted from logback thread,%C, %M and %L all convert to ?;
  4. This results in MEASURABLE CPU overhead if the system owner forgets to remove the %C, %M or %L from the layout.
// Pass
@Test
public void settingNotIncludeCallerDataPropertyCausedCallerDataToBeNotIncluded() {
    asyncAppender.addAppender(listAppender);
    asyncAppender.setIncludeCallerData(false);
    asyncAppender.start();
    ILoggingEvent event = builder.build(diff);
    asyncAppender.doAppend(event);
    asyncAppender.stop();
    assertFalse(event.hasCallerData());
}

// Fail
@Test
public void settingNotIncludeCallerDataPropertyCausedCallerDataToBeNotIncluded_withL() {
    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setContext(loggerContext);
    patternLayoutEncoder.setPattern("%L");
    patternLayoutEncoder.start();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamAppender<ILoggingEvent> outputStreamAppender = new OutputStreamAppender<>();
    outputStreamAppender.setContext(loggerContext);
    outputStreamAppender.setName("oss");
    outputStreamAppender.setEncoder(patternLayoutEncoder);
    outputStreamAppender.setOutputStream(baos);
    outputStreamAppender.start();

    asyncAppender.addAppender(outputStreamAppender);
    asyncAppender.setIncludeCallerData(false);
    asyncAppender.start();

    ILoggingEvent event = builder.build(diff);
    asyncAppender.doAppend(event);
    asyncAppender.stop();

    assertFalse(event.hasCallerData());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions