io.dropwizard.logging.TcpSocketAppenderFactory Maven / Gradle / Ivy
package io.dropwizard.logging;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.spi.DeferredProcessingAware;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.socket.DropwizardSocketAppender;
import io.dropwizard.util.Duration;
import io.dropwizard.util.DataSize;
import io.dropwizard.validation.MinDataSize;
import io.dropwizard.validation.PortRange;
import javax.validation.constraints.NotEmpty;
import javax.net.SocketFactory;
import javax.validation.constraints.NotNull;
/**
* An {@link AppenderFactory} implementation which provides an appender that writes events to a TCP socket.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* {@code host}
* {@code localhost}
* The hostname of the TCP server.
*
*
* {@code port}
* {@code 4560}
* The port on which the TCP server is listening.
*
*
* {@code connectionTimeout}
* {@code 500 ms}
* The timeout to connect to the TCP server.
*
*
* {@code immediateFlush}
* {@code true}
* If set to true, log events will be immediately send to the server. Immediate flushing is safer, but it
* degrades logging throughput.
*
*
* {@code sendBufferSize}
* 8KiB
* The buffer size of the underlying SocketAppender. Takes into effect if immediateFlush is disabled.
*
*
*/
@JsonTypeName("tcp")
public class TcpSocketAppenderFactory extends AbstractOutputStreamAppenderFactory {
@NotEmpty
private String host = "localhost";
@PortRange
private int port = 4560;
@NotNull
private Duration connectionTimeout = Duration.milliseconds(500);
private boolean immediateFlush = true;
@MinDataSize(1)
private DataSize sendBufferSize = DataSize.kibibytes(8);
@JsonProperty
public String getHost() {
return host;
}
@JsonProperty
public void setHost(String host) {
this.host = host;
}
@JsonProperty
public int getPort() {
return port;
}
@JsonProperty
public void setPort(int port) {
this.port = port;
}
@JsonProperty
public Duration getConnectionTimeout() {
return connectionTimeout;
}
@JsonProperty
public void setConnectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
@JsonProperty
public boolean isImmediateFlush() {
return immediateFlush;
}
@JsonProperty
public void setImmediateFlush(boolean immediateFlush) {
this.immediateFlush = immediateFlush;
}
@JsonProperty
public DataSize getSendBufferSize() {
return sendBufferSize;
}
@JsonProperty
public void setSendBufferSize(DataSize sendBufferSize) {
this.sendBufferSize = sendBufferSize;
}
@Override
protected OutputStreamAppender appender(LoggerContext context) {
final OutputStreamAppender appender = new DropwizardSocketAppender<>(host, port,
(int) connectionTimeout.toMilliseconds(), (int) sendBufferSize.toBytes(), socketFactory());
appender.setContext(context);
appender.setName("tcp-socket-appender");
appender.setImmediateFlush(immediateFlush);
return appender;
}
protected SocketFactory socketFactory() {
return SocketFactory.getDefault();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy