Hi,

In this article, we will discuss about apllication level logging in Openshift. It is always good to have separate application logs, so that it becomes easier for troubleshooting and monitoring.  As we know there are several ways to do it, but we are going to use log4j right now. You would need to download log4j from apache site :

http://logging.apache.org/log4j/1.2/download.html

Below are the steps to achieve separate application level logging :

Step-1 : We will create a small test war file and call it as “log4jtest.war”

log4jtest.war

- WEB-INF
- classes
- TestLog.java (Developers will kill me for placing this here :P )
- log4j.properties
- /com/mpinfo/test/servlet/TestLog.class (my package structure along with class file)
- lib
- log4j.jar
- web.xml
- META-INF
- jboss-deployment-structure.xml
- index.jsp

Now the content of above files :

# index.jsp :

 This is a test page to demonstarte a test with log4j application level logging.
<h3> <a href="TestLog">Click here to Invoke Logger Servlet</a>

# TestLog.java :

package com.mpinfo.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class TestLog extends HttpServlet
{
	//private Logger logger = Logger.getLogger(this.getClass().getName());

	private Logger logger = Logger.getLogger("test");

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("from log4j=== test log4j log");
		logger.debug("from log4j=== test log4j debug log");
		logger.error("from log4j=== test log4j error log");

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

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
    	out.println("from printwriter=== test printwriter log");

    	try
    	{
    		ArrayList ar = new ArrayList();
    		ar.add(new Integer("1"));
    		ar.add(new String("2"));

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

    	out.close();
	}
}

We are generating error in above program to make sure the logging works.

# log4j.properties :


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

# My Application Log
log4j.appender.test=org.apache.log4j.RollingFileAppender
log4j.appender.test.File=/var/lib/stickshift/871lw2a5e1234f5b90da0754977bc176/log4jtest/logs/MyApp.log
log4j.appender.logfile.Threshold=INFO
log4j.appender.test.MaxBackupIndex=100
log4j.appender.test.MaxFileSize=1Gb
log4j.appender.test.encoding=UTF8
log4j.appender.test.layout=org.apache.log4j.PatternLayout
log4j.appender.test.layout.ConversionPattern=%p %t %c - %m%n

Basically “/var/lib/stickshift/871lw2a5e1234f5b90da0754977bc176/log4jtest/logs/” is the default log directory for this application on Openshift, you can check yours by doing ssh to Openshift. You can refer my previous article : How to connect to Openshift Server from command line ??

# jboss-deployment-structure.xml :

<jboss-deployment-structure>
  <deployment>
    <exclusions>
        <module name="org.apache.log4j" />
    </exclusions>
  </deployment>
</jboss-deployment-structure>

There is a catch here, JBoss AS7 uses “java.util.logging” package, but the container allows to use other logging mechanism, but the package need to be mentioed inside “jboss-deployment-structure.xml”.

Step-2 :

Assuming that you already have a domain on Openshift, we will proceed with creating an app. Navigate to your project directory and run below command :

$ rhc app create -a log4jtest -t jbossas-7

Step-3 : Step 2 will create a “deployments” directory, now copy your war file to here :

.
$ cp log4jtest.war /log4jtest/deployments/
.

Step-4 : As it is an exploded war we need to create a blank file as “log4jtest.war.dodeploy”

$ touch log4jtest.war.dodeploy

Step-5 : Remove pom.xml file, as we are not performing any build :

$ cd /log4jtest/

$ git rm -f pom.xml

Step-6 : Now lets sync our local repository to Openshift repository :

$ rsync -avz /log4jtest/deployments/ @log4jtest-.rhcloud.com:~/log4jtest/repo/deployments

for example :

$ rsync -avz /OpenShift/log4jtest/deployments/ 871lw2a5e1234f5b90da0754977bc176@log4jtest-testdomain.rhcloud.com:~/log4jtest/repo/deployments
Enter passphrase for key '/root/.ssh/libra_id_rsa':
sending incremental file list
./
.gitkeep
log4jtest.war.dodeply
log4jtest.war/
log4jtest.war/index.jsp
log4jtest.war/index.jsp~
log4jtest.war/META-INF/
log4jtest.war/META-INF/jboss-deployment-structure.xml
log4jtest.war/META-INF/jboss-deployment-structure.xml~
log4jtest.war/WEB-INF/
log4jtest.war/WEB-INF/jboss-web.xml.txt
log4jtest.war/WEB-INF/jboss-web.xml~
log4jtest.war/WEB-INF/web.xml
log4jtest.war/WEB-INF/web.xml~
log4jtest.war/WEB-INF/classes/
log4jtest.war/WEB-INF/classes/Log4jInit.java~
log4jtest.war/WEB-INF/classes/TestLog.java
log4jtest.war/WEB-INF/classes/TestLog.java~
log4jtest.war/WEB-INF/classes/log4j.properties
log4jtest.war/WEB-INF/classes/log4j.properties~
log4jtest.war/WEB-INF/classes/mylog4j.properties~
log4jtest.war/WEB-INF/classes/com/
log4jtest.war/WEB-INF/classes/com/mpinfo/
log4jtest.war/WEB-INF/classes/com/mpinfo/test/
log4jtest.war/WEB-INF/classes/com/mpinfo/test/servlet/
log4jtest.war/WEB-INF/classes/com/mpinfo/test/servlet/TestLog.class
log4jtest.war/WEB-INF/lib/
log4jtest.war/WEB-INF/lib/log4j-1.2.15.jar

sent 364677 bytes  received 393 bytes  22125.45 bytes/sec
total size is 405159  speedup is 1.11

Step-7 : Hit the url from browser :

http://log4jtest-.rhcloud.com/log4jtest/

Step-8 : tail the application logs from Openshift server (need to do ssh to Openshift server)

[log4jtest-testdomain.rhcloud.com deployments]\> tail -50f /var/lib/stickshift/871lw2a5e1234f5b90da0754977bc176/log4jtest/logs/MyApp.log
INFO http-127.6.205.129-127.6.205.129-8080-1 test - from log4j=== test log4j log
ERROR http-127.6.205.129-127.6.205.129-8080-1 test - from log4j=== test log4j error log
ERROR http-127.6.205.129-127.6.205.129-8080-1 test - from log4j=== error===Index: 2, Size: 2

And we are done :) , stay high on OpenShift !!

Thanks,
MiddlewareMagic Team