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 Integrations

Log4j Log4j

Last Updated: Aug. 16, 2022

Appenders are nice, and they help take our logging standard to a whole new level.
In this section we will explain how you can integrate log4j with Coralogix Java SDK.

Requirements:

  • A Private Key – You can find your key in your Coralogix account under ‘Data Flow’ -> ‘API Keys’ -> ‘Send your logs’.
  • 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://jfrog.coralogix.com/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.10</version>
    </dependency>
</dependencies>

If your Account is not in the EU region (the account url does not have a .com suffix)

you will need to add this environment variable (where the code will run):

CORALOGIX_LOG_URL=https://<coralogix-api-url>/api/v1/logs
Account URLCoralogix API URL
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 this 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 company private 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 simple 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

Need help? We love to assist our customers, simply reach out via our in-app chat, and we will walk you through, step by step.

On this page