How can log4j2 be configured to set the logging output …

To configure the logging output path of log4j2, you need to edit the log4j2.xml configuration file and set the logging output path to the desired location.

Here is an example of a log4j2.xml configuration file:

<Configuration status="warn" name="MyApp" packages="">
    <Properties>
        <Property name="logPath">logs</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="File" fileName="${logPath}/myapp.log"
                     filePattern="${logPath}/myapp-%d{MM-dd-yyyy}.log.gz">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

In this example, the log output path is set to the “logs” directory. You can modify the file name and output path as needed.

Please make sure to place the log4j2.xml file in the classpath of your project and ensure that you correctly reference the log4j2 library and configuration file in your code.

bannerAds