Package ch.qos.logback.core.net
Class AbstractSocketAppender<E>
- java.lang.Object
-
- ch.qos.logback.core.spi.ContextAwareBase
-
- ch.qos.logback.core.AppenderBase<E>
-
- ch.qos.logback.core.net.AbstractSocketAppender<E>
-
- All Implemented Interfaces:
Appender<E>
,SocketConnector.ExceptionHandler
,ContextAware
,FilterAttachable<E>
,LifeCycle
- Direct Known Subclasses:
AbstractSSLSocketAppender
,SocketAppender
,SocketAppender
public abstract class AbstractSocketAppender<E> extends AppenderBase<E> implements SocketConnector.ExceptionHandler
An abstract base for module specificSocketAppender
implementations in other logback modules.
-
-
Field Summary
Fields Modifier and Type Field Description private int
acceptConnectionTimeout
private java.net.InetAddress
address
private SocketConnector
connector
private static int
DEFAULT_ACCEPT_CONNECTION_DELAY
Default timeout when waiting for the remote server to accept our connection.private static int
DEFAULT_EVENT_DELAY_TIMEOUT
Default timeout for how long to wait when inserting an event into the BlockingQueue.static int
DEFAULT_PORT
The default port number of remote logging server (4560).static int
DEFAULT_QUEUE_SIZE
Default size of the deque used to hold logging events that are destined for the remote peer.static int
DEFAULT_RECONNECTION_DELAY
The default reconnection delay (30000 milliseconds or 30 seconds).private java.util.concurrent.BlockingDeque<E>
deque
private Duration
eventDelayLimit
private ObjectWriterFactory
objectWriterFactory
private java.lang.String
peerId
private int
port
private QueueFactory
queueFactory
private int
queueSize
private Duration
reconnectionDelay
private java.lang.String
remoteHost
private java.net.Socket
socket
private java.util.concurrent.Future<?>
task
-
Fields inherited from class ch.qos.logback.core.AppenderBase
name, started
-
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractSocketAppender()
Constructs a new appender.(package private)
AbstractSocketAppender(QueueFactory queueFactory, ObjectWriterFactory objectWriterFactory)
Constructs a new appender using the givenQueueFactory
andObjectWriterFactory
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
append(E event)
void
connectionFailed(SocketConnector connector, java.lang.Exception ex)
private void
connectSocketAndDispatchEvents()
private SocketConnector
createConnector(java.net.InetAddress address, int port, int initialDelay, long retryDelay)
private ObjectWriter
createObjectWriterForSocket()
private void
dispatchEvents(ObjectWriter objectWriter)
Duration
getEventDelayLimit()
Returns the value of the eventDelayLimit property.int
getPort()
Returns value of the Port property.protected abstract PreSerializationTransformer<E>
getPST()
Get the pre-serialization transformer that will be used to transform each event into a Serializable object before delivery to the remote receiver.int
getQueueSize()
Returns the value of the queueSize property.Duration
getReconnectionDelay()
Returns value of the reconnectionDelay property.java.lang.String
getRemoteHost()
Returns value of the RemoteHost property.protected javax.net.SocketFactory
getSocketFactory()
Gets the defaultSocketFactory
for the platform.protected SocketConnector
newConnector(java.net.InetAddress address, int port, long initialDelay, long retryDelay)
Creates a newSocketConnector
.protected abstract void
postProcessEvent(E event)
Post-processes an event before it is serialized for delivery to the remote receiver.(package private) void
setAcceptConnectionTimeout(int acceptConnectionTimeout)
Sets the timeout that controls how long we'll wait for the remote peer to accept our connection attempt.void
setEventDelayLimit(Duration eventDelayLimit)
The eventDelayLimit takes a non-negative integer representing the number of milliseconds to allow the appender to block if the underlying BlockingQueue is full.void
setPort(int port)
The Port property takes a positive integer representing the port where the server is waiting for connections.void
setQueueSize(int queueSize)
The queueSize property takes a non-negative integer representing the number of logging events to retain for delivery to the remote receiver.void
setReconnectionDelay(Duration delay)
The reconnectionDelay property takes a positiveDuration
value representing the time to wait between each failed connection attempt to the server.void
setRemoteHost(java.lang.String host)
The RemoteHost property takes the name of of the host where a corresponding server is running.private boolean
socketConnectionCouldBeEstablished()
void
start()
void
stop()
private void
tryReAddingEventToFrontOfQueue(E event)
-
Methods inherited from class ch.qos.logback.core.AppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, toString
-
Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
-
-
-
Field Detail
-
DEFAULT_PORT
public static final int DEFAULT_PORT
The default port number of remote logging server (4560).- See Also:
- Constant Field Values
-
DEFAULT_RECONNECTION_DELAY
public static final int DEFAULT_RECONNECTION_DELAY
The default reconnection delay (30000 milliseconds or 30 seconds).- See Also:
- Constant Field Values
-
DEFAULT_QUEUE_SIZE
public static final int DEFAULT_QUEUE_SIZE
Default size of the deque used to hold logging events that are destined for the remote peer.- See Also:
- Constant Field Values
-
DEFAULT_ACCEPT_CONNECTION_DELAY
private static final int DEFAULT_ACCEPT_CONNECTION_DELAY
Default timeout when waiting for the remote server to accept our connection.- See Also:
- Constant Field Values
-
DEFAULT_EVENT_DELAY_TIMEOUT
private static final int DEFAULT_EVENT_DELAY_TIMEOUT
Default timeout for how long to wait when inserting an event into the BlockingQueue.- See Also:
- Constant Field Values
-
objectWriterFactory
private final ObjectWriterFactory objectWriterFactory
-
queueFactory
private final QueueFactory queueFactory
-
remoteHost
private java.lang.String remoteHost
-
port
private int port
-
address
private java.net.InetAddress address
-
reconnectionDelay
private Duration reconnectionDelay
-
queueSize
private int queueSize
-
acceptConnectionTimeout
private int acceptConnectionTimeout
-
eventDelayLimit
private Duration eventDelayLimit
-
deque
private java.util.concurrent.BlockingDeque<E> deque
-
peerId
private java.lang.String peerId
-
connector
private SocketConnector connector
-
task
private java.util.concurrent.Future<?> task
-
socket
private volatile java.net.Socket socket
-
-
Constructor Detail
-
AbstractSocketAppender
protected AbstractSocketAppender()
Constructs a new appender.
-
AbstractSocketAppender
AbstractSocketAppender(QueueFactory queueFactory, ObjectWriterFactory objectWriterFactory)
Constructs a new appender using the givenQueueFactory
andObjectWriterFactory
.
-
-
Method Detail
-
start
public void start()
- Specified by:
start
in interfaceLifeCycle
- Overrides:
start
in classAppenderBase<E>
-
stop
public void stop()
- Specified by:
stop
in interfaceLifeCycle
- Overrides:
stop
in classAppenderBase<E>
-
append
protected void append(E event)
- Specified by:
append
in classAppenderBase<E>
-
connectSocketAndDispatchEvents
private void connectSocketAndDispatchEvents()
-
socketConnectionCouldBeEstablished
private boolean socketConnectionCouldBeEstablished() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
createObjectWriterForSocket
private ObjectWriter createObjectWriterForSocket() throws java.io.IOException
- Throws:
java.io.IOException
-
createConnector
private SocketConnector createConnector(java.net.InetAddress address, int port, int initialDelay, long retryDelay)
-
dispatchEvents
private void dispatchEvents(ObjectWriter objectWriter) throws java.lang.InterruptedException, java.io.IOException
- Throws:
java.lang.InterruptedException
java.io.IOException
-
tryReAddingEventToFrontOfQueue
private void tryReAddingEventToFrontOfQueue(E event)
-
connectionFailed
public void connectionFailed(SocketConnector connector, java.lang.Exception ex)
- Specified by:
connectionFailed
in interfaceSocketConnector.ExceptionHandler
-
newConnector
protected SocketConnector newConnector(java.net.InetAddress address, int port, long initialDelay, long retryDelay)
Creates a newSocketConnector
.The default implementation creates an instance of
DefaultSocketConnector
. A subclass may override to provide a differentSocketConnector
implementation.- Parameters:
address
- target remote addressport
- target remote portinitialDelay
- delay before the first connection attemptretryDelay
- delay before a reconnection attempt- Returns:
- socket connector
-
getSocketFactory
protected javax.net.SocketFactory getSocketFactory()
Gets the defaultSocketFactory
for the platform.Subclasses may override to provide a custom socket factory.
-
postProcessEvent
protected abstract void postProcessEvent(E event)
Post-processes an event before it is serialized for delivery to the remote receiver.- Parameters:
event
- the event to post-process
-
getPST
protected abstract PreSerializationTransformer<E> getPST()
Get the pre-serialization transformer that will be used to transform each event into a Serializable object before delivery to the remote receiver.- Returns:
- transformer object
-
setRemoteHost
public void setRemoteHost(java.lang.String host)
The RemoteHost property takes the name of of the host where a corresponding server is running.
-
getRemoteHost
public java.lang.String getRemoteHost()
Returns value of the RemoteHost property.
-
setPort
public void setPort(int port)
The Port property takes a positive integer representing the port where the server is waiting for connections.
-
getPort
public int getPort()
Returns value of the Port property.
-
setReconnectionDelay
public void setReconnectionDelay(Duration delay)
The reconnectionDelay property takes a positiveDuration
value representing the time to wait between each failed connection attempt to the server. The default value of this option is to 30 seconds.Setting this option to zero turns off reconnection capability.
-
getReconnectionDelay
public Duration getReconnectionDelay()
Returns value of the reconnectionDelay property.
-
setQueueSize
public void setQueueSize(int queueSize)
The queueSize property takes a non-negative integer representing the number of logging events to retain for delivery to the remote receiver. When the deque size is zero, event delivery to the remote receiver is synchronous. When the deque size is greater than zero, theappend(Object)
method returns immediately after enqueing the event, assuming that there is space available in the deque. Using a non-zero deque length can improve performance by eliminating delays caused by transient network delays.- Parameters:
queueSize
- the deque size to set.
-
getQueueSize
public int getQueueSize()
Returns the value of the queueSize property.
-
setEventDelayLimit
public void setEventDelayLimit(Duration eventDelayLimit)
The eventDelayLimit takes a non-negative integer representing the number of milliseconds to allow the appender to block if the underlying BlockingQueue is full. Once this limit is reached, the event is dropped.- Parameters:
eventDelayLimit
- the event delay limit
-
getEventDelayLimit
public Duration getEventDelayLimit()
Returns the value of the eventDelayLimit property.
-
setAcceptConnectionTimeout
void setAcceptConnectionTimeout(int acceptConnectionTimeout)
Sets the timeout that controls how long we'll wait for the remote peer to accept our connection attempt.This property is configurable primarily to support instrumentation for unit testing.
- Parameters:
acceptConnectionTimeout
- timeout value in milliseconds
-
-