SocketHandlerpublic class SocketHandler extends StreamHandler A handler that writes log messages to a socket connection.
This handler reads the following properties from the log manager to
initialize itself:
- java.util.logging.ConsoleHandler.level specifies the logging level,
defaults to {@code Level.ALL} if this property is not found or has an invalid
value.
- java.util.logging.SocketHandler.filter specifies the name of the filter
class to be associated with this handler, defaults to {@code null} if this
property is not found or has an invalid value.
- java.util.logging.SocketHandler.formatter specifies the name of the
formatter class to be associated with this handler, defaults to
{@code java.util.logging.XMLFormatter} if this property is not found or has
an invalid value.
- java.util.logging.SocketHandler.encoding specifies the encoding this
handler will use to encode log messages, defaults to {@code null} if this
property is not found or has an invalid value.
- java.util.logging.SocketHandler.host specifies the name of the host that
this handler should connect to. There's no default value for this property.
- java.util.logging.SocketHandler.encoding specifies the port number that
this handler should connect to. There's no default value for this property.
This handler buffers the outgoing messages, but flushes each time a log
record has been published.
This class is not thread-safe.
|
Fields Summary |
---|
private static final String | DEFAULT_LEVEL | private static final String | DEFAULT_FORMATTER | private Socket | socket |
Constructors Summary |
---|
public SocketHandler()Constructs a {@code SocketHandler} object using the properties read by
the log manager, including the host name and port number. Default
formatting uses the XMLFormatter class and level is set to ALL.
super(DEFAULT_LEVEL, null, DEFAULT_FORMATTER, null);
initSocket(LogManager.getLogManager().getProperty(
"java.util.logging.SocketHandler.host"), LogManager //$NON-NLS-1$
.getLogManager().getProperty(
"java.util.logging.SocketHandler.port")); //$NON-NLS-1$
| public SocketHandler(String host, int port)Constructs a {@code SocketHandler} object using the specified host name
and port number together with other properties read by the log manager.
Default formatting uses the XMLFormatter class and level is set to ALL.
super(DEFAULT_LEVEL, null, DEFAULT_FORMATTER, null);
initSocket(host, String.valueOf(port));
|
Methods Summary |
---|
public void | close()Closes this handler. The network connection to the host is also closed.
try {
super.close();
if (null != this.socket) {
this.socket.close();
this.socket = null;
}
} catch (Exception e) {
// logging.F=Exception occurred when closing the socket handler.
getErrorManager().error(Messages.getString("logging.F"), e, //$NON-NLS-1$
ErrorManager.CLOSE_FAILURE);
}
| private void | initSocket(java.lang.String host, java.lang.String port)
// check the validity of the host name
if (null == host || "".equals(host)) { //$NON-NLS-1$
// logging.C=Illegal host argument.
throw new IllegalArgumentException(Messages.getString("logging.C")); //$NON-NLS-1$
}
// check the validity of the port number
int p = 0;
try {
p = Integer.parseInt(port);
} catch (NumberFormatException e) {
// logging.D=Illegal port argument.
throw new IllegalArgumentException(Messages.getString("logging.D")); //$NON-NLS-1$
}
if (p <= 0) {
// logging.D=Illegal port argument.
throw new IllegalArgumentException(Messages.getString("logging.D")); //$NON-NLS-1$
}
// establish the network connection
try {
this.socket = new Socket(host, p);
} catch (IOException e) {
// logging.E=Failed to establish the network connection.
getErrorManager().error(Messages.getString("logging.E"), e, //$NON-NLS-1$
ErrorManager.OPEN_FAILURE);
throw e;
}
// BEGIN android-modified
super.internalSetOutputStream(new BufferedOutputStream(this.socket
.getOutputStream(), 8192));
// END android-modified
| public void | publish(java.util.logging.LogRecord record)Logs a record if necessary. A flush operation will be done afterwards.
super.publish(record);
super.flush();
|
|