Real time Java server logs in your web browser

In this tutorial I’ll show you how you can use free LogDigger tools to get request-scoped log messages from your Java based web application right into your browser. (In this article, we will talk about the LogDigger add-on for Firefox, although there’s a LogDigger plugin for Internet Explorer too.)

How does it work?

Using LogDigger Server you can collect and save for later examination all events logged during the application usage session.

We’re going to use the LogDigger Connector for Java and accompanying add-on for Firefox. Here’s a somewhat simplified explanation of how it works:

  1. When Firefox sends an HTTP request to your server, the add-on inserts an additional header that activates LogDigger (LD) servlet filter.
  2. The LD servlet filter collects log messages related to the request, and sends a “ticket” as a part of the response header. The browser extension can use this to pick up the logs.
  3. The Firefox add-on uses the received ticket to send an additional HTTP request to the server, this time requesting the log messages.
  4. The LD servlet filter fulfills this second request, sending logs back to the browser (so this second request doesn’t reach your application).

Let’s start…

Installing the LogDigger Add-on for Firefox

LogDigger’s log console appears as a tab in Firebug, so go and get Firebug if you don’t have it already. Next, get the LogDigger Add-on for Firefox. You need to allow Firefox to retrieve the extension from logdigger.com.

Allow Firefox to install add-on from logdigger.com

After you restart the Firefox, open http://logdigger-testlab.appspot.com/. That’s our very simple Grails application with the LogDigger Connector, installed on the Google App Engine infrastructure. The Test Lab form allows you to generate Log4j events on the server, and we’re going to use it here to test our real-time server log monitoring.

LogDigger will not request logs from the server unless it’s enabled, so:

  • Use the LogDigger button to enable LogDigger for the current web site, and
  • Enable LogDigger console in Firebug.

Test lab

Now, in the Test Lab page, click on Submit button. You will see something like this:

Error log (server default)

You can notice that only messages with the ERROR status are shown, which is in accordance with default server logging settings. Now, select the DEBUG logging level and submit the form again. This time you will get a long list of log messages that also includes framework logs.

Log messages with threshold set to DEBUG

What happens on the server is that the LogDigger Connector temporary changes the logging level with Log4j, while the request is being handled.

Now it’s time to add the LogDigger Connector to your application.

Installing the LogDigger Connector for Java

We assume here that your application uses Log4j for logging (although you can use another logging API with an adapter for Log4j). The LogDigger Connector requires Log4j 1.2.15, so you may need to upgrade to that version.

Next, download the LogDigger Connector for Java distribution, and copy all JAR files to your WEB-INF/lib directory. Finally, add RequestLogCollectorFilter to your web.xml:

<!-- Define LogDigger filter -->
<filter>
    <filter-name>LogDiggerServletFilter</filter-name>
    <filter-class>com.logdigger.connector.servlet.filter.RequestLogCollectorFilter</filter-class>
</filter>

<!-- Define mapping for the LogDigger filter -->
<filter-mapping>
    <filter-name>LogDiggerServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

That’s all! Start your application now, direct Firefox to it, enable LogDigger and see how it works for you.

Controlling access to logs

We can’t stress enough how important it is that you put restrictions on who can pull your server logs, especially when going live with your application.

The LogDigger servlet filters allows you to control access through a “request authorizer” mechanism. You can create your own implementation, or use the provided SimpleAuthorizer that can filter requests depending on the presence of a session attribute, server name, remote IP address or host name (as Java regexp expressions). For example:

<filter>
    <filter-name>LogDiggerServletFilter</filter-name>
    <filter-class>com.logdigger.connector.servlet.filter.RequestLogCollectorFilter.</filter-class>
    <init-param>
        <param-name>authorizer</param-name>
        <param-value>com.logdigger.connector.impl.SimpleAuthorizer</param-value>
    </init-param>
    <init-param>
        <param-name>simpleAuthorizer.hasSessionAttribute</param-name>
        <param-value>USER_DATA</param-value>
    </init-param>
    <init-param>
        <param-name>simpleAuthorizer.serverName</param-name>
        <param-value>localhost</param-value>
    </init-param>
    <init-param>
        <param-name>simpleAuthorizer.remoteHost</param-name>
        <param-value>hostname</param-value>
    </init-param>
    <init-param>
        <param-name>simpleAuthorizer.remoteIP</param-name>
        <param-value>192.168.1.[1-9]*</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>LogDiggerServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

For additional details please check the documentation or feel free to start a discussion in our support forums (listed bellow).

Finally, it’s worth mentioning that the LogDigger Connector for Java is available under the Apache 2 license, so you’re free to redistribute it with your application. The add-on for Firefox is also free to use.

Happy log-digging!



Further information:

About

We are a micro-ISV, founded around a common passion – to improve the process of issue reporting, so that even occasional testers (such as customers and managers) can easily provide developers with the sort of input that we, personally, always wanted to have. We wish to eliminate boring manual tasks from the process, so that developers and testers can enjoy what they do a lot more.

Copyright © 2010. LogDigger. All Rights Reserved.