Skip to content

Logger additivity setting

In logging frameworks such as Log4j2, additivity determines whether a given Logger should also delegate its logging events to the parent Logger (usually the Root Logger).^[600-developer__java__3-party__java-log4j2.md#L48-68]

By default, logging hierarchies typically allow events to propagate up, meaning a log event handled by a specific appender might also be captured by the root appender.^[600-developer__java__3-party__java-log4j2.md#L48-68] The additivity setting allows this inheritance behavior to be controlled on a per-logger basis.

Configuration

In XML configuration, the additivity attribute is applied to a specific <Logger> element.^[600-developer__java__3-party__java-log4j2.md#L48-68]

  • additivity="false": The logger will not pass log events to the root logger. Output is restricted strictly to the appenders referenced within that specific logger's definition.^[600-developer__java__3-party__java-log4j2.md#L48-68]
  • additivity="true" (or omitted): The logger will pass log events to the root logger, potentially causing the event to be logged by both the specific appender and the root appender(s).^[600-developer__java__3-party__java-log4j2.md#L48-68]

Example

In the following configuration, the loggers named "main", "bolt", and "spout" all have additivity="false" set.^[600-developer__java__3-party__java-log4j2.md#L48-68] This ensures that logs written to the specific files (main.log, bolt.log, etc.) are not duplicated in the console appender defined in the <Root> section.^[600-developer__java__3-party__java-log4j2.md#L48-68]

<Loggers>
    <Logger name="main" additivity="false">
        <AppenderRef ref="main"/>
    </Logger>
    <Logger name="bolt" additivity="false">
        <AppenderRef ref="bolt"/>
    </Logger>
    <Logger name="spout" additivity="false">
        <AppenderRef ref="spout"/>
    </Logger>

    <Root level="all">
        <AppenderRef ref="Console"/>
    </Root>
</Loggers>

Sources

^[600-developer__java__3-party__java-log4j2.md]