This guide explains how to configure Apache Log4j to send logs to Axiom
Log4j is a Java logging framework developed by the Apache Software Foundation and widely used in the Java community. This page covers how to get started with Log4j, configure it to forward log messages to Fluentd, and send logs to Axiom.
Log4j is a flexible and powerful logging framework for Java applications. To use Log4j in your project, add the necessary dependencies to your pom.xml
file. The dependencies required for Log4j include log4j-core
, log4j-api
, and log4j-slf4j2-impl
for logging capability, and jackson-databind
for JSON support.
Create a new Maven project:
Open the pom.xml
file and replace its contents with the following:
This pom.xml
file includes the necessary Log4j dependencies and configures the Maven Shade plugin to create an executable JAR file.
Create a new file named log4j2.xml
in your root directory and add the following content:
This configuration sets up two appenders:
localhost:24224
. Is uses JSON format for the log messages, which makes it easier to parse and analyze the logs later in Axiom.Log4j supports various log levels, allowing you to control the verbosity of your logs. The main log levels, in order of increasing severity, are the following:
TRACE
: Fine-grained information for debugging.DEBUG
: General debugging information.INFO
: Informational messages.WARN
: Indications of potential problems.ERROR
: Error events that might still allow the app to continue running.FATAL
: Severe error events that might lead the app to cancel.In the configuration above, the root logger level is set to INFO which means it logs messages at INFO level and above (WARN, ERROR, and FATAL).
To set the log level, create a simple Java class to demonstrate these log levels. Create a new file named App.java
in the src/main/java/com/example
directory with the following content:
This class demonstrates the use of different log levels and also shows how to add context to your logs using ThreadContext
.
Fluentd is a popular open-source data collector used to forward logs from Log4j to Axiom. The Log4j configuration is already set up to send logs to Fluentd using the Socket appender. Fluentd acts as a unified logging layer, allowing you to collect, process, and forward logs from various sources to different destinations.
To configure Fluentd, create a configuration file. Create a new file named fluentd.conf
in your project root directory with the following content:
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.DATASET_NAME
with the name of the Axiom dataset where you want to send data.This configuration does the following:
java.log4j
tag to all logs.To simplify the deployment of the Java app and Fluentd, use Docker. Create a new file named Dockerfile
in your project root directory with the following content:
This Dockerfile does the following:
To build the Docker image, run the following command in your project root directory:
Run the container with the following:
This command starts the container, running both Fluentd and your Java app.
Now that your app is running and sending logs to Axiom, you can view them in the Axiom dashboard. Log in to your Axiom account and go to the dataset you specified in the Fluentd configuration.
Logs appear in real-time, with various log levels and context information added.
This guide explains how to configure Apache Log4j to send logs to Axiom
Log4j is a Java logging framework developed by the Apache Software Foundation and widely used in the Java community. This page covers how to get started with Log4j, configure it to forward log messages to Fluentd, and send logs to Axiom.
Log4j is a flexible and powerful logging framework for Java applications. To use Log4j in your project, add the necessary dependencies to your pom.xml
file. The dependencies required for Log4j include log4j-core
, log4j-api
, and log4j-slf4j2-impl
for logging capability, and jackson-databind
for JSON support.
Create a new Maven project:
Open the pom.xml
file and replace its contents with the following:
This pom.xml
file includes the necessary Log4j dependencies and configures the Maven Shade plugin to create an executable JAR file.
Create a new file named log4j2.xml
in your root directory and add the following content:
This configuration sets up two appenders:
localhost:24224
. Is uses JSON format for the log messages, which makes it easier to parse and analyze the logs later in Axiom.Log4j supports various log levels, allowing you to control the verbosity of your logs. The main log levels, in order of increasing severity, are the following:
TRACE
: Fine-grained information for debugging.DEBUG
: General debugging information.INFO
: Informational messages.WARN
: Indications of potential problems.ERROR
: Error events that might still allow the app to continue running.FATAL
: Severe error events that might lead the app to cancel.In the configuration above, the root logger level is set to INFO which means it logs messages at INFO level and above (WARN, ERROR, and FATAL).
To set the log level, create a simple Java class to demonstrate these log levels. Create a new file named App.java
in the src/main/java/com/example
directory with the following content:
This class demonstrates the use of different log levels and also shows how to add context to your logs using ThreadContext
.
Fluentd is a popular open-source data collector used to forward logs from Log4j to Axiom. The Log4j configuration is already set up to send logs to Fluentd using the Socket appender. Fluentd acts as a unified logging layer, allowing you to collect, process, and forward logs from various sources to different destinations.
To configure Fluentd, create a configuration file. Create a new file named fluentd.conf
in your project root directory with the following content:
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.DATASET_NAME
with the name of the Axiom dataset where you want to send data.This configuration does the following:
java.log4j
tag to all logs.To simplify the deployment of the Java app and Fluentd, use Docker. Create a new file named Dockerfile
in your project root directory with the following content:
This Dockerfile does the following:
To build the Docker image, run the following command in your project root directory:
Run the container with the following:
This command starts the container, running both Fluentd and your Java app.
Now that your app is running and sending logs to Axiom, you can view them in the Axiom dashboard. Log in to your Axiom account and go to the dataset you specified in the Fluentd configuration.
Logs appear in real-time, with various log levels and context information added.