從 log4j2.xml 訪問環境變量 (Accessing environment variable from log4j2.xml)


問題描述

從 log4j2.xml 訪問環境變量 (Accessing environment variable from log4j2.xml)

我正在開發一個 Spring Boot Web 應用程序。我正在嘗試從我的 log4j2.xml 配置中的 Websphere 服務器訪問環境變量。但不知何故它不起作用。

環境變量在WAS中設置在以下路徑下‑

應用程序服務器> server001>進程定義> Java虛擬機>自定義屬性

環境變量圖片

我的log4j2.xml配置如下如下 ‑

<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>

我嘗試使用 $${env:environment} / ${env:environment}/ ${environment} 獲取環境變量。什麼都沒有工作。它無法獲取環境變量。所以它創建了一個名為“${env:environment}”/${environment} 而不是“DVL”的文件夾。

從 java 我可以毫無問題地訪問我的環境變量 ‑

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

請幫忙


參考解法

方法 1:

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

方法 2:

We had a same issue and we implemented a workaround.

We read the environment value using java and store into MDC (Using slf4j MDC)

And MDC key is accessible in log4j2.xml

Example:

MDC.put("environment", System.getProperty("environment"));

Now, you can access environemnt by simply writing {environment}

Hope this will help you.

(by SubhadipSubhadipYogesh Prajapati)

參考文件

  1. Accessing environment variable from log4j2.xml (CC BY‑SA 2.5/3.0/4.0)

#log4j2 #websphere #web-applications #spring-boot #was






相關問題

Làm cách nào để thêm phần phụ Log4J2 vào thời gian chạy theo chương trình? (How to add Log4J2 appenders at runtime programmatically?)

使用 Spring 和 Log4j2 的 Classloader-Leak (Classloader-Leak with Spring and Log4j2)

log4j2 RolllingFileAppender filePattern 創建日期錯誤 (log4j2 RolllingFileAppender filePattern create a wrong date)

將時間戳添加到日誌文件名時出錯 (log4j2) (Error adding timestamp to log file name (log4j2))

log4j2 - 以編程方式更改 RollingFileAppender 的最大文件大小 (log4j2 - programmatically change max file size of RollingFileAppender)

log4j 沒有登錄到文件或控制台 (log4j is not logging into file or console)

Log4j2 未在 Linux 中使用 Spring Boot 創建日誌文件 (Log4j2 not creating log file with Spring Boot in Linux)

application.properties 中的外部 log4j2.xml 文件路徑 (External log4j2.xml file path in application.properties)

使用 Spring 的依賴於配置文件的 log4j2 配置 (Profile-dependent log4j2 configuration with Spring)

從 log4j2.xml 訪問環境變量 (Accessing environment variable from log4j2.xml)

Spring Boot 應用程序中的內部 tomcat 停止響應調用 (internal tomcat inside spring boot application stops responding to calls)

如何在 mule 應用程序中實現 log4j2 (2.14.0) 'JSON 模板佈局'? (How can I implement log4j2 (2.14.0) 'JSON Template Layout' in a mule application?)







留言討論