Skip to content

Commit

Permalink
Layout is replaced with Encoder
Browse files Browse the repository at this point in the history
Update:
- Layout is deprecated and encoder is introduced when socket is used.
- Logback configuration file is updated with encoder settings.
  • Loading branch information
bparmar-splunk committed Sep 1, 2022
1 parent 8221dd3 commit 5675cc0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/main/java/com/splunk/logging/TcpAppender.java
Expand Up @@ -18,6 +18,8 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.net.DefaultSocketConnector;
import ch.qos.logback.core.net.SocketConnector;
import ch.qos.logback.core.util.CloseUtil;
Expand Down Expand Up @@ -52,7 +54,10 @@ public class TcpAppender extends AppenderBase<ILoggingEvent> implements Runnable
private int port;
private InetAddress address;

private Layout<ILoggingEvent> layout;
/**
* Layout is replaced with Encoder because Logback has deprecated the use of Layout directly to format message.
*/
private Encoder<ILoggingEvent> encoder;
private ExecutorService executor;
private Future<Socket> connectorTask;

Expand Down Expand Up @@ -122,7 +127,7 @@ public void run() {

while (true) {
ILoggingEvent event = queue.take();
String formatted = layout.doLayout(event);
String formatted = new String(encoder.encode(event));
writer.write(formatted);
writer.flush();
}
Expand Down Expand Up @@ -219,8 +224,8 @@ public void start() {
addError("Queue size must be non-negative");
}

if (this.layout == null) {
addError("No layout set for the appender named [" + name + "].");
if (this.encoder == null) {
addError("No encoder set for the appender named [" + name + "].");
errorPresent = true;
}

Expand Down Expand Up @@ -311,8 +316,21 @@ protected void append(ILoggingEvent event) {
public void setQueueSize(int queueSize) { this.queueSize = queueSize; }
public int getQueueSize() { return this.queueSize; }

public void setLayout(Layout<ILoggingEvent> layout) { this.layout = layout; }
public Layout<ILoggingEvent> getLayout() { return this.layout; }
public void setLayout(Layout<ILoggingEvent> layout) {
this.addWarn("This appender no longer admits a layout as a sub-component, set an encoder instead.");
this.addWarn("To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.");
this.addWarn("See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details");

LayoutWrappingEncoder<ILoggingEvent> layoutWrappingEncoder = new LayoutWrappingEncoder();
layoutWrappingEncoder.setLayout(layout);
layoutWrappingEncoder.setContext(this.context);
this.encoder = layoutWrappingEncoder;
}
public Encoder<ILoggingEvent> getEncoder() { return this.encoder; }

public void setEncoder(Encoder<ILoggingEvent> encoder) {
this.encoder = encoder;
}

/**
* The <b>eventDelayLimit</b> takes a non-negative integer representing the
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/logback.xml
Expand Up @@ -39,9 +39,9 @@ under the License.
<appender name="socket" class="com.splunk.logging.TcpAppender">
<RemoteHost>127.0.0.1</RemoteHost>
<Port>15000</Port>
<layout class="ch.qos.logback.classic.PatternLayout">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%thread %level: %msg%n</pattern>
</layout>
</encoder>
</appender>

<logger name="splunk.logger" additivity="false" level="INFO">
Expand Down

0 comments on commit 5675cc0

Please sign in to comment.