我已经实现了一个简单的应用程序,用于将日志流式传输到apache kafka。在生产者中,我已经实现了用于配置的
log4j2.xml
文件,现在我必须将.xml
文件转换为properties
文件。它与 .xml
一起正常工作,但是当我使用 properties
文件时,出现以下错误。
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:196)
at com.benz.producer.log.api.LogProducerApplication.main(LogProducerApplication.java:10)
Caused by: org.apache.logging.log4j.core.config.ConfigurationException: No type attribute provided for component property
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createComponent(PropertiesConfigurationBuilder.java:334)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.processRemainingProperties(PropertiesConfigurationBuilder.java:348)
这是我的
.xml
和 properties
文件
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="spring-boot-kafka-log" status="info"
packages="com.benz.producer.log.api">
<Appenders>
<Kafka name="Kafka" topic="LOG_TOPIC">
<PatternLayout pattern="%date %message"/>
<Property name="bootstrap.servers">
localhost:9092
</Property>
</Kafka>
<Async name="Async">
<AppenderRef ref="Kafka"/>
</Async>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5p [%-7t] %F:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Kafka"/>
<AppenderRef ref="stdout"/>
</Root>
<Logger name="org.apache.kafka" level="WARN"/>
</Loggers>
</Configuration>
log4j2.properties
name=spring-boot-kafka-log
status=info
packages=com.benz.producer.log.api
appenders=console,kafka
appender.console.type=console
appender.console.name=stdout
appender.console.target=SYSTEM_OUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{HH:mm:ss.SSS} %-5 [%-7t] %F:%L - %m%n
appender.kafka.type=kafka
appender.kafka.name=Kafka
appender.kafka.topic=LOG_TOPIC
appender.kafka.layout.type=PatternLayout
appender.kafka.layout.pattern=%date %message
appender.kafka.property.name=bootstrap.servers
appender.kafka.property.value=localhost:9092
rootLogger.level=INFO
rootLogger.appenderRef.ref=kafka
rootLogger.appenderRef.kafka.ref=stdout
logger.name=org.apache.kafka
logger.level=WARN
已更新
通过将No type attribute provided for component property
添加到属性文件中来修复
appender.kafka.property.type=property
异常。但是当我再次运行时,出现了不同的错误。
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:196)
at com.benz.producer.log.api.LogProducerApplication.main(LogProducerApplication.java:10)
Caused by: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 5
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)
您应该按照
appenders配置来配置您的
kafka
记录器:
name=spring-boot-kafka-log
status=info
packages=com.benz.producer.log.api
appenders=console,kafka
appender.console.type=console
appender.console.name=stdout
appender.console.target=SYSTEM_OUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{HH:mm:ss.SSS} %-5level [%-7t] %F:%L - %m%n
appender.kafka.type=kafka
appender.kafka.name=Kafka
appender.kafka.topic=LOG_TOPIC
appender.kafka.layout.type=PatternLayout
appender.kafka.layout.pattern=%date %message
appender.kafka.property.name=bootstrap.servers
appender.kafka.property.value=localhost:9092
rootLogger.level = INFO
rootLogger.appenderRefs = console, kafka
rootLogger.appenderRef.kafka.ref = Kafka
rootLogger.appenderRef.console.ref = stdout
loggers = kafka
logger.kafka.name = org.apache.kafka
logger.kafka.level = WARN