BAEL1731 Log4j2 Plugins
This commit is contained in:
parent
4f12335bcf
commit
fe9405607a
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.logging.log4j2.plugins;
|
||||
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.pattern.ConverterKeys;
|
||||
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
|
||||
import org.apache.logging.log4j.core.pattern.PatternConverter;
|
||||
|
||||
@Plugin(name = "DockerPatternConverter", category = PatternConverter.CATEGORY)
|
||||
@ConverterKeys({"docker", "container"})
|
||||
public class DockerPatternConverter extends LogEventPatternConverter {
|
||||
|
||||
private DockerPatternConverter(String[] options) {
|
||||
super("Docker", "docker");
|
||||
}
|
||||
|
||||
public static DockerPatternConverter newInstance(String[] options) {
|
||||
return new DockerPatternConverter(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void format(LogEvent event, StringBuilder toAppendTo) {
|
||||
toAppendTo.append(dockerContainer());
|
||||
}
|
||||
|
||||
private String dockerContainer() {
|
||||
//get docker container ID inside which application is running here
|
||||
return "container-1";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
package com.baeldung.logging.log4j2.plugins;
|
||||
|
||||
import org.apache.logging.log4j.core.Core;
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.Layout;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Plugin(name = "Kafka2", category = Core.CATEGORY_NAME)
|
||||
public class KafkaAppender extends AbstractAppender {
|
||||
|
||||
@PluginBuilderFactory
|
||||
public static Builder newBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder implements org.apache.logging.log4j.core.util.Builder<KafkaAppender> {
|
||||
|
||||
@PluginBuilderAttribute("name")
|
||||
@Required
|
||||
private String name;
|
||||
|
||||
@PluginBuilderAttribute("ip")
|
||||
private String ipAddress;
|
||||
|
||||
@PluginBuilderAttribute("port")
|
||||
private int port;
|
||||
|
||||
@PluginBuilderAttribute("topic")
|
||||
private String topic;
|
||||
|
||||
@PluginBuilderAttribute("partition")
|
||||
private String partition;
|
||||
|
||||
@PluginElement("Layout")
|
||||
private Layout<? extends Serializable> layout;
|
||||
|
||||
@PluginElement("Filter")
|
||||
private Filter filter;
|
||||
|
||||
public Layout<? extends Serializable> getLayout() {
|
||||
return layout;
|
||||
}
|
||||
|
||||
public Builder setLayout(Layout<? extends Serializable> layout) {
|
||||
this.layout = layout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Filter getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setFilter(Filter filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public Builder setIpAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public Builder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getTopic() {
|
||||
return topic;
|
||||
}
|
||||
|
||||
public Builder setTopic(String topic) {
|
||||
this.topic = topic;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPartition() {
|
||||
return partition;
|
||||
}
|
||||
|
||||
public Builder setPartition(String partition) {
|
||||
this.partition = partition;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KafkaAppender build() {
|
||||
return new KafkaAppender(getName(), getFilter(), getLayout(), true, new KafkaBroker(ipAddress, port, topic, partition));
|
||||
}
|
||||
}
|
||||
|
||||
private KafkaBroker broker;
|
||||
|
||||
private KafkaAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, KafkaBroker broker) {
|
||||
super(name, filter, layout, ignoreExceptions);
|
||||
this.broker = broker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(LogEvent event) {
|
||||
|
||||
connectAndSendToKafka(broker, event);
|
||||
}
|
||||
|
||||
private void connectAndSendToKafka(KafkaBroker broker, LogEvent event) {
|
||||
//send to Kafka
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.logging.log4j2.plugins;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class KafkaBroker implements Serializable {
|
||||
|
||||
private final String ipAddress;
|
||||
private final int port;
|
||||
|
||||
public KafkaBroker(String ipAddress, int port, String topic, String partition) {
|
||||
this.ipAddress = ipAddress;
|
||||
this.port = port;
|
||||
this.topic = topic;
|
||||
this.partition = partition;
|
||||
}
|
||||
|
||||
public String getTopic() {
|
||||
return topic;
|
||||
}
|
||||
|
||||
public String getPartition() {
|
||||
return partition;
|
||||
}
|
||||
|
||||
private final String topic;
|
||||
private final String partition;
|
||||
|
||||
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.logging.log4j2.plugins;
|
||||
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.lookup.StrLookup;
|
||||
|
||||
@Plugin(name = "kafka", category = StrLookup.CATEGORY)
|
||||
public class KafkaLookup implements StrLookup {
|
||||
|
||||
@Override
|
||||
public String lookup(String key) {
|
||||
return getFromKafka(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookup(LogEvent event, String key) {
|
||||
return getFromKafka(key);
|
||||
}
|
||||
|
||||
private String getFromKafka(String topicName) {
|
||||
//kafka search logic should go here
|
||||
return "topic1-p1";
|
||||
}
|
||||
}
|
|
@ -50,6 +50,12 @@
|
|||
</Policies>
|
||||
</RollingFile>
|
||||
<MapAppender name="MapAppender"/>
|
||||
<Console name="DockerConsoleLogger" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%pid %docker" />
|
||||
</Console>
|
||||
<Kafka2 name="KafkaLogger" ip ="127.0.0.1" port="9010" topic="log" partition="p-1">
|
||||
<PatternLayout pattern="%pid%style{%message}{red}%n" />
|
||||
</Kafka2>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||
|
@ -80,6 +86,9 @@
|
|||
additivity="false">
|
||||
<AppenderRef ref="ConsoleJSONAppender" />
|
||||
</Logger>
|
||||
<Logger name="com.baeldung.logging.log4j2.plugins" level="INFO">
|
||||
<AppenderRef ref="KafkaLogger" />
|
||||
</Logger>
|
||||
<Root level="DEBUG">
|
||||
<AppenderRef ref="ConsoleAppender" />
|
||||
<AppenderRef ref="MapAppender" />
|
||||
|
|
Loading…
Reference in New Issue