Log4j2: One Generic Parameterized Appender [Type:RollingFile] for Multiple Loggers

251 views Asked by At

Dears, I have question please, I have many services writing in different log files with same log level. What I got from the internet, I need to create appenders and loggers based on the number of services. Now, my question is, can I make one generic parmeterized appender to be used for multiple loggers? I need the parameters in the appender to specify the log file name and its path as well. The below xml snippet for the definition of the appender:

<Appenders>
    <RollingFile name="RollingFile-Appender"
                 fileName="${log-path}/{This one based on service}.log"
                 filePattern="${log-path}/{This one based on service}/{This one based on service}_%d{yyyy-MM-dd}.log">
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
            <Policies>
                 <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${fileSize}" />
            </Policies>
        <DefaultRolloverStrategy>
            <Delete basePath="${log-path}/{This one based on service}" maxDepth="1">
              <IfFileName glob="{This one based on service}*.log">
                <IfAny>
                  <IfAccumulatedFileSize exceeds="500 MB" />
                  <IfAccumulatedFileCount exceeds="3" />
                </IfAny>
              </IfFileName>
        </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>  
</Appenders>

Thanks in advance.

1

There are 1 answers

1
Remko Popma On

Yes, you can use the RoutingAppender to log to multiple log files, even dynamically create log files on the fly. Often people use the context map as the routing rule.

For examples, see the manual page and the Log4j2 FAQ page.