简体   繁体   English

在web.xml中使用表达式语言(EL)访问JVM变量

[英]Using Expression Language (EL) in web.xml to access JVM variables

I have two web applications running on the same WebSphere 8.5.5 server. 我有两个在同一WebSphere 8.5.5服务器上运行的Web应用程序。 The first application is able to use expression language (EL) in the web.xml to access JVM variables like so: 第一个应用程序能够使用web.xml中的表达式语言(EL)来访问JVM变量,如下所示:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/config/log4j-${app.env}.xml</param-value>
</context-param>

This allows me to set a JVM variable named app.env on the server that determines which log4j configuration file is used. 这使我可以在服务器上设置一个名为app.env的JVM变量,该变量确定使用哪个log4j配置文件。 This variable is set to rad, test, or production according to the environment. 此变量根据环境设置为rad,test或production。

Furthermore, because these applications will run in a clustered environment but log to the same network share, I need to use EL in the log4j configuration file to differentiate the log files. 此外,由于这些应用程序将在群集环境中运行,但登录到相同的网络共享,因此我需要在log4j配置文件中使用EL来区分日志文件。 This is achieved using EL in the log4j config files like this: 这是通过在log4j配置文件中使用EL来实现的,如下所示:

<appender name="file" class="com.example.util.CustodianDailyRollingFileAppender">
    <param name="maxNumberOfDays" value="10" />
    <param name="compressBackups" value="yes" />
    <param name="File" value="/netshare/logs/${app.serv}_app.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d] %-5p %c - %m%n" />
    </layout>
</appender>

The app.serv JVM variable is set to "1" on the first server in the cluster and "2" on the second, this results in a log file for each server being created named 1_app.log and 2_app.log respectively. 在集群中的第一台服务器上,app.serv JVM变量设置为“ 1”,在第二台服务器上将其设置为“ 2”,这将为每个服务器创建一个分别名为1_app.log和2_app.log的日志文件。

The first application uses Servlets 2.4 and Spring Framework 3.0.4. 第一个应用程序使用Servlets 2.4和Spring Framework 3.0.4。

In the second application I'm trying to use this same scheme for my log files but, I'm using Servlets 3.0 and not using Spring Framework, instead trying to stick to JEE6. 在第二个应用程序中,我尝试对日志文件使用相同的方案,但是,我使用的是Servlets 3.0,而不是使用Spring Framework,而是尝试坚持使用JEE6。 However, the EL is not being evaluated in the web.xml or the log4j configuration files. 但是,不会在web.xml或log4j配置文件中评估EL。

If I put EL in the second application's web.xml then log4j throws an exception because it can't find the configuration file at the location defined because the EL hasn't been evaluated: 如果我将EL放在第二个应用程序的web.xml中,则log4j会引发异常,因为它由于未评估EL而无法在定义的位置找到配置文件:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>/WEB-INF/log4j2-${app.env}.xml</param-value>
</context-param>

This results in an exception because log4j is looking for log4j2-${app.env}.xml instead of log4j2-rad.xml. 这会导致异常,因为log4j正在寻找log4j2-$ {app.env} .xml而不是log4j2-rad.xml。

Similarly, if I hard code the config location and attempt to use EL in the log4j2 configuration the EL does not get evaluated and I end up with log file named ${app.serv}_app.log when it should be evaluating the EL and naming the file 1_app.log. 同样,如果我硬编码配置位置并尝试在log4j2配置中使用EL,则不会评估EL,并且在评估EL和命名时会得到名为$ {app.serv} _app.log的日志文件文件1_app.log。

Which brings up my questions: 这提出了我的问题:

1) What is the difference between Application 1 and Application 2 that is causing EL in my web.xml and log4j configuration to not be evaluated? 1)Application 1和Application 2之间的区别是什么,导致我的web.xml和log4j配置中的EL无法评估?

2) How can I make EL work in my web.xml and log4j configuration files in Application 2? 2)如何在应用程序2的web.xml和log4j配置文件中使EL工作?

I think I've managed to answer my own questions: 我想我已经回答了自己的问题:

1) What is the difference between Application 1 and Application 2 that is causing EL in my web.xml and log4j configuration to not be evaluated? 1)什么是应用1和应用2之差,是造成EL在我的web.xml和log4j的配置不进行评估?

The difference was two-fold: 区别是双重的:

First, Applicaton A was a Spring application making use of the Log4JConfigListener class that comes with Spring. 首先,Applicaton A是一个Spring应用程序,它利用了Spring附带的Log4JConfigListener类。 Log4JConfigListener is aa ServletContextListener that basically just helps the Log4j configuration classes to find the configuration files. Log4JConfigListener是AA的ServletContextListener,基本上只是帮助Log4j的配置类查找配置文件。 Additionally, before it does this, it resolves any placeholders used in the Log4J config location context parameter in the web.xml. 此外,它在此之前,它解决了在web.xml Log4J的配置位置情境的参数中使用的任何占位符。 I was able to reproduce this behavior myself by implementing my own ServletContextListener that did essentially the same thing that Spring does. 我能够通过实现自己的ServletContextListener来重现此行为,该行为基本上与Spring相同。

Second, Application A was using Log4j 1.2.x and Application B was attempting to use Log4j 2.x. 其次,应用程序A使用Log4j 1.2.x,而应用程序B尝试使用Log4j2.x。 Initially, I thought that Log4j 2.x simply didn't do the same placeholder resolution that Log4j 1.2.x did in it's OptionConverter class. 最初,我认为Log4j 2.x在OptionConverter类中根本没有执行与Log4j 1.2.x相同的占位符解析。 So, I solved this problem by reverting to Log4j 1.2.x. 所以,我通过恢复到Log4j的1.2.x.解决了这个问题 Since, I made the change back to Log4j 1.2.x, I've noticed that there appear to be adapter classes in the log4j-1.2-api-2.x.jar. 自从我将更改返回到Log4j 1.2.x之后,我注意到log4j-1.2-api-2.x.jar中似乎有适配器类。 This jar was NOT present on my classpath for Application B and may have made the difference had they been there. 这个jar在我的应用程序B的类路径中不存在,如果在那里,它们可能会有所作为。 I may investigate this at a later date but, for now, I'm happy to have it working using Log4j 1.2.x. 我可能会在以后进行调查,但是到目前为止,我很高兴能够使用Log4j 1.2.x使其工作。

2) How can I make EL work in my web.xml and log4j configuration files in Application 2? 2)如何在应用程序2的web.xml和log4j配置文件中使EL工作?

Question 2 is answered above, with a ServletContextListener that resolves expressions prior to passing the file location to the Log4j configurers and with Log4j 1.2.x instead of Log4j 2.x. 上面回答了问题2,使用ServletContextListener在将文件位置传递到Log4j配置程序之前解析表达式,并使用Log4j 1.2.x而不是Log4j2.x。 Again, I emphasize that Log4j 2.x may have the same behaviors if log4j-1.2-api-2.x.jar is on the classpath but I can't personally verify this at this time. 再次强调,如果log4j-1.2-api-2.x.jar位于类路径中,则Log4j 2.x可能具有相同的行为,但是我目前无法亲自验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM