- java.lang.Object
-
- java.util.logging.Handler
-
- org.jboss.logmanager.ExtHandler
-
- org.jboss.logmanager.handlers.SyslogHandler
-
- All Implemented Interfaces:
Flushable,AutoCloseable
public class SyslogHandler extends ExtHandler
A syslog handler for logging to syslogd. This handler can write to syslog servers that accept the RFC3164 and RFC5424 formats. Writes can be done via TCP, SSL over TCP or UDP protocols. You can also override theoutput streamif a custom protocol is needed.Configuration Properties: Property Description Type Default serverHostname The address of the syslog server Stringlocalhost port The port of the syslog server int 514 facility The facility used to calculate the priority of the log message FacilityUSER_LEVELappName The name of the application that is logging Stringjava hostname The name of the host the messages are being sent from. See setHostname(String)for more detailsStringnullsyslogType The type of the syslog used to format the message SyslogTypeRFC5424protocol The protocol to send the message over ProtocolUDPdelimiter The delimiter to use at the end of the message if useDelimiteris set totrueStringFor UDPnull- ForTCPorSSL_TCP\nuseDelimiter Whether or not the message should be appended with a delimiterbooleanFor UDPfalse- ForTCPorSSL_TCPtrueuseCountingFraming Prefixes the size of the message, mainly used for TCPorSSL_TCP, connections to the message being sent to the syslog server. See http://tools.ietf.org/html/rfc6587 for more details on framing types.booleanfalsetruncate Whether or not a message, including the header, should truncate the message if the length in bytes is greater than the maximum length. If set tofalsemessages will be split and sent with the same header values.booleantruemaxLength The maximum length a log message, including the header, is allowed to be. intFor RFC31641024 (1k) - ForRFC54242048 (2k)- Author:
- James R. Perkins
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSyslogHandler.FacilityFacility as defined by RFC-5424 (http://tools.ietf.org/html/rfc5424) and RFC-3164 (http://tools.ietf.org/html/rfc3164).static classSyslogHandler.ProtocolThe type of socket the syslog should write tostatic classSyslogHandler.SeveritySeverity as defined by RFC-5424 (http://tools.ietf.org/html/rfc5424) and RFC-3164 (http://tools.ietf.org/html/rfc3164).static classSyslogHandler.SyslogTypeThe syslog type used for formatting the message.
-
Field Summary
Fields Modifier and Type Field Description static InetAddressDEFAULT_ADDRESSstatic StringDEFAULT_ENCODINGstatic SyslogHandler.FacilityDEFAULT_FACILITYstatic intDEFAULT_PORTstatic intDEFAULT_SECURE_PORTstatic StringNILVALUE_SP-
Fields inherited from class org.jboss.logmanager.ExtHandler
handlers, handlersUpdater, lock
-
-
Constructor Summary
Constructors Constructor Description SyslogHandler()The default class constructor.SyslogHandler(String serverHostname, int port)Creates a new syslog handler that sends the messages to the server represented by theserverHostnameparameter on the port represented by theportparameter.SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, SyslogHandler.Protocol protocol, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(InetAddress serverAddress, int port)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, SyslogHandler.Protocol protocol, String hostname)Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected intcalculatePriority(Level level, SyslogHandler.Facility facility)voidclose()Close all child handlers.protected byte[]createRFC3164Header(ExtLogRecord record)protected byte[]createRFC5424Header(ExtLogRecord record)voiddoPublish(ExtLogRecord record)Do the actual work of publication; the record will have been filtered already.voidflush()Flush all child handlers.StringgetAppName()Gets app name used when formatting the message in RFC5424 format.SyslogHandler.FacilitygetFacility()Returns the facility used for calculating the priority of the message.StringgetHostname()Returns the host name which is used when sending the message to the syslog.intgetMaxLength()Returns the maximum length, in bytes, of the message allowed to be sent.StringgetMessageDelimiter()Returns the delimiter being used for the message ifuse message delimiteris set totrue.StringgetPid()Returns the pid being used as the PROCID for RFC5424 messages.intgetPort()Returns the port the syslogd is listening on.SyslogHandler.ProtocolgetProtocol()The protocol used to connect to the syslog serverInetAddressgetServerAddress()Returns the server address the messages are being sent to.SyslogHandler.SyslogTypegetSyslogType()Returns thesyslog typethis handler is using to format the message sent.booleanisBlockOnReconnect()booleanisEscapeEnabled()Deprecated.escaping message values is not required per RFC5424 and is no longer supported in this handlerbooleanisTruncate()Checks if the message should truncated if the total length exceeds themaximum length.booleanisUseCountingFraming()Returnstrueif the message size should be prefixed to the message being sent.booleanisUseMessageDelimiter()Checks whether to append the message with a delimiter or not.voidsetAppName(String appName)Sets app name used when formatting the message in RFC5424 format.voidsetBlockOnReconnect(boolean blockOnReconnect)voidsetClientSocketFactory(ClientSocketFactory clientSocketFactory)Sets the client socket factory used to create sockets.voidsetEscapeEnabled(boolean escapeEnabled)Deprecated.escaping message values is not required per RFC5424 and is no longer supported in this handlervoidsetFacility(SyslogHandler.Facility facility)Sets the facility used when calculating the priority of the message.voidsetHostname(String hostname)Sets the host name which is used when sending the message to the syslog.voidsetMaxLength(int maxLen)Sets the maximum length, in bytes, of the message allowed to tbe sent.voidsetMessageDelimiter(String delimiter)Sets the message delimiter to be used ifuse message delimiteris set totrue.voidsetOutputStream(OutputStream out)Sets the output stream for the syslog handler to write to.voidsetPort(int port)Sets the port the syslogd server is listening on.voidsetProtocol(SyslogHandler.Protocol type)Sets the protocol used to connect to the syslog servervoidsetServerAddress(InetAddress serverAddress)Sets the server address the messages should be sent to.voidsetServerHostname(String hostname)Sets the server address the messages should be sent to.voidsetSyslogType(SyslogHandler.SyslogType syslogType)Set thesyslog typethis handler should use to format the message sent.voidsetTruncate(boolean truncate)Set totrueif the message should be truncated if the total length themaximum length.voidsetUseCountingFraming(boolean useCountingFraming)Set totrueif the message being sent should be prefixed with the size of the message.voidsetUseMessageDelimiter(boolean useDelimiter)Whether to append the message with a delimiter or not.-
Methods inherited from class org.jboss.logmanager.ExtHandler
addHandler, checkAccess, checkAccess, clearHandlers, getCharset, getEncoding, getErrorManager, getFilter, getFormatter, getHandlers, getLevel, isAutoFlush, isCallerCalculationRequired, isCloseChildren, isEnabled, publish, publish, publishToNestedHandlers, removeHandler, reportError, reportError, setAutoFlush, setCharset, setCharsetPrivate, setCloseChildren, setEnabled, setEncoding, setErrorManager, setFilter, setFormatter, setHandlers, setLevel
-
Methods inherited from class java.util.logging.Handler
isLoggable
-
-
-
-
Field Detail
-
DEFAULT_ADDRESS
public static final InetAddress DEFAULT_ADDRESS
-
DEFAULT_PORT
public static final int DEFAULT_PORT
- See Also:
- Constant Field Values
-
DEFAULT_SECURE_PORT
public static final int DEFAULT_SECURE_PORT
- See Also:
- Constant Field Values
-
DEFAULT_ENCODING
public static final String DEFAULT_ENCODING
- See Also:
- Constant Field Values
-
DEFAULT_FACILITY
public static final SyslogHandler.Facility DEFAULT_FACILITY
-
NILVALUE_SP
public static final String NILVALUE_SP
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SyslogHandler
public SyslogHandler() throws IOExceptionThe default class constructor.- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(String serverHostname, int port) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverHostnameparameter on the port represented by theportparameter.- Parameters:
serverHostname- the server to send the messages toport- the port the syslogd is listening on- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(InetAddress serverAddress, int port) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverAddress- the server to send the messages toport- the port the syslogd is listening on- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverHostname- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating priorityhostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverAddress- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating priorityhostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverHostname- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating prioritysyslogType- the type of the syslog used to format the messagehostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverAddress- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating prioritysyslogType- the type of the syslog used to format the messagehostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(String serverHostname, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, SyslogHandler.Protocol protocol, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverHostname- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating prioritysyslogType- the type of the syslog used to format the messageprotocol- the socket type used to the connect to the syslog serverhostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
SyslogHandler
public SyslogHandler(InetAddress serverAddress, int port, SyslogHandler.Facility facility, SyslogHandler.SyslogType syslogType, SyslogHandler.Protocol protocol, String hostname) throws IOException
Creates a new syslog handler that sends the messages to the server represented by theserverAddressparameter on the port represented by theportparameter.- Parameters:
serverAddress- the server to send the messages toport- the port the syslogd is listening onfacility- the facility to use when calculating prioritysyslogType- the type of the syslog used to format the messageprotocol- the socket type used to the connect to the syslog serverhostname- the name of the host the messages are being sent from seesetHostname(String)for details on the hostname- Throws:
IOException- if an error occurs creating the UDP socket
-
-
Method Detail
-
doPublish
public final void doPublish(ExtLogRecord record)
Description copied from class:ExtHandlerDo the actual work of publication; the record will have been filtered already. The default implementation does nothing except to flush if theautoFlushproperty is set totrue; if this behavior is to be preserved in a subclass then this method should be called after the record is physically written.- Overrides:
doPublishin classExtHandler- Parameters:
record- the log record to publish
-
close
public void close()
Description copied from class:ExtHandlerClose all child handlers.- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classExtHandler
-
flush
public void flush()
Description copied from class:ExtHandlerFlush all child handlers.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classExtHandler
-
getAppName
public String getAppName()
Gets app name used when formatting the message in RFC5424 format. By default the app name is "java".- Returns:
- the app name being used
-
setAppName
public void setAppName(String appName)
Sets app name used when formatting the message in RFC5424 format. By default the app name is "java". If set tonulltheExtLogRecord.getProcessName()will be used.- Parameters:
appName- the app name to use- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
isBlockOnReconnect
public boolean isBlockOnReconnect()
- Returns:
trueif blocking is enabled, otherwisefalse
-
setBlockOnReconnect
public void setBlockOnReconnect(boolean blockOnReconnect)
Enables or disables blocking when attempting to reconnect aTCPorSSL TCPprotocol. If set totruethepublishmethods will block when attempting to reconnect. This is only advisable to be set totrueif using an asynchronous handler.- Parameters:
blockOnReconnect-trueto block when reconnecting orfalseto reconnect asynchronously discarding any new messages coming in
-
setClientSocketFactory
public void setClientSocketFactory(ClientSocketFactory clientSocketFactory)
Sets the client socket factory used to create sockets.- Parameters:
clientSocketFactory- the client socket factory to use
-
isEscapeEnabled
@Deprecated public boolean isEscapeEnabled()
Deprecated.escaping message values is not required per RFC5424 and is no longer supported in this handlerChecks whether or not characters below decimal 32, traditional US-ASCII control values expectDEL, are being escaped or not.- Returns:
false
-
setEscapeEnabled
@Deprecated public void setEscapeEnabled(boolean escapeEnabled)
Deprecated.escaping message values is not required per RFC5424 and is no longer supported in this handlerNote: This method no longer does anything. Set totrueto escape characters within the message string that are below decimal 32. These values are tradition US-ASCII control values. The values will be replaced in a#xxxformat wherexxxis the octal value of the character being replaced.- Parameters:
escapeEnabled-trueto escape characters,falseto not escape characters
-
getPid
public String getPid()
Returns the pid being used as the PROCID for RFC5424 messages.- Returns:
- the pid or
nullif the pid could not be determined
-
getPort
public int getPort()
Returns the port the syslogd is listening on.- Returns:
- the port
-
setPort
public void setPort(int port)
Sets the port the syslogd server is listening on.- Parameters:
port- the port- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
getFacility
public SyslogHandler.Facility getFacility()
Returns the facility used for calculating the priority of the message.- Returns:
- the facility
-
setFacility
public void setFacility(SyslogHandler.Facility facility)
Sets the facility used when calculating the priority of the message.- Parameters:
facility- the facility- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
getHostname
public String getHostname()
Returns the host name which is used when sending the message to the syslog.- Returns:
- the host name
-
getMaxLength
public int getMaxLength()
Returns the maximum length, in bytes, of the message allowed to be sent. The length includes the header and the message.- Returns:
- the maximum length, in bytes, of the message allowed to be sent
-
setMaxLength
public void setMaxLength(int maxLen)
Sets the maximum length, in bytes, of the message allowed to tbe sent. Note that the message length includes the header and the message itself.- Parameters:
maxLen- the maximum length, in bytes, allowed to be sent to the syslog server
-
getMessageDelimiter
public String getMessageDelimiter()
Returns the delimiter being used for the message ifuse message delimiteris set totrue.- Returns:
- the delimiter being used for the message
-
setMessageDelimiter
public void setMessageDelimiter(String delimiter)
Sets the message delimiter to be used ifuse message delimiteris set totrue.- Parameters:
delimiter- the delimiter to use for the message
-
isUseMessageDelimiter
public boolean isUseMessageDelimiter()
Checks whether to append the message with a delimiter or not.- Returns:
trueto append the message with a delimiter, otherwisefalse
-
setUseMessageDelimiter
public void setUseMessageDelimiter(boolean useDelimiter)
Whether to append the message with a delimiter or not.- Parameters:
useDelimiter-trueto append the message with a delimiter, otherwisefalse
-
setHostname
public void setHostname(String hostname)
Sets the host name which is used when sending the message to the syslog. This should be the name of the host sending the log messages, Note that the name cannot contain any whitespace. The hostname should be the most specific available value first. The order of preference for the contents of the hostname is as follows:- FQDN
- Static IP address
- hostname
- Dynamic IP address
null
- Parameters:
hostname- the host name
-
isUseCountingFraming
public boolean isUseCountingFraming()
Returnstrueif the message size should be prefixed to the message being sent. See http://tools.ietf.org/html/rfc6587 for more details on framing types.- Returns:
- the message transfer type
-
setUseCountingFraming
public void setUseCountingFraming(boolean useCountingFraming)
Set totrueif the message being sent should be prefixed with the size of the message. See http://tools.ietf.org/html/rfc6587 for more details on framing types.- Parameters:
useCountingFraming-trueif the message being sent should be prefixed with the size of the message- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
setServerHostname
public void setServerHostname(String hostname) throws UnknownHostException
Sets the server address the messages should be sent to.- Parameters:
hostname- the hostname used to created the connection- Throws:
UnknownHostException- if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)- See Also:
InetAddress.getByName(String)
-
getServerAddress
public InetAddress getServerAddress()
Returns the server address the messages are being sent to.- Returns:
- the server address
-
setServerAddress
public void setServerAddress(InetAddress serverAddress)
Sets the server address the messages should be sent to.- Parameters:
serverAddress- the server address- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
getSyslogType
public SyslogHandler.SyslogType getSyslogType()
Returns thesyslog typethis handler is using to format the message sent.- Returns:
- the syslog type
-
setSyslogType
public void setSyslogType(SyslogHandler.SyslogType syslogType)
Set thesyslog typethis handler should use to format the message sent.- Parameters:
syslogType- the syslog type- Throws:
SecurityException- if a security manager exists and if the caller does not haveLoggingPermission(control)
-
getProtocol
public SyslogHandler.Protocol getProtocol()
The protocol used to connect to the syslog server- Returns:
- the protocol
-
setProtocol
public void setProtocol(SyslogHandler.Protocol type)
Sets the protocol used to connect to the syslog server- Parameters:
type- the protocol
-
setOutputStream
public void setOutputStream(OutputStream out)
Sets the output stream for the syslog handler to write to. Setting the output stream closes any already established connections or open output streams and will not open any new connections until the output stream is set tonull. Theprotocol, {@link #setServerAddress(InetAddress), server address},server hostnameorporthave no effect when the output stream is set.- Parameters:
out- the output stream to write to
-
isTruncate
public boolean isTruncate()
Checks if the message should truncated if the total length exceeds themaximum length.- Returns:
trueif the message should be truncated if too large, otherwisefalse
-
setTruncate
public void setTruncate(boolean truncate)
Set totrueif the message should be truncated if the total length themaximum length. Set tofalseif the message should be split and sent as multiple messages. The header will remain the same for each message sent. The wrapping is not a word based wrap and could split words between log messages.- Parameters:
truncate-trueto truncate, otherwisefalseto send multiple messages
-
calculatePriority
protected int calculatePriority(Level level, SyslogHandler.Facility facility)
-
createRFC5424Header
protected byte[] createRFC5424Header(ExtLogRecord record) throws IOException
- Throws:
IOException
-
createRFC3164Header
protected byte[] createRFC3164Header(ExtLogRecord record) throws IOException
- Throws:
IOException
-
-