Wednesday, January 25, 2012

Configuring Web Logic 10.3.5 and log4j for your Java Application

I recently spent several days trying to track this down why Apache log4j for my Java application was working when deployed to Oracle WebLogic Server 10.3.5. I am posting my findings here in the hopes of saving others some time. I found several blogs that talked about this, but the end result was that log4j was still not working correctly. There was always one step missing. A co-worker and I finally figured it out for both Linux and Windows.

1. Copy wllog4j.jar from your WebLogic server/lib directory and place it in your domain_root/lib folder.
2. Copy your version of log4j.jar to your domain_root/lib folder.
3. Copy your log4j.xml to your domain_root folder.
4. Log in to the your WebLogic admin server console. Click on Servers -> Admin Server -> Logging. Click on advanced mode, and change the logging implementation from JDK to Log4J. Save your changes.

Most of the blogs had these items, but the one critical piece that was missing:
5. (Linux)Edit the setDomainEnv.sh file in the domain_root/bin directory. and find the following code:

if [ "${LOG4J_CONFIG_FILE}" != "" ] ; then
JAVA_PROPERTIES="${JAVA_PROPERTIES} -Dlog4j.configuration=file:${LOG4J_CONFIG_FILE}"
export JAVA_PROPERTIES
fi

Insert the following above those lines of code(replacing the path with the one for your system's domain_root directory:

LOG4J_CONFIG_FILE=”/u01/app/oracle/middleware/user_projects/domains/base_domain/log4j.xml”
Export LOG4J_CONFIG_FILE

5. (Windows)
Edit the setDomainEnv.cmd file in the domain_root/bin directory. and find the following code:

if NOT "%LOG4J_CONFIG_FILE%"=="" (
set JAVA_PROPERTIES=%JAVA_PROPERTIES% -Dlog4j.configuration=file:%LOG4J_CONFIG_FILE%
)

Insert the following above those lines of code(replacing the path with the one for your system's domain_root directory:

set LOG4J_CONFIG_FILE=”C:\Oracle\Middleware\user_projects\domains\base_domain\log4j.xml”


6. Activate the changes and restart the admin server.

Here is a sample log4j.xml file. Obviously, you will need to update it for a valid location for your file appender, and replace yourcompany and yourproject with valid values.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!--====================================================================-->
<!-- Appenders -->
<!-- %d{dd-MMM-yy HH:mm:ss} %-5p [%c{1}] %m%n -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] - %m (%F:%M:%L)%n"/>
<!--param name="ConversionPattern" value="%d{ISO8601} %p [%c{1}] - %m (%F:%M:%L)%n"/-->
</layout>
</appender>

<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/temp/logs/yourproject.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %t %-5p %c{2} - %m%n"/>
</layout>
</appender>

<!--====================================================================-->
<!-- Logging Levels -->

<category name="com.yourcompany.yourproject">
<priority value="DEBUG"/>
</category>

<category name="org.apache">
<priority value="WARN"/>
</category>

<category name="org.springframework">
<priority value="WARN"/>
</category>

<!--===================================================================-->
<!-- Appender Assignments -->
<root>
<priority value="ERROR"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>


Again, I hope that this will save someone the pain I went through to track this down.

4 comments:

techworldrocks said...

Thank you for this post. This configuration worked.

Kaushal Pandya said...

Thanks really working for me .

Mateusz Iwan said...

Hello

That's very good tutorial. But i have a quesion- in my organization we have 3 separate applications deployed to weblogic 10.3.5.0
and we need 3 different log files, one for each application. How can i make it, if in log4j.xml i have only one parameter with path?

Thanks
Mat

mynam said...

Thank you very much for the post. I have been looking for this everywhere.. Good karma for you my man.