Java Agent

Use the Java Agent to post events to Loggr from a Java desktop app or JSP web app.

Installing
Posting Events
Handling Exceptions

Installing

You can download the source for this agent from GitHub. You will need to have a properly configured Java environment for building this component into your application.

You will need to specify your LOGKEY and APIKEY for the library to use. This can be done in an app.properties file included in your app's project like this:

################################################
## Loggr Application Settings
#  All values are required.
#  LastModified: September 28, 2011 10:35 PM EST
## LastModifiedBy: Justin Kunder
################################################

logKey = <LOGKEY>
apiKey = <APIKEY>
server = post.loggr.net
version = 1
Replace the appropriately labeled values with your LOGKEY and APIKEY for your Loggr log.

top

Posting Events

This agent uses a fluent-style interface for posting events to you Loggr log. Depending how you have your Java project setup, you may need to tell your code to use the Loggr namespace like:

import net.loggr.*;

The only value required for posting events is the Text property.

Events.create().text("Test").post();

Here's an example that shows how to post an event that utilizes all properties of an event. Keep in mind, you can use any combination that works for your app.

Events.create()
    .text("More complex event: " + counter)
    .link("http://loggr.net")
    .tags("tag1 tag2")
    .source(User.Identity.Name)
    .value(35.50)
    .data("<b>user-agent:</b> " + r.UA + <br/><b>on:</b> " + DateTime.Now)
    .dataType(DataType.html)
    .post();

Data Property
The Data property is just a string field, however you can specify a format for how the data should be handled. To specify the format, use the DataType property like:

Loggr.Events.Create()
    .Text("this event includes JSON data")
    .Data("{field1:true, field2:'hello'}")
    .DataType(DataType.json)
    .Post();

The DataType is prefixed to the actual data stored in your log. If you request the data for the previous event from our Web API you would get back:

@json
{field1:true, field2:'hello'}

Internally we parse out the DataType value when we display your event's data. We use it to show a formatted view of your data. The following table shows the currently recognized DataTypes (however, you can prefix your data with any DataType manually for your own purposes).

Value Description Prefix
DataType.plaintext Plain text data, no formatting (default)
DataType.html Text formatted as HTML @html
DataType.json Text formatted as JSON @json


top

Handling Exceptions

Using Loggr to track application exceptions is very helpful. It's even better when using the .NET agent because it automatically formats the exception event for you. All you have to do is trap an exception using a Try/Catch or within a global error handler and then post the exception to Loggr.

To post a .NET exception to Loggr use:

try 
{
    ## some code that generates an exception
	int divideByZero = one / zero;
} 
catch (Exception ex) 
{
    Events.createFromException(ex).Post()
}

If you need to add any information to the event before posting, use the built-in methods:

Events.createFromException(ex)
    .text("This was an error")
    .source("myapp")
    .tags("critical error")
    .post()

top