Our next-gen architecture is built to help you make sense of your ever-growing data. Watch a 4-min demo video!

Back to All Docs

Log4j Log4j

Last Updated: Feb. 04, 2024

Appenders are nice, and they help take our logging standard to a whole new level.
The following tutorial demonstrates how to integrate Log4j with Coralogix Java SDK.

Requirements

  • Coralogix Send-Your-Data API key
  • Company ID – You can find your company Id from the ‘Settings’ -> ‘Send Your Data’ : company ID will be on the top right.
  • Add the ‘Coralogix log4j2-appender’ to your project dependencies.

As we use Artifactory as our repository you will need to also add a repository to your pom.xml file:

<repositories>
    <repository>
        <id>coralogix</id>
        <name>coralogix repo</name>
        <url>https://cgx.jfrog.io/artifactory/maven</url>
    </repository>
</repositories>

Then, add the dependency to your pom.xml file:

<dependencies>
    <dependency>
        <groupId>com.coralogix.sdk</groupId>
        <artifactId>log4j2-appender</artifactId>
        <version>2.0.11</version>
    </dependency>
</dependencies>

If your Coralogix domain does not end in .com, you will need to add the proper API endpoint:

CORALOGIX_LOG_URL=https://<coralogix-api-endpoint>/api/v1/logs
DomainCoralogix API Endpoint
coralogix.comapi.coralogix.com
coralogix.usapi.coralogix.us
eu2.coralogix.comapi.eu2.coralogix.com
app.coralogix.inapi.app.coralogix.in
coralogixsg.comapi.coralogixsg.com

Usage

Below are Code examples to demonstrate how to add the appender.

LOG4J-2 Appender

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.coralogix.sdk.appenders">
    <Appenders>
        <Coralogix name="Coralogix" companyId="*insert your company ID*" privateKey="*Insert your company private key*" applicationName="*Insert desired Application name*" subSystemName="*Insert desired Subsystem name*">
        </Coralogix>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Coralogix"/>
        </Root>
    </Loggers>
</Configuration>

Above settings will add the Coralogix appended to the root logger, thus all loggers will also send to Coralogix.

Use any logger to send data.

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

public class App 
{
    public static void main( String[] args )
    {
        Logger logger = LogManager.getRootLogger();
        logger.info("Hello from coralogix");
    }
}

To create a specific logger that will send data to Coralogix, use these settings:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.coralogix.sdk.appenders">
    <Appenders>
        <Coralogix name="Coralogix" companyId="*insert your company ID*" privateKey="*Insert your Send-Your-Data API key*" applicationName="*Insert desired Application name*" subSystemName="*Insert desired Subsystem name*">
        </Coralogix>
        <Console name="Console">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
          </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
  	<logger name="coralogixLogger" level="INFO">
  		<AppenderRef ref="Coralogix"/>
  	</logger>
    </Loggers>
</Configuration>

Then simply use the specific logger to send data.

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

public class App 
{
    public static void main( String[] args )
    {
        Logger logger = LogManager.getLogger("coralogixLogger");
        logger.info("Hello from coralogix");
    }
}

LOG4J-1 Appender

For legacy support you can still use the old log4j version.

log4j.appender.CORALOGIX=com.coralogix.sdk.appenders.CoralogixLog4j1Appender 
log4j.appender.CORALOGIX.companyId=<Your Coralogix company ID> 
log4j.appender.CORALOGIX.privateKey=<Your Coralogix Secret> 
log4j.appender.CORALOGIX.applicationName=<Your Coralogix application name>
log4j.appender.CORALOGIX.subsystemName=<Your Coralogix subsystem name> 
log4j.rootLogger=DEBUG, CORALOGIX

LOG4J-2 HTTP Appender

This is useful for cases where some software doesn’t allow installing 3rd party appenders.

<Http name="Coralogix" url="https://<HERE ENDPOING>/logs/v1/singles">
   <Property name="Authorization" value="Bearer ${sys:CORALOGIX_API_KEY}" />
   <Property name="Content-Type" value="application/json" />
   <PatternLayout>
        <pattern>{"json": {"message": "%enc{%m}{JSON}", "timestamp": "%d","severity": "%p", "className": "%C", "methodName": "%M", "threadId": "%t"}, "applicationName": "ApplicationName", "subsystemName": "SubsystemName"}</pattern>
      </PatternLayout>
  </Http>

Support

Need help?

Our world-class customer success team is available 24/7 to walk you through your setup and answer any questions that may come up.

Contact us via our in-app chat or by emailing [email protected].

On this page