EAR的Log4j2.xml配置文件位置 [英] Log4j2.xml configuration file location for EAR

查看:186
本文介绍了EAR的Log4j2.xml配置文件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Java EE application packaged with ejbs and war. Following is the structure of the EAR:

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



<我需要使用log4j2,所以我首先尝试通过以下在Web应用程序中初始化Log4j 2的说明但是当我在EJB中创建 Logger 时它会抛出:

I need to use log4j2 so I have tried to configure it at first from the web.xml by following instructions to initialize Log4j 2 in a web application but when I am creating the Logger in an EJB it is throwing:

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

给出的指令这里对我来说不太清楚,但据我所知,我需要将log4j2.xml放在共享位置。

The instruction given here is not much clear to me, but what I understand that I need to place the log4j2.xml in a shared location.

我试图将EML放在EAR中,在EAR / lib内部,在EAR / META-INF内,但我得到了相同的结果。在这些情况下,我没有在web.xml中配置任何内容。

I have tried to place the xml inside the EAR, inside the EAR/lib, inside the EAR/META-INF but I got same result. In these case I haven't configured anything in the web.xml.

如何为EAR配置log4j2,以便配置可用于所有类( ejb-module,web-module的类??

How can I configure log4j2 for an EAR so that the configuration will be available for all the classes (classes for ejb-module, web-module)?

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

I am using Weblogic 12C. Previously I have successfully used log4j2 in Weblogic 11G but in that case the packaging was a WAR file.

推荐答案

你可以打包log4j2如果您愿意,可以在其中一个ejbjar1.jar中创建.xml文件或创建一个新的configonly.jar。然后应该在您的ejb模块和战争中共享它。此外,如果您想将日志与ejb和war分开,您可以配置两个不同的文件追加器和两个不同的记录器,一个用于ejb,另一个用于war。这是一个带有GlassFish v4.1的工作示例log4j2.xml。请注意status =trace以跟踪任何配置问题。

You can package the log4j2.xml file in one of your ejbjar1.jar or create a new configonly.jar if you like. It should then be shared across your ejb modules and war. Also if you want to separate the logs from ejb and war you can configure two different file appenders and two different loggers one for ejb and one for war. Here is a working sample log4j2.xml with GlassFish v4.1 Please note the status="trace" to trace any configuration issue.

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

这篇关于EAR的Log4j2.xml配置文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆