Skip to content

Commit 0495b40

Browse files
authored
Merge pull request #1 from newrelic-experimental/custom-log4j-Appender
feat: 1st working draft Custom log4j2 appender
2 parents b9a65b3 + 1aea27b commit 0495b40

File tree

15 files changed

+1212
-46
lines changed

15 files changed

+1212
-46
lines changed

README.md

Lines changed: 118 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,159 @@
11
<a href="https://opensource.newrelic.com/oss-category/#new-relic-experimental"><picture><source media="(prefers-color-scheme: dark)" srcset="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/dark/Experimental.png"><source media="(prefers-color-scheme: light)" srcset="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/Experimental.png"><img alt="New Relic Open Source experimental project banner." src="https://github.com/newrelic/opensource-website/raw/main/src/images/categories/Experimental.png"></picture></a>
22

33

4-
![GitHub forks](https://img.shields.io/github/forks/newrelic-experimental/java-instrumentation-template?style=social)
5-
![GitHub stars](https://img.shields.io/github/stars/newrelic-experimental/java-instrumentation-template?style=social)
6-
![GitHub watchers](https://img.shields.io/github/watchers/newrelic-experimental/java-instrumentation-template?style=social)
4+
![GitHub forks](https://img.shields.io/github/forks/newrelic-experimental/newrelic-java-log4j-appender?style=social)
5+
![GitHub stars](https://img.shields.io/github/stars/newrelic-experimental/newrelic-java-log4j-appender?style=social)
6+
![GitHub watchers](https://img.shields.io/github/watchers/newrelic-experimental/newrelic-java-log4j-appender?style=social)
77

8-
![GitHub all releases](https://img.shields.io/github/downloads/newrelic-experimental/java-instrumentation-template/total)
9-
![GitHub release (latest by date)](https://img.shields.io/github/v/release/newrelic-experimental/java-instrumentation-template)
10-
![GitHub last commit](https://img.shields.io/github/last-commit/newrelic-experimental/java-instrumentation-template)
11-
![GitHub Release Date](https://img.shields.io/github/release-date/newrelic-experimental/java-instrumentation-template)
8+
![GitHub all releases](https://img.shields.io/github/downloads/newrelic-experimental/newrelic-java-log4j-appender/total)
9+
![GitHub release (latest by date)](https://img.shields.io/github/v/release/newrelic-experimental/newrelic-java-log4j-appender)
10+
![GitHub last commit](https://img.shields.io/github/last-commit/newrelic-experimental/newrelic-java-log4j-appender)
11+
![GitHub Release Date](https://img.shields.io/github/release-date/newrelic-experimental/newrelic-java-log4j-appender)
1212

1313

14-
![GitHub issues](https://img.shields.io/github/issues/newrelic-experimental/java-instrumentation-template)
15-
![GitHub issues closed](https://img.shields.io/github/issues-closed/newrelic-experimental/java-instrumentation-template)
16-
![GitHub pull requests](https://img.shields.io/github/issues-pr/newrelic-experimental/java-instrumentation-template)
17-
![GitHub pull requests closed](https://img.shields.io/github/issues-pr-closed/newrelic-experimental/java-instrumentation-template)
14+
![GitHub issues](https://img.shields.io/github/issues/newrelic-experimental/newrelic-java-log4j-appender)
15+
![GitHub issues closed](https://img.shields.io/github/issues-closed/newrelic-experimental/newrelic-java-log4j-appender)
16+
![GitHub pull requests](https://img.shields.io/github/issues-pr/newrelic-experimental/newrelic-java-log4j-appender)
17+
![GitHub pull requests closed](https://img.shields.io/github/issues-pr-closed/newrelic-experimental/newrelic-java-log4j-appender)
1818

19+
# New Relic Log4j2 Appender
1920

20-
# [Project Name - use format "newrelic-java-<name>"] [build badges go here when available]
21+
A custom Log4j2 appender that sends logs to New Relic.
2122

22-
>[Brief description - what is the project and value does it provide? How often should users expect to get releases? How is versioning set up? Where does this project want to go?]
23+
## Installation
2324

24-
## Value
25+
Add the library to your project using Maven Central:
2526

26-
|Metrics | Events | Logs | Traces | Visualization | Automation |
27-
|:-:|:-:|:-:|:-:|:-:|:-:|
28-
|:x:|:x:|:x:|:white_check_mark:|:x:|:x:|
27+
```xml
28+
<dependency>
29+
<groupId>io.github.newrelic-experimental</groupId>
30+
<artifactId>custom-log4j2-appender</artifactId>
31+
<version>0.0.8</version>
32+
</dependency>
33+
```
2934

30-
### List of Metrics,Events,Logs,Traces
31-
|Name | Type | Description |
32-
|:-:|:-:|:-:|
33-
|*metric.name* | Metric| *description*|
34-
|*event.name* | Event| *description*|
35-
|*log.name* | Log| *description*|
36-
|*trace.name*| Trace| *description*
37-
|---|---|---|
35+
Or, if using a locally built JAR file:
3836

37+
```xml
38+
<dependency>
39+
<groupId>io.github.newrelic-experimental</groupId>
40+
<artifactId>custom-log4j2-appender</artifactId>
41+
<version>0.0.8</version>
42+
<scope>system</scope>
43+
<systemPath>${project.basedir}/src/main/resources/custom-log4j2-appender.jar</systemPath>
44+
</dependency>
45+
```
3946

40-
## Installation
47+
## Usage
4148

42-
> [Include a step-by-step procedure on how to get your code installed. Be sure to include any third-party dependencies that need to be installed separately]
49+
### Set up New Relic Log4j2 Appender
4350

44-
## Getting Started
51+
Follow the instructions for setting up the New Relic Log4j2 Appender.
4552

46-
>[Simple steps to start working with the software similar to a "Hello World"]
53+
### Log4J XML Configuration
4754

48-
## Usage
55+
Replace `[your-api-key]` with the ingest key obtained from the New Relic platform.
56+
57+
#### log4j2.xml:
58+
59+
```xml
60+
<?xml version="1.0" encoding="UTF-8"?>
61+
<Configuration status="DEBUG" name="cloudhub" packages="com.newrelic.labs">
62+
<Appenders>
63+
<!-- Custom New Relic Batching Appender with Pattern Layout -->
64+
<NewRelicBatchingAppender name="NewRelicAppender"
65+
apiKey="[your-api-key]"
66+
apiUrl="https://log-api.newrelic.com/log/v1"
67+
logType="muleLog"
68+
applicationName="your-application-name"
69+
batchSize="5000"
70+
maxMessageSize="1048576"
71+
flushInterval="120000">
72+
<PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n"/>
73+
</NewRelicBatchingAppender>
74+
</Appenders>
75+
76+
<Loggers>
77+
<AsyncRoot level="INFO">
78+
<AppenderRef ref="NewRelicAppender"/>
79+
</AsyncRoot>
80+
</Loggers>
81+
</Configuration>
82+
```
83+
84+
### Parameters
85+
86+
| Parameter | Required? | Default Value | Description |
87+
|---------------------|-----------|---------------|-----------------------------------------------------------------------------|
88+
| name | Yes | | Name used to register Log4j Appender |
89+
| apiKey | Yes | | API key for authenticating with New Relic's logging service |
90+
| apiUrl | Yes | | URL for New Relic's log ingestion API |
91+
| logType | No | "muleLog" | Type of log being sent |
92+
| applicationName | Yes | | Name of the application generating the logs |
93+
| batchSize | No | 5000 | Maximum number of log entries to batch together before sending to New Relic |
94+
| maxMessageSize | No | 1048576 | Maximum size (in bytes) of the payload to be sent in a single HTTP request |
95+
| flushInterval | No | 120000 | Interval (in milliseconds) at which the log entries are flushed to New Relic|
96+
97+
### TLS 1.2 Requirement
4998

50-
>[**Optional** - Include more thorough instructions on how to use the software. This section might not be needed if the Getting Started section is enough. Remove this section if it's not needed.]
99+
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.
100+
101+
### Create GROK Parsing Rule at New Relic Platform
102+
103+
- **Name**: `NRMuleParser`
104+
- **Field to parse**: `message`
105+
- **Filter logs based on NRQL**: `logtype = 'muleLog'`
106+
- **Parsing rule**:
107+
```sh
108+
%{LOGLEVEL:log.level} %{DATA:log.logger} \[%{DATA:log.thread}\]: %{GREEDYDATA:log.message}
109+
```
110+
111+
## Sample log details at New Relic Platform
112+
113+
<img width="745" alt="image" src="https://github.com/user-attachments/assets/6adff21d-7fdf-4b39-b19e-0ed385ff6ed5">
51114

52115
## Building
53116

54-
>[**Optional** - Include this section if users will need to follow specific instructions to build the software from source. Be sure to include any third party build dependencies that need to be installed separately. Remove this section if it's not needed.]
117+
### Building the Local JAR File
118+
119+
```sh
120+
cd custom-log4j2-appender
121+
./build-local.sh
122+
```
55123

56-
## Testing
124+
### Copying the Local JAR File to the Destination
57125

58-
>[**Optional** - Include instructions on how to run tests if we include tests with the codebase. Remove this section if it's not needed.]
126+
Example:
127+
128+
```sh
129+
cp build/libs/custom-log4j2-appender.jar /Users/gsidhwani/AnypointStudio/studio-workspace/samplemuleapp/src/main/resources/
130+
```
131+
132+
## Scripts
133+
134+
### `publish-jar.sh` and `publish-shadowJar.sh`
135+
136+
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.
59137

60138
## Support
61139

62140
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.
63141

64-
>[Choose 1 of the 2 options below for Support details, and remove the other one.]
65-
66-
>[Option 1 - no specific thread in Community]
67-
>We encourage you to bring your experiences and questions to the [Explorers Hub](https://discuss.newrelic.com) where our community members collaborate on solutions and new ideas.
142+
### Community Support
68143

69-
>[Option 2 - thread in Community]
70-
>New Relic hosts and moderates an online forum where customers can interact with New Relic employees as well as other customers to get help and share best practices. Like all official New Relic open source projects, there's a related Community topic in the New Relic Explorers Hub.
71-
>You can find this project's topic/threads here: [URL for Community thread]
144+
We encourage you to bring your experiences and questions to the [Explorers Hub](https://discuss.newrelic.com) where our community members collaborate on solutions and new ideas.
72145

73146
## Contributing
74147

75-
We encourage your contributions to improve [Project Name]! Keep in mind 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 opensource@newrelic.com.
148+
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 opensource@newrelic.com.
76149

77-
**A note about vulnerabilities**
150+
### A Note About Vulnerabilities
78151

79-
As noted in our [security policy](../../security/policy), New Relic is committed to the privacy and security of our customers and their data. We believe that providing coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.
152+
As noted in our [security policy](../../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.
80153

81154
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](https://hackerone.com/newrelic).
82155

83156
## License
84157

85-
[Project Name] is licensed under the [Apache 2.0](http://apache.org/licenses/LICENSE-2.0.txt) License.
158+
The New Relic Log4j 2 Appender is licensed under the [Apache 2.0](http://apache.org/licenses/LICENSE-2.0.txt) License.
86159

87-
>[If applicable: [Project Name] also uses source code from third-party libraries. You can find full details on which libraries are used and the terms under which they are licensed in the third-party notices document.]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
plugins {
2+
id 'java'
3+
id 'maven-publish'
4+
}
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
dependencies {
11+
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
12+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
13+
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
14+
implementation 'org.apache.logging.log4j:log4j-api:2.14.1'
15+
}
16+
17+
jar {
18+
manifest {
19+
attributes(
20+
'Implementation-Title': 'Custom Log4j2 Appender',
21+
'Implementation-Vendor': 'New Relic Labs',
22+
'Implementation-Vendor-Id': 'com.newrelic.labs',
23+
'Implementation-Version': '0.0.8'
24+
)
25+
}
26+
}
27+
28+
29+
30+
tasks.withType(JavaCompile) {
31+
options.encoding = 'UTF-8'
32+
}
33+
34+
task javadocJar(type: Jar) {
35+
archiveClassifier.set('javadoc')
36+
from javadoc
37+
}
38+
39+
task sourcesJar(type: Jar) {
40+
archiveClassifier.set('sources')
41+
from sourceSets.main.allSource
42+
}
43+
44+
45+
46+
publishing {
47+
publications {
48+
mavenJava(MavenPublication) {
49+
from components.java // Include standard JAR
50+
51+
artifact javadocJar
52+
artifact sourcesJar
53+
54+
groupId = 'io.github.newrelic-experimental'
55+
artifactId = 'custom-log4j2-appender'
56+
version = '0.0.8'
57+
58+
pom {
59+
name = 'Custom Log4j2 Appender'
60+
description = 'A custom Log4j2 appender for New Relic.'
61+
url = 'https://github.com/newrelic-experimental/newrelic-java-log4j-appender'
62+
licenses {
63+
license {
64+
name = 'The Apache License, Version 2.0'
65+
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
66+
}
67+
}
68+
developers {
69+
developer {
70+
id = 'newrelic'
71+
name = 'New Relic'
72+
email = 'gsidhwani@newrelic.com'
73+
}
74+
}
75+
scm {
76+
connection = 'scm:git:git://github.com/newrelic-experimental/newrelic-java-log4j-appender.git'
77+
developerConnection = 'scm:git:ssh://github.com/newrelic-experimental/newrelic-java-log4j-appender.git'
78+
url = 'https://github.com/newrelic-experimental/newrelic-java-log4j-appender'
79+
}
80+
}
81+
}
82+
}
83+
84+
repositories {
85+
maven {
86+
name = 'sonatype'
87+
url = uri('https://oss.sonatype.org/service/local/staging/deploy/maven2/')
88+
credentials {
89+
username = project.findProperty("ossrhUsername") ?: ""
90+
password = project.findProperty("ossrhPassword") ?: ""
91+
}
92+
}
93+
}
94+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# clean existing stuff
3+
rm -rf build io
4+
# copy correct build.gradle
5+
cp build-shadowJar.gradle build.gradle
6+
7+
# Get the current directory (assuming the script is run from the custom-log4j2-appender directory)
8+
CURRENT_DIR=$(pwd)
9+
PROJECT_ROOT=$(dirname "$CURRENT_DIR")
10+
11+
echo "Current Directory: $CURRENT_DIR"
12+
echo "Project Root: $PROJECT_ROOT"
13+
14+
# Clean and build the project
15+
cd "$PROJECT_ROOT"
16+
./gradlew clean build shadowJar
17+
18+
# Navigate to the directory containing the artifacts
19+
cd "$CURRENT_DIR/build/libs"
20+
21+
# List the contents of the build/libs directory for debugging
22+
echo "Contents of build/libs:"
23+
ls -la
24+
25+
echo "Local jar custom-log4j2-appender.jar is produced. You can now copy custom-log4j2-appender.jar to your applications classpath ."

0 commit comments

Comments
 (0)