a0x8o / kafka

A high-throughput, distributed, publish-subscribe messaging system
Apache License 2.0
67 stars 172 forks source link

ERROR Kafka contains an invalid element or attribute #25

Open schotten opened 3 years ago

schotten commented 3 years ago

Hi, i have one problema in my application and i need to check...

1 - i use the springboot in this version:

org.springframework.boot spring-boot-starter-parent 2.5.0

and, this dependency for log4j and kafka

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-log4j-appender</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

I use the log4j2.xml in the atachment...

<?xml version="1.0" encoding="UTF-8"?>

%c.%M %m %ex%n http://logging.weg.net:9092

In the springBoot application, i have this plugin configutated:

package net.weg.maestro.infrastructure.logging;

import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import net.weg.maestro.wdd3d.WddException; import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.Node; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.PluginAttribute; import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.layout.AbstractStringLayout;

import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.charset.Charset;

@Plugin(name = "WDD3DLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) public class GlobalLogLayout extends AbstractStringLayout {

protected GlobalLogLayout(Charset charSet) {
    super(charSet);
}

@PluginFactory
public static GlobalLogLayout createLayout(@PluginAttribute(value = "charset", defaultString = "UTF-8") Charset charset) {
    return new GlobalLogLayout(charset);
}

@Override
public String toSerializable(LogEvent logEvent) {
    try {
        return new ObjectMapper().writeValueAsString(SerializableLogEvent.from(logEvent));
    } catch (JsonProcessingException e) {
        throw new WddException("It was not possible to serialize logEvent object.", e);
    }
}

private static class SerializableLogEvent {

    private String message;

    private String level;

    private String server;

    private String uuid;

    private String application;

    private String timestamp;

    private String user;

    public static SerializableLogEvent from(LogEvent logEvent){
        SerializableLogEvent serializableLogEvent = new SerializableLogEvent();
        serializableLogEvent.setMessage(logEvent.getMessage().getFormattedMessage());
        serializableLogEvent.setLevel(logEvent.getLevel().name());
        try {
            serializableLogEvent.setServer(InetAddress.getLocalHost().toString());
        } catch (UnknownHostException ignore) {
            serializableLogEvent.setServer("");
        }
        serializableLogEvent.setUuid((String) logEvent.getMessage().getParameters()[0]);
        serializableLogEvent.setApplication(logEvent.getMessage().getParameters()[1].toString());
        serializableLogEvent.setTimestamp(logEvent.getMessage().getParameters()[2].toString());
        serializableLogEvent.setUser(logEvent.getMessage().getParameters()[3].toString());
        return serializableLogEvent;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getApplication() {
        return application;
    }

    public void setApplication(String application) {
        this.application = application;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
}

}

When, i start the aplication, the below problem ocorr: "ERROR Kafka contains an invalid element or attribute "WDD3DLayout"" I don't found the error... can you help? is it one Bug?

image