Logback

Logback: First you need to download the logback package from the logback official site. Place the logback-*.jar file from the package in your project and add it to the build path.

Maven: You can use Maven for organizing both Logback and its InsightOps plugin. For this you can simply replace the two steps above with adding the following dependencies to your project:

xml
1
<dependencies>
2
<dependency>
3
<groupId>org.slf4j</groupId>
4
<artifactId>slf4j-api</artifactId>
5
<version>1.7.5</version>
6
</dependency>
7
<dependency>
8
<groupId>ch.qos.logback</groupId>
9
<artifactId>logback-classic</artifactId>
10
<version>1.3.8</version>
11
</dependency>
12
<dependency>
13
<groupId>com.rapid7</groupId>
14
<artifactId>r7insight_java</artifactId>
15
<version>RELEASE</version>
16
</dependency>
17
</dependencies>

Now, when we compile the project (mvn compile), we’ll see Maven download both the logback and the InsightOps Java library for us.

Configure Logback

Set the logback.xml file for your project. For this, download logback.xml and place it on the classpath. Its contents are shown below. Note that this file needs to be explicitly called logback.xml

xml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<configuration>
3
<appender name="LE"
4
class="com.rapid7.logback.LogentriesAppender">
5
<Token>Your log token here</Token>
6
<Region>Your region here</Region>
7
<Debug>False</Debug>
8
<Ssl>true</Ssl>
9
<facility>USER</facility>
10
<encoder>
11
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
12
</encoder>
13
</appender>
14
15
<root level="debug">
16
<appender-ref ref="LE" />
17
</root>
18
</configuration>

Replace the "Token" parameter with the token of your log file. You can view this in InsightOps by browsing to the log and clicking the "settings" icon in the entries card. This will open the settings panel where the token will be displayed. Replace the "Region" parameter with the data center that your account is in - e.g. "us" or "eu".

Insert Logging Code

In each class you wish to log from, you have to include the following imports:

1
import org.slf4j.Logger;
2
import org.slf4j.LoggerFactory;

And then create an instance of the Logger class at class-level:

1
private static Logger log = LoggerFactory.getLogger("logentries");
2

Finally add your logging code.

1
log.debug("Debugging Message");
2
log.info("Informational message");
3
log.warn("Warning Message");

Note: if you use an example as simple as the one above, your application might end before any message is sent to InsightOps. For testing purposes and to avoid this issue, you can add a “System.in.read()” statement just after the three lines above.