log4j support for Raven.
It provides an Appender
for log4j to send the logged events to Sentry.
<dependency>
<groupId>com.getsentry.raven</groupId>
<artifactId>raven-log4j</artifactId>
<version>7.4.0</version>
</dependency>Details in the central Maven repository.
Relies on:
- raven dependencies
- log4j-1.2.17.jar
- slf4j-log4j12-1.7.7.jar is recommended as the implementation of slf4j (instead of slf4j-jdk14).
In the log4j.properties file set:
log4j.rootLogger=WARN, SentryAppender
log4j.appender.SentryAppender=com.getsentry.raven.log4j.SentryAppender
log4j.appender.SentryAppender.dsn=https://publicKey:secretKey@host:port/1?options
// Optional, provide tags
log4j.appender.SentryAppender.tags=tag1:value1,tag2:value2
// Optional, provide release version of your application
log4j.appender.SentryAppender.release=1.0.0
// Optional, override the server name (rather than looking it up dynamically)
log4j.appender.SentryAppender.serverName=server1
# Optional, select the ravenFactory class
#log4j.appender.SentryAppender.ravenFactory=com.getsentry.raven.DefaultRavenFactoryAlternatively in the log4j.xml file set:
<appender name="sentry" class="com.getsentry.raven.log4j.SentryAppender">
<param name="dsn" value="https://publicKey:secretKey@host:port/1"/>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="WARN"/>
</filter>
</appender>
You'll also need to associate the sentry appender with your root logger, like so:
<root>
<priority value="info" />
<appender-ref ref="my-other-appender" />
<appender-ref ref="sentry" />
</root>
It's possible to add extra data to events, thanks to both the MDC and the NDC systems provided by Log4j.
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
void logSimpleMessage() {
// This adds a simple message to the logs
logger.info("This is a test");
}
void logWithExtras() {
// MDC extras
MDC.put("extra_key", "extra_value");
// NDC extras are sent under 'log4J-NDC'
NDC.push("Extra_details");
// This adds a message with extras to the logs
logger.info("This is a test");
}
void logException() {
try {
unsafeMethod();
} catch (Exception e) {
// This adds an exception to the logs
logger.error("Exception caught", e);
}
}
void unsafeMethod() {
throw new UnsupportedOperationException("You shouldn't call that");
}
}By default all MDC parameters are sent under the Additional Data Tab. By specify the extraTags parameter in your configuration file. You can specify MDC keys to send as tags instead of including them in Additional Data. This allows them to be filtered within Sentry.
log4j.appender.SentryAppender.extraTags=User,OS void logWithExtras() {
// MDC extras
MDC.put("User", "test user");
MDC.put("OS", "Linux");
// This adds a message with extras and MDC keys declared in extraTags as tags to Sentry
logger.info("This is a test");
}It is not recommended to attempt to set up SentryAppender within an
AsyncAppender.
While this is a common solution to avoid blocking the current thread until the
event is sent to Sentry, it is recommended to rely instead on the asynchronous
connection provided by Raven.