- log4j2-spring.xml config file created under src/main/resources:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RabbitMQ name="rmqAppender" host="${rabbitmq.in.host}"
port="${rabbitmq.in.port}"
virtualHost="${rabbitmq.in.virtualhost}"
username="${rabbitmq.in.username}"
password="${rabbitmq.in.password}"
exchange="${rabbitmq.in.exchange}"
routingKey="${rabbitmq.in.routingkey}">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</RabbitMQ>
</Appenders>
<Loggers>
<Logger name="com.example.demo" level="info" additivity="false">
<AppenderRef ref="rmqAppender" />
</Logger>
<Root level="info">
<!-- <AppenderRef ref="Console" /> -->
<AppenderRef ref="rmqAppender" />
</Root>
</Loggers>
</Configuration>
- Application.properties file is:
rabbitmq.in.queue=queue
rabbitmq.in.exchange=exchange
rabbitmq.in.routingkey=routingkey
rabbitmq.in.host=host
rabbitmq.in.port=port
rabbitmq.in.exchangetype=exchangetype
rabbitmq.in.virtualhost=virtualhost
rabbitmq.in.username=username
rabbitmq.in.password=password
listener.prefetchcount=1
listener.maxConcurrentConsumerCount=10
management.health.rabbit.enabled=false
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
logging.config=classpath:log4j2.xml
- pom.xml is used is (spring-boot version-2.7.11)
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency>
</dependencies>
But facing the error related to configuration file as shown below:
Logging system failed to initialize using configuration from 'classpath:log4j2-spring.xml'
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:13 - no applicable action for [Appenders], current ElementPath is [[Configuration][Appenders]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@38:47 - no applicable action for [RabbitMQ], current ElementPath is [[Configuration][Appenders][RabbitMQ]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@40:69 - no applicable action for [PatternLayout], current ElementPath is [[Configuration][Appenders][RabbitMQ][PatternLayout]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@43:11 - no applicable action for [Loggers], current ElementPath is [[Configuration][Loggers]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@44:66 - no applicable action for [Logger], current ElementPath is [[Configuration][Loggers][Logger]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@45:35 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Logger][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@47:22 - no applicable action for [Root], current ElementPath is [[Configuration][Loggers][Root]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@49:37 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Root][AppenderRef]]
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:179)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:332)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
at java.util.ArrayList.forEach(Unknown Source)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
at com.example.demo.DemoApplication.main(DemoApplication.java:12)
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected:
Need to send the logging custom json message to RMqueue, using asynchronous log4j2 configuration.