Class UDPAppender
java.lang.Object
org.apache.log4j.AppenderSkeleton
org.apache.log4j.receivers.net.UDPAppender
- All Implemented Interfaces:
org.apache.log4j.Appender, NetworkBased, PortBased, org.apache.log4j.spi.OptionHandler
Sends log information as a UDP datagrams.
The UDPAppender is meant to be used as a diagnostic logging tool so that logging can be monitored by a simple UDP client.
Messages are not sent as LoggingEvent objects but as text after applying the designated Layout.
The port and remoteHost properties can be set in configuration properties. By setting the remoteHost to a broadcast address any number of clients can listen for log messages.
This was inspired and really extended/copied from SocketAppender.
Please see the docs for the proper credit to the authors of that class.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) InetAddressprivate boolean(package private) Stringstatic final intThe default port number for the UDP packets, 9991.(package private) String(package private) StringWe remember host name as String in addition to the resolved InetAddress so that it can be returned via getOption().(package private) boolean(package private) DatagramSocket(package private) int(package private) Stringprivate org.apache.log4j.net.ZeroConfSupportstatic final StringThe MulticastDNS zone advertised by a UDPAppenderFields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold -
Constructor Summary
ConstructorsConstructorDescriptionUDPAppender(String host, int port) Sends UDP packets to theaddressandport.UDPAppender(InetAddress address, int port) Sends UDP packets to theaddressandport. -
Method Summary
Modifier and TypeMethodDescriptionvoidOpen the UDP sender for the RemoteHost and Port.voidappend(org.apache.log4j.spi.LoggingEvent event) voidcleanUp()Close the UDP Socket and release the underlying connector thread if it has been createdvoidclose()Close this appender.(package private) voidconnect(InetAddress address, int port) (package private) InetAddressgetAddressByName(String host) Returns value of the App option.Returns value of the Encoding option.intgetPort()Returns value of the Port option.Returns value of the RemoteHost option.booleanisActive()Get if item is active.booleanbooleanThe UDPAppender uses layouts.voidsetAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) voidsetApplication(String app) The App option takes a string value which should be the name of the application getting logged.voidsetEncoding(String encoding) The Encoding option specifies how the bytes are encoded.voidsetPort(int port) The Port option takes a positive integer representing the port where UDP packets will be sent.voidsetRemoteHost(String host) The RemoteHost option takes a string value which should be the host name or ipaddress to send the UDP packets.Methods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThresholdMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface NetworkBased
getName
-
Field Details
-
DEFAULT_PORT
public static final int DEFAULT_PORTThe default port number for the UDP packets, 9991.- See Also:
-
hostname
String hostnameWe remember host name as String in addition to the resolved InetAddress so that it can be returned via getOption(). -
remoteHost
String remoteHost -
application
String application -
encoding
String encoding -
address
InetAddress address -
port
int port -
outSocket
DatagramSocket outSocket -
ZONE
-
inError
boolean inError -
advertiseViaMulticastDNS
private boolean advertiseViaMulticastDNS -
zeroConf
private org.apache.log4j.net.ZeroConfSupport zeroConf
-
-
Constructor Details
-
UDPAppender
public UDPAppender() -
UDPAppender
Sends UDP packets to theaddressandport. -
UDPAppender
Sends UDP packets to theaddressandport.
-
-
Method Details
-
activateOptions
public void activateOptions()Open the UDP sender for the RemoteHost and Port.- Specified by:
activateOptionsin interfaceorg.apache.log4j.spi.OptionHandler- Overrides:
activateOptionsin classorg.apache.log4j.AppenderSkeleton
-
close
public void close()Close this appender.This will mark the appender as closed and call then
cleanUp()method.- Specified by:
closein interfaceorg.apache.log4j.Appender
-
cleanUp
public void cleanUp()Close the UDP Socket and release the underlying connector thread if it has been created -
connect
-
append
public void append(org.apache.log4j.spi.LoggingEvent event) - Specified by:
appendin classorg.apache.log4j.AppenderSkeleton
-
isActive
public boolean isActive()Description copied from interface:NetworkBasedGet if item is active.- Specified by:
isActivein interfaceNetworkBased- Returns:
- if true, item is active.
-
getAddressByName
-
requiresLayout
public boolean requiresLayout()The UDPAppender uses layouts. Hence, this method returnstrue.- Specified by:
requiresLayoutin interfaceorg.apache.log4j.Appender
-
setRemoteHost
The RemoteHost option takes a string value which should be the host name or ipaddress to send the UDP packets. -
getRemoteHost
Returns value of the RemoteHost option. -
setApplication
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. -
getApplication
Returns value of the App option. -
setEncoding
The Encoding option specifies how the bytes are encoded. If this option is not specified, the System encoding is used. -
getEncoding
Returns value of the Encoding option. -
setPort
public void setPort(int port) The Port option takes a positive integer representing the port where UDP packets will be sent. -
getPort
-
setAdvertiseViaMulticastDNS
public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) -
isAdvertiseViaMulticastDNS
public boolean isAdvertiseViaMulticastDNS()
-