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.
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 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 URL | Coralogix API URL |
---|---|
coralogix.com | api.coralogix.com |
coralogix.us | api.coralogix.us |
eu2.coralogix.com | api.eu2.coralogix.com |
app.coralogix.in | api.app.coralogix.in |
coralogixsg.com | api.coralogixsg.com |
Below are Code examples to demonstrate how to add the 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"); } }
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.