Sample using Event Hub Log4j Appender (#482)

* sample added for Log4j Event Hub appender

* updated ReadME

* updated files
This commit is contained in:
sagar027 2021-09-03 04:34:54 +05:30 коммит произвёл Serkant Karaca
Родитель 09a38157db
Коммит 033cc8937b
5 изменённых файлов: 141 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,14 @@
# EventHub Appender Java Sample
This sample demonstrates the use of EventHubAppender for streaming/sending log4j2 logs to Azure Event Hub.
The EventHubAppender extension (Log4j plugin) code can be found at https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/microsoft-azure-eventhubs-extensions
Note : The above extension depends on [this version of Azure Event Hubs library](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/microsoft-azure-eventhubs).
Steps to run the project :
1. Clone the repository.
2. Update the value of 'eventHubConnectionString' in src/main/resources/log4j2.xml file. (This is the config file)
3. Update the value of 'classpathPrefix' element in the pom.xml file of the project
4. Run 'mvn clean'
5. Run 'mvn package'
6. Run 'java -cp target/my-app-1.0-SNAPSHOT.jar:<classpath-to-jar-dependencies>/*: com.eventhubappendersample.app.App'

Просмотреть файл

@ -0,0 +1,74 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>log4jeventhubappendersample</groupId>
<artifactId>log4jeventhubappendersample</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>[2.13.2,)</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventhubs-extensions</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.microsoft.azure.eventhubs.samples.log4jeventhubappendersample.Log4JEventHubAppenderSample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

Просмотреть файл

@ -0,0 +1,29 @@
package com.microsoft.azure.eventhubs.samples.log4jeventhubappendersample;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Hello world!
*
*/
public class Log4JEventHubAppenderSample
{
private static final Logger logger = LogManager.getLogger(Log4JEventHubAppenderSample.class);
public static void main( String[] args )
{
int i;
for(i=0; i<=10;i++)
{
try {
TimeUnit.SECONDS.sleep(1);
logger.error("Hello World " + i);
}
catch(InterruptedException e)
{
}
}
}
}

Просмотреть файл

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="all" packages="com.microsoft.azure.eventhubs.extensions.appender">
<Appenders>
<EventHub name="EventHub" ignoreExceptions="false" eventHubConnectionString="--event-hub-connection-string" immediateFlush="true">
<JSONLayout />
</EventHub>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="all">
<appender-ref ref="console" />
<appender-ref ref="EventHub" />
</Root>
</Loggers>
</Configuration>

Просмотреть файл

@ -30,7 +30,8 @@
<module>Basic/SendBatch</module> <module>Basic/SendBatch</module>
<module>Basic/SimpleSend</module> <module>Basic/SimpleSend</module>
<module>Basic/AsyncSend</module> <module>Basic/AsyncSend</module>
<module>Basic/SimpleProxy</module> <module>Basic/SimpleProxy</module>
<module>Basic/Log4jAppenderSample</module>
<module>Benchmarks/AutoScaleOnIngress</module> <module>Benchmarks/AutoScaleOnIngress</module>
<module>Benchmarks/IngressBenchmark</module> <module>Benchmarks/IngressBenchmark</module>
</modules> </modules>