StackHub Home

Log analytics as a service

Sign Up | Log In | Guides

Configuring your Log4J application to use StackHub

This tutorial assumes you already have the StackHub daemon up and running with your application token. If you don't have the daemon installed yet, do that first.

1. Specify an appender

Let's say your existing application prints logging events of level INFO and above to stdout using a simple console appender like this:

# Simple log4j.properties file
log4j.rootCategory=INFO, dest
log4j.appender.dest=org.apache.log4j.ConsoleAppender
log4j.appender.dest.layout=org.apache.log4j.PatternLayout
log4j.appender.dest.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Make sure your application is happily logging to the console before proceeding.

We are going to keep your existing Log4J configuration the way it is, and just add a new SocketAppender to the mix. The SocketAppender will connect to the StackHub daemon when your application starts up.

# A log4j.properties file using a SocketAppender to connect with the StackHub daemon
log4j.rootCategory=INFO, dest, stackhub
log4j.appender.dest=org.apache.log4j.ConsoleAppender
log4j.appender.dest.layout=org.apache.log4j.PatternLayout
log4j.appender.dest.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

log4j.appender.stackhub=org.apache.log4j.net.SocketAppender
log4j.appender.stackhub.RemoteHost=10.0.1.2
log4j.appender.stackhub.Port=8888
log4j.appender.stackhub.LocationInfo=false
log4j.appender.stackhub.ReconnectionDelay=5

You'll need to change RemoteHost to whatever IP address your daemon says it is listening on.

2. Reload Log4J configuration

If you have Log4J set up with configureAndWatch() then your changes will take effect without having to restart your application. Otherwise, go ahead and restart it.

3. Verify that it's working

In your StackHub daemon output, you should see something like this:

INFO  stackhub.LoggingReceiver  - Starting new socket node with remote client at 10.0.1.17

In the example above, both appenders are logging the same things. So if you don't see anything logged in stdout, you won't see anything at StackHub either!

Continue