Skip to content

A custom Log4j2 appender for New Relic that batches and transmits log entries from Java applications, including MuleSoft, to New Relic's logging service.

License

Notifications You must be signed in to change notification settings

newrelic/newrelic-java-log4j-appender

New Relic Open Source community project banner.

GitHub forks GitHub stars GitHub watchers

GitHub all releases GitHub release (latest by date) GitHub last commit GitHub Release Date

GitHub issues GitHub issues closed GitHub pull requests GitHub pull requests closed

New Relic Log4j2 Appender

A custom Log4j2 appender that sends logs to New Relic. This appender supports both plain text log messages and JSON log objects.

Installation

Add the library to your project using Maven Central:

<dependency>
    <groupId>com.newrelic.labs</groupId>
    <artifactId>custom-log4j2-appender</artifactId>
    <version>1.0.6</version>
</dependency>

Or, if using a locally built JAR file:

<dependency>
    <groupId>com.newrelic.labs</groupId>
    <artifactId>custom-log4j2-appender</artifactId>
    <version>1.0.6</version>

    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/custom-log4j2-appender.jar</systemPath>
</dependency>

Usage

Set up New Relic Log4j2 Appender

Follow the instructions for setting up the New Relic Log4j2 Appender.

Log4J2 XML Configuration

Replace [your-api-key] with the ingest key obtained from the New Relic platform.

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" name="cloudhub" packages="com.newrelic.labs">
    <Appenders>
        <NewRelicBatchingAppender name="NewRelicAppender"
                                  apiKey="YOUR_API_KEY"
                                  apiUrl="https://log-api.newrelic.com/log/v1"
                                  logType="muleLog"
                                  applicationName="your-application-name"
                                  batchSize="2000"
                                  maxMessageSize="1048576"
                                  flushInterval="120000"
                                  customFields="businessGroup=exampleGroup,environment=production"
                                  mergeCustomFields="true"
                                  maxRetries="3"
                                  connPoolSize="10"
                                  queueCapacity="2097152"
                                  timeout="15000">
            <PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n"/>
        </NewRelicBatchingAppender>
    </Appenders>
    <Loggers>
        <AsyncRoot level="INFO">
            <!-- Comment out the NewRelicAppender reference to disable it -->
            <AppenderRef ref="NewRelicAppender"/>
        </AsyncRoot>
    </Loggers>
</Configuration>

Parameters

Parameter Required? Default Value Description
name Yes Name used to register Log4j Appender
apiKey Yes API key for authenticating with New Relic's logging service
apiUrl Yes URL for New Relic's log ingestion API
logType No "muleLog" Type of log being sent
applicationName Yes Name of the application generating the logs
batchSize No 2000 Maximum number of log entries to batch together before sending to New Relic
maxMessageSize No 1048576 Maximum size (in bytes) of the payload to be sent in a single HTTP request
flushInterval No 120000 Interval (in milliseconds) at which the log entries are flushed to New Relic
customFields No Add extra context to your logs with custom fields, represented as comma-separated name-value pairs.
mergeCustomFields No "false" Whether to merge custom fields into the main log attributes
maxRetries No 3 Maximum number of retry attempts for sending logs
connPoolSize No 5 Size of the connection pool for HTTP requests
queueCapacity No 2097152 Maximum capacity (in bytes) of the log queue
timeout No 30000 Connection timeout (in milliseconds) for HTTP requests

Log4j2 Layouts

PatternLayout

  • Purpose: Formats logs in a customizable text format.
  • Example:
    <PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n"/>
  • Use Case: Ideal for human-readable logs.

JsonLayout

  • Purpose: Formats logs as JSON objects for structured logging.
  • Example:
    <JsonLayout compact="true" eventEol="true"/>
  • Use Case: Suitable for integration with log management systems.

Choosing Layout

  • PatternLayout: For text-based logs.
  • JsonLayout: For structured, machine-readable logs.

Custom Fields [ v1.0.1 + ]

Custom fields provide a way to include additional custom data in your logs. They are represented as comma-separated name-value pairs. This feature allows you to add more context to your logs, making them more meaningful and easier to analyze.

Configuring Custom Fields as Subfields of Custom Fields [v1.0.3+]

Starting from version 1.0.3, a new configuration parameter mergeCustomFields has been added. By default, all custom fields will be available as subfields under the custom field (e.g., custom.field1, custom.field2). If mergeCustomFields is set to true, custom fields will be available as main attributes (e.g., field1, field2).

Configuring queueCapacity and connPoolSize [v1.0.6+]

Starting from version 1.0.6, the queueCapacity and connPoolSize parameters are exposed to allow for fine-tuning of the appender's performance, especially under high load conditions. These parameters help manage the flow of log data and the efficiency of network connections.

TLS 1.2 Requirement

New Relic only accepts connections from clients using TLS version 1.2 or greater. Ensure that your execution environment is configured to use TLS 1.2 or greater.

Create GROK Parsing Rule at New Relic Platform

  • Name: NRMuleParser
  • Field to parse: message
  • Filter logs based on NRQL: logtype = 'muleLog'
  • Parsing rule:
    %{LOGLEVEL:log.level}  %{DATA:log.logger} \[%{DATA:log.thread}\]: %{GREEDYDATA:log.message}

Sample log details at New Relic Platform

image

Building

Please ensure JDK 8 is installed for this build.

Building the Local JAR File

cd custom-log4j2-appender
./build-local.sh 

Copying the Local JAR File to the Destination

Example:

cp build/libs/custom-log4j2-appender.jar /Users/gsidhwani/AnypointStudio/studio-workspace/samplemuleapp/src/main/resources/

Scripts

publish-jar.sh and publish-shadowJar.sh

These scripts generate a Maven publishing compatible bundle for publishing to Maven Central. This is for New Relic's use, and users are not required to use it. They should mention this dependency in their application's pom.xml file to pull it directly from Maven Central or build locally as per the instructions above.

Support

New Relic has open-sourced this project. This project is provided AS-IS WITHOUT WARRANTY OR DEDICATED SUPPORT. Issues and contributions should be reported to the project here on GitHub.

Community Support

We encourage you to bring your experiences and questions to the Explorers Hub where our community members collaborate on solutions and new ideas.

Contributing

We encourage your contributions to improve the New Relic Log4j 2 Appender! When you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project. If you have any questions, or to execute our corporate CLA (required if your contribution is on behalf of a company), please drop us an email at [email protected].

A Note About Vulnerabilities

As noted in our security policy, New Relic is committed to the privacy and security of our customers and their data. We believe that coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.

If you believe you have found a security vulnerability in this project or any of New Relic's products or websites, we welcome and greatly appreciate you reporting it to New Relic through HackerOne.

License

The New Relic Log4j 2 Appender is licensed under the Apache 2.0 License.

About

A custom Log4j2 appender for New Relic that batches and transmits log entries from Java applications, including MuleSoft, to New Relic's logging service.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published