How to add hostname for my log file in log4j2.xml ? I am working on spring 3.0.5 and need to migrate log4j to log4j2
Asked Answered
I

1

0

I have my log4j2.xml in which I need to add the hostname to the log file for the file Appender.
I have the following log4j2.xml-

    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        </Properties>
        <Appenders>
            <!-- Console Appender -->
            <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
                <PatternLayout pattern="${LOG_PATTERN}"/>
            </Console>

            <!-- File Appenders on need basis -->
            <RollingFile name="FileAppender" fileName="/apps/logs/app-${hostname}.log"
                         filePattern="/apps/logs/app-%server%-%d{yyyy-MM-dd}-%i.log">
                <LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
                <PatternLayout>
                    <Pattern>%d{MM/dd HH:mm:ss,SSS} [%X{jobId}] [%t] %-5p %c{1}.%M:%L %m%n</Pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="10MB" />
                </Policies>
                <DefaultRolloverStrategy max="10"/>
            </RollingFile>
        </Appenders>
    </Configuration>
Impeachable answered 30/12, 2021 at 10:58 Comment(2)
Which OS are you using? Depending on the OS, the hostname might be in different environment variables.Quincuncial
#36171462 - may help youDedededen
I
3

added ${hostName} worked! fileName="/apps/xyz-${hostName}.log"

Impeachable answered 3/1, 2022 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.