Log4j2.xml EAR 的配置文件位置

Log4j2.xml configuration file location for EAR

我有一个 Java EE 应用程序与 ejbs 和 war 打包在一起。以下是 EAR 的结构:

myapp.ear
-lib
-META-INF
-ejbjar1.jar
-ejbjar2.jar
-mywebapp.war

我需要使用 log4j2,所以我首先尝试按照 instructions to initialize Log4j 2 in a web application 从 web.xml 配置它,但是当我在 EJB 中创建 Logger 时,它抛出:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

here 给出的说明对我来说不是很清楚,但我知道我需要将 log4j2.xml 放在共享位置。

我尝试将 xml 放在 EAR 中,放在 EAR/lib 中,放在 EAR/META-INF 中,但我得到了相同的结果。在这些情况下,我没有在 web.xml.

中配置任何内容

我如何为 EAR 配置 log4j2,以便配置可用于所有 类(类 用于 ejb 模块、web 模块)?

我正在使用 Weblogic 12C。以前我在 Weblogic 11G 中成功使用了 log4j2,但在那种情况下,打包是一个 WAR 文件。

您可以将 log4j2.xml 文件打包到您的 ejbjar1.jar 文件中,或者根据需要创建一个新的 configonly.jar。然后它应该在您的 ejb 模块和 war 之间共享。此外,如果您想将日志与 ejb 和 war 分开,您可以配置两个不同的文件附加程序和两个不同的记录器,一个用于 ejb,一个用于 war。这是 GlassFish v4.1 的工作示例 log4j2.xml 请注意状态="trace" 以跟踪任何配置问题。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd EEE HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
        </Console>

        <!-- for GlassFish v4.1 the logs will be in the domains directory -->
        <RollingFile name="appServerRollingFile" fileName="../app-logs/app-server.log"
            append="true"
            filePattern="../app-logs/$${date:yyyy-MMM}/app-server-%d{yyyy-MMM-dd}-%i.log.zip"
            ignoreExceptions="false">    
            <PatternLayout
                pattern="%d{yyyy-MMM-dd EEE HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
            <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>

        <!-- for GlassFish v4.1 the logs will be in the domains directory -->
        <RollingFile name="appWebRollingFile" fileName="../app-logs/app-web.log"
            append="true"
            filePattern="../app-logs/$${date:yyyy-MMM}/app-web-%d{yyyy-MMM-dd}-%i.log.zip" ignoreExceptions="false">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd EEE HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
            <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="Console" level="TRACE"/>
        </Root>
        <Logger name="test.business" level="TRACE" additivity="false">
            <AppenderRef ref="Console" />
            <AppenderRef ref="appServerRollingFile" />
        </Logger>

        <Logger name="test.web" level="TRACE" additivity="false">
            <AppenderRef ref="Console" />
            <AppenderRef ref="appWebRollingFile" />
        </Logger>
    </Loggers>
</Configuration>

为了保险起见,让我们引用您所指文档中重要的部分:

Java EE Applications

A Java EE application will consist of one or more WAR files and possible some EJBs, typically all packaged in an EAR file. Usually, it is desirable to have a single configuration that applies to all the components in the EAR. The logging classes will generally be placed in a location shared across all the components and the configuration needs to also be shareable. Be sure to follow the instructions to initialize Log4j 2 in a web application.

来自"Using Log4j 2 in Web Applications"

Configuration

Log4j allows the configuration file to be specified in web.xml using the log4jConfiguration context parameter. Log4j will search for configuration files by:

  • If a location is provided it will be searched for as a servlet context resource. For example, if log4jConfiguration contains "logging.xml" then Log4j will look for a file with that name in the root directory of the web application.
  • If no location is defined Log4j will search for a file that starts with "log4j2" in the WEB-INF directory. If more than one file is found, and if a file that starts with "log4j2-name" is present, where name is the name of the web application, then it will be used. Otherwise the first file will be used.
  • The "normal" search sequence using the classpath and file URLs will be used to locate the configuration file.

请注意,当从 EAR 开始时,其中的每个模块通常开始使用它自己的隔离 classloader。

第一次尝试让它工作可能是通过提供 log4j2 作为单个嵌入式 war 组件的一部分。

所以,我不确定您在 assemble 您的 EAR 中使用什么,但最简单的方法是将其放入您的 webModule (war)(每个)的 WEB-INF 中打包在您的 EE 应用程序中 (EAR)。

如果您使用的是 Maven,则您的个人 EJB 和 Web 模块会有单独的项目。因此,您应该能够在以下位置提供 log4j2 文件:

  • 网络模块:src/main/webapp/WEB-INF
  • EJB:src/main/resources(应该让 maven 将它复制到你的 jar 中的 META-INF)。

为了仍然提供 log4j2 文件作为您的 EAR 的一部分(由模块共享),我认为需要在您的 META-INF/MANIFEST.MF 中创建一个 Class-Path 条目。您将提供目录或您的资源 jar 位置作为 Class-Path 的一部分。要提供目录,需要 trailing path separator

我刚才没有试过,但我希望它能给你一个线索,你会在需要的地方纠正我。

此外,Log4j2 还具有 automatically reload on changes 的功能并可动态适应它。为了使它对您有利,我强烈建议您提供 Log4j2 文件作为服务器 class 路径的一部分,而不是深入嵌入到您的 jar、war 或 ear 中。只会更容易查找和修改。