Skip to end of metadata
Go to start of metadata

WSO2 Application Server provides a platform for hosting and managing web applications. Multitenancy further allows you to maintain your own deployment area inside the server. During the development phase of a web application, it is necessary to see the errors published on the server console in order to troubleshoot problems and errors. 

WSO2 Application Server already provides container level logging facility. It supports most of the commonly used logging API's, such as log4j, common-logging, java-utils-logging, etc. Therefore, if you have inserted log statements to your web application, you will be able to access the logs either on the product's startup console or the log file stored in the <AS_HOME>/repository/logs/ directory. The default log file configured for your product is wso2carbon.log. The log4j configurations for your server are specified in the log4j.properties file stored in the <PRODUCT_HOME>/repository/conf/ directory, which you can customize as required. See the topic on Working with Logs for more information.

However, if it is necessary to manage customized logs for your web application individually, a customized log4j.properties file should be packaged in the .war file of the web application along with the relevant libraries and configurations.

Note that tenant aware logging and app-context aware logging are built using the carbon context. Therefore, to use tenant aware logging and app context aware logging, it is recommended to use the AS container level logging facility without doing any customizing the individual web applications if it is there.

Given below are the steps that you must follow in order to enable customized logging for your web application:

  1. Create a Maven project to build the web application. Note that Apache Maven WAR Plugin is used to build the .war file.

  2. Create a log4j.properties file inside the src/main/resources folder. You can simply copy the log4j.properties file in the <AS_HOME>/repository/conf/ directory and do the necessary customizations. Note that the default resource directory for all Maven projects is src/main/resources.

  3. Add the Maven dependency for log4j to the pom.xml of the web application as shown below. This will package apache-logging-log4j.jar when the .war file is build.


    Be sure that <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> does not exist in the pom.xml of the web application.

  4. Add the following Maven dependency if you are using the commons API:

  5. Update the web application by adding the necessary log lines and specifying the Java classes that need to be imported. See the following example, which uses the Apache Commons API for logging:

    1. Add the following import statements to your web application's Java classes when you need to use JCL SPI (Jakarta Commons Logging Service Provider Interface):

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
    2. For every class in which you want to use Commons logging, you need to declare and initialize a log using the following:

      private Log log = LogFactory.getLog(ClassName.class);
    3. To log a message to a logger use the appropriate method provided by org.apache.commons.logging.Log interface as the log priority.

      log.info(Object message);
      log.debug(Object message);
      log.error(Object message);
      log.warn(Object message);

    Example: See the following simple GET method which generates logs with different priorities:

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    public class CommonsLogGenerator {
    	private Log log = LogFactory.getLog(CommonsLogGenerator.class);
    	public String createLogs() {
    		log.info("INFO LOG");
    		log.debug("DEBUG LOG");
    		log.error("ERROR LOG");
    		log.warn("WARN LOG");
    		return "hello";
  6. Build the .war file using Maven. Check the following:
    • The default resource directory for all Maven projects (src/main/resources) will be copied to the target/classes and the WEB-INF/classes folders in the .war file. 
    • The JARs related to log4j (apache-logging-log4j.jarwill be bundled in the WEB-INF/lib folder.
    • The directory structure is preserved by the build process. 
  7. Start WSO2 AS and deploy the web application's .war file.
  8. Now, when you invoke the web application, the logs will be created according to the configurations you specified in the log4j.properties file.
  • No labels