We're launching a new cybersecurity venture! Learn more about Snowbit >

Log4j Log4j

Last Updated: Mar. 30, 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:

You need to have the Coralogix Java SDK integrated into your project.
Read all about it, Here.


as we use Artifactory as our repository you will need to also add a repository to the build.

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

You need to add the log4j appender to your projects dependencies (This example is for version 2.0.8).

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

Usage:

You must provide the following four variables when creating a Coralogix logger instance.

ParameterDescription
Private KeyThe private key can be found under ‘settings’->’ send your logs’.
Company IDA unique ID that represents your company.
The private key can be found under ‘settings’->’ send your logs’.
Application NameThe application Tag you wish to append to every log line sent
SubSystem NameThe subsystem Tag you wish to append to every log line sent

If your Coralogix account top-level domain is not ‘.com’ set the following environment variables:

CORALOGIX_LOG_URL=https://<coralogix_cluster_url>/api/v1/logs
Cluster locationcoralogix_cluster_url
USapi.coralogix.us
Indiaapi.app.coralogix.in
Singaporeapi.coralogixsg.com

Below are Code examples to demonstrate how to add the appender

LOG4J-1 appender:

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, YOUR_LOGGER, YOUR_LOGGER2, YOUR_LOGGER3

Note!: YOUR_LOGGER is another logger you have defined in your log4j configuration that you want to be defined on the rootLogger

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"/>
            <AppenderRef ref="YOUR_LOGGER"/>
            <AppenderRef ref="YOUR_LOGGER2"/>
        </Root>
    </Loggers>
</Configuration>

Note: YOUR_LOGGER is another logger you have defined in your log4j configuration that you want to be defined on the rootLogger

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