Log4j & Log4j2

Log4j: First you need to download the log4j package from the log4j official site. Place the log4j-*.jarfile from the package in your project and add it to the build path.

InsightOps: Secondly you need to get the latest InsightOps jar library here and add it to your build path. Maven: Alternatively you can use Maven for organizing both Log4j and its InsightOps plugin. For this you can simply replace the two steps above with adding the following dependencies to your project

Maven: Alternatively you can use Maven for organizing both Log4j 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>log4j</groupId>
4
<artifactId>log4j</artifactId>
5
<version>1.2.17</version>
6
</dependency>
7
<dependency>
8
<groupId>com.rapid7</groupId>
9
<artifactId>r7insight_java</artifactId>
10
<version>RELEASE</version>
11
</dependency>
12
</dependencies>

Now, when we compile the project (mvn compile), we’ll see Maven download both the log4j and the logentriesappender.

log4j Configuration

Set the log4j.xml file for your project. For this, download log4j.xml and place it on the classpath. Its contents are shown below

1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3
<log4j:configuration debug="true">
4
<appender name="le" class="com.rapid7.log4j.LogentriesAppender">
5
<param name="Token" value="Your log token here" />
6
<param name="Region" value="Your region here" />
7
<param name="Debug" value="false" />
8
<param name="Ssl" value="true" />
9
<layout class="org.apache.log4j.PatternLayout">
10
<param name="ConversionPattern"
11
value="%d{yyyy-MM-dd HH:mm:ss ZZZ} %-5p (%F:%L) %m" />
12
</layout>
13
</appender>
14
<logger name="example">
15
<level value="debug" />
16
</logger>
17
<root>
18
<priority value="debug"></priority>
19
<appender-ref ref="le" />
20
</root>
21
</log4j:configuration>

You can get a Log token by from the "add data" page in InsightOps. Your region refers to the data centre where your account is - e.g. "us" or "eu"/

If you have already created a log, browse to the log in InsightOps and click the settings icon on the entries card. You will be presented with the settings for the log, where the token will be displayed. Note we also support log4j.properties if you prefer to use this type of configuration file.

log4j Insert Logging Code

This will send log data from a console app

1
import org.apache.log4j.Logger;
2
import org.apache.log4j.LogManager;
3
4
class HelloLogentries {
5
private static Logger log = LogManager.getRootLogger();
6
7
public static void main(String[] args)
8
{
9
log.debug("I'm a debug message");
10
log.info("I'm an info message");
11
12
// Wait here for user input, as logger needs a moment
13
// to spawn its daemon thread and begin sending
14
try{
15
System.in.readline();
16
}catch(IOException e){
17
//Do nothing
18
}
19
}
20
}