Using log4j as a shared library in Oracle Application Server 10.1.3.x.

Posted by Steve Racanovic | Posted in , | Posted on 2:48 PM

0

In this example I installed log4j as a shared library to my oc4j instance and then deploy my application from Jdev that utilises this library.

First Download log4j from

http://jakarta.apache.org/log4j/docs/download.html

Deploying log4j as shared library.

1. Unzip the downloaded log4j file.

2. In OAS ASC, select your oc4j instance -> Administration. Under Administration Tasks -> Properties and select Shared Libraries.



3. In Shared Libraries click Create.



4. Enter the library name/version

Shared Library Name = log4j
Shared Library Version = 1.0



5. Select the jar to load into the library.




6. Click finish and see the loaded library.



In Jdev, create a simple web application servlet that uses log4j.



7. My servlet looks like:

package project1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class Log4jDemoInOAS extends HttpServlet {
private static final String CONTENT_TYPE =
"text/html; charset=windows-1252";
Logger log = Logger.getLogger("Log4jDemoInOAS.class");

public void init(ServletConfig config) throws ServletException {
super.init(config);
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-prop-file");
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}

}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Log4j Demo In OAS</title></head>");
out.println("<body>");
out.println("<h2>Using log4j shared libraries in OAS Demo</h2>");
System.out.println("=== Log4jDemo ===");
log.debug("== DEBUG Message ==");
log.info("== INFO Message ==");
log.warn("== WARN Message ==");
log.error("== ERROR Message ==");
log.fatal("== FATAL Message ==");
out.close();
}
}

8. The orion-application.xml must import the library name define in step 4. It looks like:

...
<imported-shared-libraries>
<import-shared-library name="log4j"></import-shared-library>
</imported-shared-libraries>
...

http://download-west.oracle.com/docs/cd/B32110_01/web.1013/b28957/deploysimple.htm#CHDIEFGE

9. Now, before deploying the application to oc4j, I need to uncheck the log4j library that we included in the JDev application during development. This is to ensure that when Jdev packages the application it does not for include the library in the package. From the deployment properties, uncheck this library.



10. Deploy the application and see the opmn container logs for the results.

Comments (0)