javaspringspring-bootapache-kafkalog4j2

No type attribute provided for component property in log4j2 with Spring Boot


I have implemented a simple application for streaming logs to apache kafka.In the producer, I have implemented log4j2.xml file for the configuration and now I have to convert .xml file into properties file. It is working properly with .xml but when I use properties file then following error has occurred.

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)

Here my .xml and properties files

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

Updated

No type attribute provided for component property exception is fixed by adding appender.kafka.property.type=property into properties file. But when I run again a different error has occurred.

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)

Solution

  • You should configure your kafka logger following the appenders configuration:

    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