Skip to content

Latest commit

 

History

History
 
 

raven-log4j2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Raven-Log4j 2

Log4j 2 support for Raven. It provides an Appender for Log4j 2 to send the logged events to Sentry.

Installation

Maven

<dependency>
    <groupId>net.kencochrane.raven</groupId>
    <artifactId>raven-log4j2</artifactId>
    <version>5.0</version>
</dependency>

Other dependency managers

Details in the central Maven repository.

Manual dependency management

Relies on:

Usage

Configuration

In the log4j2.xml file set:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" packages="org.apache.logging.log4j.core,net.kencochrane.raven.log4j2">
    <appenders>
        <Raven name="Sentry">
            <dsn>
                https://publicKey:secretKey@host:port/1?options
            </dsn>
            <tags>
                tag1:value1,tag2:value2
            </tags>
            <!--
                Optional, allows to select the ravenFactory
            -->
            <!--
            <ravenFactory>
                net.kencochrane.raven.DefaultRavenFactory
            </ravenFactory>
            -->
        </Raven>
    </appenders>

    <loggers>
        <root level="all">
            <appender-ref ref="Sentry"/>
        </root>
    </loggers>
</configuration>

Additional data and information

It's possible to add extra details to events captured by the Log4j 2 module thanks to the marker system which will add a tag log4j2-Marker. Both the MDC and the NDC systems provided by Log4j 2 are usable, allowing to attach extras information to the event.

In practice

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

public class MyClass {
    private static final Logger logger = LogManager.getLogger(MyClass.class);
    private static final Marker MARKER = MarkerManager.getMarker("myMarker");

    void logSimpleMessage() {
        // This adds a simple message to the logs
        logger.info("This is a test");
    }

    void logWithTag() {
        // This adds a message with a tag to the logs named 'log4j2-Marker'
        logger.info(MARKER, "This is a test");
    }

    void logWithExtras() {
        // MDC extras
        ThreadContext.put("extra_key", "extra_value");
        // NDC extras are sent under 'log4j2-NDC'
        ThreadContext.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");
    }
}