Tuesday, April 22, 2014

How to enable debug logs in JSP files in WSO2 Servers

When it comes to debugging a problem in a programming code logs play a huge role. Here I am going to explain how to enable debug logs in JSP files in WSO2 Servers. For logging in WSO2 Servers, it uses Apache Commons Logging library. In order to add debug logs first you need to import the relevant classes to the JSP, where you can import them as follows.
 
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>


Then you need to initialize the Log instance to do logging. As an example here I am going to log ADMIN_SERVICE_COOKIE in WSO2 Servers stored in in the session object.
 
<%
    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    Log log = LogFactory.getLog(this.getClass());
    log.info("Admin Cookie : " + cookie);
%>
Now the JSP page is ready for logging. But still there we need to add log4j properties in order to print logs in the carbon logs. You can do that by adding the following part to ${CARBON_HOME}/repository/conf/log4j.properties file.
 
log4j.logger.org.apache.jsp=INFO

You can define the logging granularity by changing INFO to DEBUG, WARNING, ERROR, FATAL, TRACE according to your requirement.

NOTE : You have to build the relevant bundle every time you change the JSP file/s.