Saturday, June 6, 2015

How to create web app specific custom log file in WSO2 carbon platform



Use case

Need to have a custom log file to log the web applications deployed into wso2 Application Server 5.2.1.


Prerequisites

Steps

You can have the basic idea about log4j 1.2 by going through following link.

You can create the log file from the code like this.



As an alternative, you can prepare your own log4j.properties file for the web application and configure it to load the configurations from the file. I will be explaining that approach.



I am going to modify the web application which can be found in AS_HOME/samples/Jaxws-Jaxrs/jaxrs_basic.

I did following modifications to the web app
  1. Modified the the pom.xml file to add the commons-logging and log4j dependencies. Changed the scope to provided for the dependencies of cxf, ws.rs and commons client.
  2. Removed the "packagingExcludes" option to make sure that the jars related to log4j is packed in the WEB-INF/lib folder. We are packing this in to lib folder to make sure, that the defined log4j.properties files is loaded again for the web application. If not the logging will use the log4j.properties defined in carbon environment.
  3. Updated the "maven-antrun-plugin" to copy the "log4j.properties" files in to the target/classes in web app war file.
You can view the pom file over here.


Once you build this maven file, you can deploy it into Application Server and check for the log file in the custom path that you defined in log4j.properties file.

You can check for logs by hitting the endpoint with the following url


The source code can be found here for this web app.







No comments:

Post a Comment