Cloud Zone is brought to you in partnership with:

I concentrate on designing and building enterprise software in Java, .NET, and related technologies. I am a strong advocate of the Feature Driven Development (FDD) process, Modeling in Color, and Peer Code Review. I have been developing software since 1995 in Java, C#, Visual Basic, ASP, PHP, and C. My primary focus has been designing and developing web-based applications, although I have worked on my share of client applications, web services, and PL/SQL. Eric is a DZone MVB and is not an employee of DZone and has posted 4 posts at DZone. You can read more from them at their website. View Full User Profile

Application Level Logging in OpenShift

  • submit to reddit

I read a good article tonight about implementing application level logging using log4J, and I thought this concept could be extended and abstracted one level further by making use of the Apache Commons Logging API. Using the Apache Commons Logging API allows the logging implementation to be abstracted away from the application; resulting in application logging code that can be reused even if the logging implementation changes. There has been some criticism of the Commons Logging API for many years, and I will address that by saying the same principle can be applied with a different logging façade like SLF4J.

Enough talking, let’s get started…

Step #1:

First, I created a JBoss AS7 application in OpenShift called “commonslogging”.

Then I opened a command prompt (or terminal),and cloned the Git repository onto my own machine using the Git command specified by OpenShift when creating a project.

Step #2:

Next, I imported the project as a Maven Project in Eclipse using the local Git repository.


Figure 1: Import Project into Eclipse


Figure 2: Create new Maven Project in Eclipse

Step #3:

Next, you will need to download the Commons Logging API and log4J implementation if you do not already have them. You can find them here:

Next, I added the Commons Logging and log4j dependancies to the Maven pom.xml file.


Step #4:

Next, expand the /src/main/webapp folder and create a new “index.jsp” file.

Replace the content with the following:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>OpenShift Apache Commons Logging Test</title>
    This is a test page to demonstrate a test with Apache Commons
    Logging and log4j application level logging.
        <a href="Log4jTest">Click here to Invoke Logger</a>

You can delete the “index.html” file that was created by the OpenShift application wizard.

Step #5:

Create a new Java class, “”, in the /src/main/java folder.

Replace the default content with the following code:

package org.ericsilva.openshift.commonslogging;

import java.util.ArrayList;
import java.util.List;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 * Tests the log4j implemenation using Apache Commons Logging API.
 * @author Eric Silva (ES)
public class Log4jTest extends HttpServlet {

     * Serialization UID.
    private static final long serialVersionUID = 1L;

     * Logger instance
    private Log log = LogFactory.getLog("test");

     * {@inheritDoc}
    protected void doGet(
                    HttpServletRequest request,
                    HttpServletResponse response) throws ServletException,
                    IOException {"from log4j=== test log4j log");
        log.debug("from log4j=== test log4j debug log");
        log.error("from log4j=== test log4j error log");

            .println("from system.out.println==== test system.out.println log");
            .println("from system.err.println==== test system.error.println log");

        PrintWriter out = response.getWriter();
        out.println("from printwriter=== test printwriter log");

        try {
            List<String> ar = new ArrayList<String>();

  "from log4j=== get arraylist index=2" + ar.get(2));
        } catch (IndexOutOfBoundsException e) {
            log.error("from log4j=== error===" + e.getMessage(), e);
            System.err.println("from system.err.println=== error==="
                + e.getMessage());


Step #6:

Open the “web.xml” file in the /src/main/webapp/WEB-INF folder, and add the following servlet mapping:


You will need to change the package name of the file to the package name you used when creating the file.

Step #7:

Create a new file, ””, in the /src/main/resources folder.

Paste the following contents into the file:


log4j.rootLogger=INFO, test
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{MM/dd HH:mm:ss.SSS}][%p]%c{1}:%L - %m%n

# My Application Log
log4j.appender.test.layout.ConversionPattern=<%d> <%t> <%p> <%F:%L> - %m%n

Step #8:

The final thing we need to do is create a “META-INF” folder in the /src/main/webapp folder and add a “jboss-deployment-structure.xml” descriptor file to tell it to use the Commons Logging API instead of the default “java.util.logging” implementation.

After you create the META-INF folder and XML descriptor, add the following contents to the XML file:

        <module name="org.apache.commons.logging" />

Step #9:

Next commit your changes to your local Git repository, and then push the changes back to OpenShift. OpenShift will automatically try to deploy your new code.

Step #10:

Next, open you application’s URL in your favorite browser, e.g.

Click the link on the page. This will generate an error that will appear in your application log file.

For information on how to connect via SSH to your application for command line access I suggest reading this article.

Happy Coding. I really want to do more with OpenShift.









Published at DZone with permission of Eric Silva, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Kevin Ongoro replied on Fri, 2014/09/19 - 12:04pm

Silva good read. To ensure settings is permanent, one can add something to .openshift/action_hooks/deploy* and this can apply this setting when the application is deployed.

Neumann batting gloves 

Mike Mikello replied on Sat, 2014/10/25 - 11:49am

 The code shown in this post is complicated for me. I think that i can't be a coder, but i like Your post and custom embroidered patches:

Jessica Barden replied on Wed, 2014/11/19 - 5:31am

Application level login issues can now be solved with the essay help UK . I am just amazed by their solving procedure in some steps

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.