Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing environment variable from log4j2.xml

I am developing a Spring Boot web application. I am trying to access an environment variable from Websphere server in my log4j2.xml configuration. But somehow it's not working.

Environment variable is set in WAS under the below path -

Application servers > server001 > Process definition > Java Virtual Machine > Custom properties

environment variable image

My log4j2.xml configuration is as follows -

<Appenders>
 
        <RollingFile name="RollingFile"
            fileName="$${env:environment}/apps/was/logs/app-logs.log"
            filePattern="$${env:environment}/apps/was/logs/$${date:yyyy-MM}/app-logs-%d{-dd-MMMM-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy
                    size="10 MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
</Appenders>

I have tried to get the environment variable using $${env:environment} / ${env:environment}/ ${environment}. Nothing is working working. It's not able to fetch the environment variable. So it's crating a folder named "${env:environment}" / ${environment} instead of "DVL".

From java I can able to access my environment variable without any issue -

System.getProperty("environment") ==> DVL

Please Help.

like image 401
Subhadip Avatar asked Jun 17 '26 14:06

Subhadip


1 Answers

I have resolved this issue by using ${sys:environment} instead of ${env:environment}. For more details please go through the documentation logging.apache.org/log4j/2.x/manual/lookups.html

like image 63
Subhadip Avatar answered Jun 20 '26 13:06

Subhadip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!