Sunday, April 11, 2021

Viewing Tomcat logs on Web Browser

While doing the development, many times developer requires to check the logs of tomcat as well as application logs in Development/QA environments. 
Hence, to check the logs the developer needs to connect to the remote environment to check the log files physically. 

Wouldnot it be nicer if we can view the logs from in browser itself.

Tomcat provides a nice way to handle this. You can view the logs from browser itself. Below are the steps.

Steps:
1. Download Tomcat 9.x & extract the zip.
2. Move to <TOMCAT_INSTALL_DIR>/conf/Catalina/localhost
3. Create a file name logs.xml
4. Add the below line & save the logs.xml file. 
<Context override="true" docBase="${catalina.base}/logs" path="/logs" />
5. Additionally, navigate to web.xml under <TOMCAT_INSTALL_DIR>/conf
6. Change the value of  "listings" parameter to true declared under DefaultServlet as below:

<servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

7. This enables to view the files under the logs folder.
8. Now Start the tomcat
9. Type in browser  http://localhost:8080/logs/

You will see a output like below , providing the logs under  <TOMCAT_INSTALL_DIR>/logs folder



Some times, the logger logs are generated outside of Tomcat logs folder. 
Suppose, your application logs are generated in under D:/logs/<APP_NAME> folder.

In that case, you can create another file in named <APP_NAME>.xml under <TOMCAT_INSTALL_DIR>/conf/Catalina/localhost

In <APP_NAME>.xml you can put the below content

<Context override="true" docBase="D:/logs/<APP_NAME>" path="/<APP_NAME>" />

Now navigating to http://localhost:8080/<APP_NAME>/ will show you the logs generated under D:/logs/<APP_NAME> folder.


Convert Java Project from Log4j 1 to Log4j2

Many times while working on old Java projects we find Log4j 1.x is used. But as the Log4j2 is the new one; hence to upgrade to Log4j2 we nee...