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.
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 using customized log4j.properties
files for web applications.
Given below are the steps that you must follow in order to enable customized logging for your web application:
Create a Maven project to build the web application. Note that Apache Maven WAR Plugin is used to build the .war file.
Create a
log4j.properties
file inside thesrc/main/resources
folder. You can simply copy thelog4j.properties
file in the<AS_HOME>/repository/conf/
directory and do the necessary customizations. Note that the default resource directory for all Maven projects issrc/main/resources.
Add the Maven dependency for
log4j
to thepom.xml
of the web application as follows:<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Ensure that
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
is removed from thepom.xml
of the web application,Add the following Maven dependency if you are using the commons API from the Carbon runtime:
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency>
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:
Add following import statements to your webapp 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;
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);
To log a message to a logger use 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; @Path("/") public class CommonsLogGenerator { private Log log = LogFactory.getLog(CommonsLogGenerator.class); @GET @Path("/logging") public String createLogs() { log.info("INFO LOG"); log.debug("DEBUG LOG"); log.error("ERROR LOG"); log.warn("WARN LOG"); return "hello"; } }
- Build the WAR file using Maven. Check the following:
- The default resource directory for all Maven projects (
src/main/resources)
will be copied to thetarget/classes
and theWEB-INF/classes
folders in the WAR file. - The JARs related to
log4j
(apache-logging-log4j.jar
) will be bundled in theWEB-INF/lib
folder. - The directory structure is preserved by the build process.
- The default resource directory for all Maven projects (
- Start WSO2 AS and deploy the web application WAR file.
- Now, when you invoke the web application, the logs will be created according to the configurations in the
log4j.properties
file.