Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR Kafka contains an invalid element or attribute #25

Open
schotten opened this issue Aug 20, 2021 · 0 comments
Open

ERROR Kafka contains an invalid element or attribute #25

schotten opened this issue Aug 20, 2021 · 0 comments

Comments

@schotten
Copy link

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...

<Properties>
    <Property name="LOG_PATTERN">
        %c.%M %m %ex%n
    </Property>
</Properties>

<Appenders>
    <ScriptAppenderSelector name="enableKafka">
        <Script language="js">
            <![CDATA[
                load("nashorn:mozilla_compat.js");
                importPackage(java.lang);
                var profileSpring = System.getProperty("spring.profiles.active");
                var profileEnvironment = System.getenv("SPRING_PROFILES_ACTIVE");
                var profile = null;

                if(profileSpring != null){
                    profile = profileSpring;
                }else{
                    profile = profileEnvironment
                }

                //System.out.println("The profile spring is " + profileSpring);
               //System.out.println("The profile enviroment is " + profileEnvironment);
                "dev1".equals(profile) ? "KafkaAppender" : "KafkaAppender";
            ]]>
        </Script>
        <AppenderSet>
            <Kafka name="KafkaAppender" topic="logProducer" syncSend="false" ignoreExceptions="true">
                <Property name="bootstrap.servers">http://logging.weg.net:9092</Property>
                <MarkerFilter marker="GlobalLog" onMatch="ACCEPT" onMismatch="DENY"/>
                <WDD3DLayout/>
            </Kafka>
            <Null name="Null"/>
        </AppenderSet>
    </ScriptAppenderSelector>
    <Console name="ConsoleAppender" target="SYSTEM_OUT">
        <PatternLayout
                pattern="%d [%t]  %highlight{%-5level}{FATAL=white, ERROR=red, WARN=yellow, INFO=blue, DEBUG=green, TRACE=cyan}: %msg%n%throwable"
                disableAnsi="false"/>
    </Console>
</Appenders>

<Loggers>
    <Root level="INFO">
        <AppenderRef ref="ConsoleAppender"/>
        <AppenderRef ref="enableKafka" />
    </Root>
</Loggers>

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant