Java – Log4j2.xml configuration file location for EAR

earjakarta-eejavalog4jlog4j2

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

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.

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.

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.

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

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

Best Answer

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>