Add Log4J2 sample with Spring Boot
SO: http://stackoverflow.com/questions/37868066/spring-boot-amqpappender-with-log4j2
This commit is contained in:
96
log4j2/pom.xml
Normal file
96
log4j2/pom.xml
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>org.springframework.amqp.samples</groupId>
|
||||
<artifactId>log4j2</artifactId>
|
||||
<version>1.6.0.RELEASE</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>log4j2</name>
|
||||
<description>Spring Boot with Spring AMQP Log4J2 Appender</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.0.M3</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.amqp.samples.log4j2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.amqp.core.ExchangeTypes;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 1.6
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class SpringBootAmqpAppenderApplication {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SpringBootAmqpAppenderApplication.class);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootAmqpAppenderApplication.class, args);
|
||||
System.out.println("Hit 'Enter' to terminate");
|
||||
System.in.read();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
|
||||
public void generateLog() {
|
||||
logger.info("Log via AmqpAppender: " + new Date());
|
||||
}
|
||||
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
exchange = @Exchange(value = "log4j2Sample", type = ExchangeTypes.FANOUT),
|
||||
value = @org.springframework.amqp.rabbit.annotation.Queue))
|
||||
public void echoLogs(String logMessage) {
|
||||
System.out.println("Logs over Log4J AmqpAppender: " + logMessage);
|
||||
}
|
||||
|
||||
}
|
||||
32
log4j2/src/main/resources/log4j2.xml
Normal file
32
log4j2/src/main/resources/log4j2.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration>
|
||||
<Appenders>
|
||||
<Console name="STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%m%n" />
|
||||
</Console>
|
||||
<RabbitMQ name="rabbitmq"
|
||||
addresses="localhost:5672"
|
||||
user="guest"
|
||||
password="guest"
|
||||
virtualHost="/"
|
||||
exchange="log4j2Sample"
|
||||
applicationId="log4j2SampleAppId"
|
||||
routingKeyPattern="%X{applicationId}.%c.%p"
|
||||
contentType="text/plain"
|
||||
contentEncoding="UTF-8"
|
||||
generateId="false"
|
||||
deliveryMode="PERSISTENT"
|
||||
charset="UTF-8"
|
||||
senderPoolSize="3"
|
||||
maxSenderRetries="5">
|
||||
</RabbitMQ>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.springframework.amqp.samples.log4j2" level="info">
|
||||
<AppenderRef ref="rabbitmq" />
|
||||
</Logger>
|
||||
<Root>
|
||||
<AppenderRef ref="STDOUT" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user