Class SocketAppender
- java.lang.Object
-
- org.apache.log4j.AppenderSkeleton
-
- org.apache.log4j.net.SocketAppender
-
- All Implemented Interfaces:
Appender
,OptionHandler
public class SocketAppender extends AppenderSkeleton
SendsLoggingEvent
objects to a remote a log server, usually aSocketNode
.The SocketAppender has the following properties:
- If sent to a
SocketNode
, remote logging is non-intrusive as far as the log event is concerned. In other words, the event will be logged with the same time stamp,NDC
, location info as if it were logged locally by the client. - SocketAppenders do not use a layout. They ship a
serialized
LoggingEvent
object to the server side. - Remote logging uses the TCP protocol. Consequently, if the server is reachable, then log events will eventually arrive at the server.
- If the remote server is down, the logging requests are simply dropped. However, if and when the server comes back up, then event transmission is resumed transparently. This transparent reconneciton is performed by a connector thread which periodically attempts to connect to the server.
- Logging events are automatically buffered by the
native TCP implementation. This means that if the link to server
is slow but still faster than the rate of (log) event production
by the client, the client will not be affected by the slow
network connection. However, if the network connection is slower
then the rate of event production, then the client can only
progress at the network rate. In particular, if the network link
to the the server is down, the client will be blocked.
On the other hand, if the network link is up, but the server is down, the client will not be blocked when making log requests but the log events will be lost due to server unavailability.
- Even if a
SocketAppender
is no longer attached to any category, it will not be garbage collected in the presence of a connector thread. A connector thread exists only if the connection to the server is down. To avoid this garbage collection problem, you shouldclose()
the theSocketAppender
explicitly. See also next item.Long lived applications which create/destroy many
SocketAppender
instances should be aware of this garbage collection problem. Most other applications can safely ignore it. - If the JVM hosting the
SocketAppender
exits before theSocketAppender
is closed either explicitly or subsequent to garbage collection, then there might be untransmitted data in the pipe which might be lost. This is a common problem on Windows based systems.To avoid lost data, it is usually sufficient to
close()
theSocketAppender
either explicitly or by calling theLogManager.shutdown()
method before exiting the application.
- Since:
- 0.8.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
SocketAppender.Connector
The Connector will reconnect when the server becomes available again.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.net.InetAddress
address
private boolean
advertiseViaMulticastDNS
private java.lang.String
application
private SocketAppender.Connector
connector
(package private) int
counter
static int
DEFAULT_PORT
The default port number of remote logging server (4560).(package private) static int
DEFAULT_RECONNECTION_DELAY
The default reconnection delay (30000 milliseconds or 30 seconds).(package private) boolean
locationInfo
(package private) java.io.ObjectOutputStream
oos
(package private) int
port
(package private) int
reconnectionDelay
(package private) java.lang.String
remoteHost
We remember host name as String in addition to the resolved InetAddress so that it can be returned via getOption().private static int
RESET_FREQUENCY
private ZeroConfSupport
zeroConf
static java.lang.String
ZONE
The MulticastDNS zone advertised by a SocketAppender-
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
-
-
Constructor Summary
Constructors Constructor Description SocketAppender()
SocketAppender(java.lang.String host, int port)
Connects to remote server athost
andport
.SocketAppender(java.net.InetAddress address, int port)
Connects to remote server ataddress
andport
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateOptions()
Connect to the specified RemoteHost and Port.void
append(LoggingEvent event)
Subclasses ofAppenderSkeleton
should implement this method to perform actual logging.void
cleanUp()
Drop the connection to the remote host and release the underlying connector thread if it has been createdvoid
close()
Close this appender.(package private) void
connect(java.net.InetAddress address, int port)
(package private) void
fireConnector()
(package private) static java.net.InetAddress
getAddressByName(java.lang.String host)
java.lang.String
getApplication()
Returns value of the Application option.boolean
getLocationInfo()
Returns value of the LocationInfo option.int
getPort()
Returns value of the Port option.int
getReconnectionDelay()
Returns value of the ReconnectionDelay option.java.lang.String
getRemoteHost()
Returns value of the RemoteHost option.boolean
isAdvertiseViaMulticastDNS()
boolean
requiresLayout()
The SocketAppender does not use a layout.void
setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS)
void
setApplication(java.lang.String lapp)
The App option takes a string value which should be the name of the application getting logged.void
setLocationInfo(boolean locationInfo)
The LocationInfo option takes a boolean value.void
setPort(int port)
The Port option takes a positive integer representing the port where the server is waiting for connections.void
setReconnectionDelay(int delay)
The ReconnectionDelay option takes a positive integer representing the number of milliseconds to wait between each failed connection attempt to the server.void
setRemoteHost(java.lang.String host)
The RemoteHost option takes a string value which should be the host name of the server where aSocketNode
is running.-
Methods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
-
-
-
-
Field Detail
-
DEFAULT_PORT
public static final int DEFAULT_PORT
The default port number of remote logging server (4560).- Since:
- 1.2.15
- See Also:
- Constant Field Values
-
DEFAULT_RECONNECTION_DELAY
static final int DEFAULT_RECONNECTION_DELAY
The default reconnection delay (30000 milliseconds or 30 seconds).- See Also:
- Constant Field Values
-
remoteHost
java.lang.String remoteHost
We remember host name as String in addition to the resolved InetAddress so that it can be returned via getOption().
-
ZONE
public static final java.lang.String ZONE
The MulticastDNS zone advertised by a SocketAppender- See Also:
- Constant Field Values
-
address
java.net.InetAddress address
-
port
int port
-
oos
java.io.ObjectOutputStream oos
-
reconnectionDelay
int reconnectionDelay
-
locationInfo
boolean locationInfo
-
application
private java.lang.String application
-
connector
private SocketAppender.Connector connector
-
counter
int counter
-
RESET_FREQUENCY
private static final int RESET_FREQUENCY
- See Also:
- Constant Field Values
-
advertiseViaMulticastDNS
private boolean advertiseViaMulticastDNS
-
zeroConf
private ZeroConfSupport zeroConf
-
-
Method Detail
-
activateOptions
public void activateOptions()
Connect to the specified RemoteHost and Port.- Specified by:
activateOptions
in interfaceOptionHandler
- Overrides:
activateOptions
in classAppenderSkeleton
-
close
public void close()
Close this appender.This will mark the appender as closed and call then
cleanUp()
method.
-
cleanUp
public void cleanUp()
Drop the connection to the remote host and release the underlying connector thread if it has been created
-
connect
void connect(java.net.InetAddress address, int port)
-
append
public void append(LoggingEvent event)
Description copied from class:AppenderSkeleton
Subclasses ofAppenderSkeleton
should implement this method to perform actual logging. See alsoAppenderSkeleton.doAppend
method.- Specified by:
append
in classAppenderSkeleton
-
setAdvertiseViaMulticastDNS
public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS)
-
isAdvertiseViaMulticastDNS
public boolean isAdvertiseViaMulticastDNS()
-
fireConnector
void fireConnector()
-
getAddressByName
static java.net.InetAddress getAddressByName(java.lang.String host)
-
requiresLayout
public boolean requiresLayout()
The SocketAppender does not use a layout. Hence, this method returnsfalse
.
-
setRemoteHost
public void setRemoteHost(java.lang.String host)
The RemoteHost option takes a string value which should be the host name of the server where aSocketNode
is running.
-
getRemoteHost
public java.lang.String getRemoteHost()
Returns value of the RemoteHost option.
-
setPort
public void setPort(int port)
The Port option takes a positive integer representing the port where the server is waiting for connections.
-
getPort
public int getPort()
Returns value of the Port option.
-
setLocationInfo
public void setLocationInfo(boolean locationInfo)
The LocationInfo option takes a boolean value. If true, the information sent to the remote host will include location information. By default no location information is sent to the server.
-
getLocationInfo
public boolean getLocationInfo()
Returns value of the LocationInfo option.
-
setApplication
public void setApplication(java.lang.String lapp)
The App option takes a string value which should be the name of the application getting logged. If property was already set (via system property), don't set here.- Since:
- 1.2.15
-
getApplication
public java.lang.String getApplication()
Returns value of the Application option.- Since:
- 1.2.15
-
setReconnectionDelay
public void setReconnectionDelay(int delay)
The ReconnectionDelay option takes a positive integer representing the number of milliseconds to wait between each failed connection attempt to the server. The default value of this option is 30000 which corresponds to 30 seconds.Setting this option to zero turns off reconnection capability.
-
getReconnectionDelay
public int getReconnectionDelay()
Returns value of the ReconnectionDelay option.
-
-