Documentation‎ > ‎

Application Instrumentation

There are different ways to setup the bug4j agent within your application.
If your application already uses Apache log4j then the simplest method is probably to add a bug4j appender.
The alternative is to call the bug4j agent.
In both cases you will have to include the bug4j client and its dependencies in your application's classpath.
The bug4j client is located in .../client/bug4j.jar. The dependencies are in the .../client/lib directory.

Using Log4J

Edit your log4j.xml and add the bug4j appender:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
    <appender name="bug4j" class="org.bug4j.client.Bug4jAppender">
        <param name="serverUrl" value="http://<>:8063/"/>
        <param name="applicationName" value="My Application"/>
        <param name="applicationVersion" value="1.3"/>
                <param name="proxy" value=""/>

        <priority value="info"/>
        <appender-ref ref="console"/>
        <appender-ref ref="bug4j"/>

If you store the configuration in then you will need this instead:
# Set root logger level to DEBUG and its only appender to A1.
.rootLogger=INFO, A1, BUG4J

# A1 is set to be a ConsoleAppender.

# A1 uses PatternLayout.
.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%

.appender.BUG4J.applicationName=My Application
Don't forget to replace <> with the name of the host where you have installed the bug4j server.
Now every call to Logger.error() in addition to report the error in the log file will also report an error to the bug4j server.

Invoking the bug4j agent
If you do not use log4j then you must instrument your application by invoking the agent explicitly.
You first have to setup the agent: Just include a in your classpath.
server.url=http://<>:8063/ Application
Don't forget to replace <> with the name of the host where you have installed the bug4j server.

To invoke the bug4j agent, call 
import org.bug4j.client.*;
    } catch (Exeption e) {"Failed to do something", e);
The key is to identify the few places where exceptions can be intercepted.
You can also install an default exception handler:
import org.bug4j.client.*;
        final Bug4jUncaughtExceptionHandler handler = new Bug4jUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(handler );