Commit 2d0c9050 by Michael Koch Committed by Michael Koch

LogRecord.java, [...]: New files from classpath.

2003-06-21  Michael Koch  <konqueror@gmx.de>

	* java/util/logging/LogRecord.java,
	java/util/logging/Logger.java,
	java/util/logging/SocketHandler.java,
	java/util/logging/SimpleFormatter.java,
	java/util/logging/Formatter.java,
	java/util/logging/ErrorManager.java,
	java/util/logging/Handler.java,
	java/util/logging/FileHandler.java,
	java/util/logging/LogManager.java,
	java/util/logging/Level.java,
	java/util/logging/ConsoleHandler.java,
	java/util/logging/StreamHandler.java,
	java/util/logging/LoggingPermission.java,
	java/util/logging/Filter.java,
	java/util/logging/MemoryHandler.java,
	java/util/logging/XMLFormatter.java:
	New files from classpath.

From-SVN: r68295
parent c18cd642
2003-06-21 Michael Koch <konqueror@gmx.de>
* java/util/logging/LogRecord.java,
java/util/logging/Logger.java,
java/util/logging/SocketHandler.java,
java/util/logging/SimpleFormatter.java,
java/util/logging/Formatter.java,
java/util/logging/ErrorManager.java,
java/util/logging/Handler.java,
java/util/logging/FileHandler.java,
java/util/logging/LogManager.java,
java/util/logging/Level.java,
java/util/logging/ConsoleHandler.java,
java/util/logging/StreamHandler.java,
java/util/logging/LoggingPermission.java,
java/util/logging/Filter.java,
java/util/logging/MemoryHandler.java,
java/util/logging/XMLFormatter.java:
New files from classpath.
2003-06-20 Michael Koch <konqueror@gmx.de> 2003-06-20 Michael Koch <konqueror@gmx.de>
* java/io/ObjectStreamField.java * java/io/ObjectStreamField.java
......
/* ConsoleHandler.java
-- a class for publishing log messages to System.err
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
/**
* A <code>ConsoleHandler</code> publishes log records to
* <code>System.err</code>.
*
* <p><strong>Configuration:</strong> Values of the subsequent
* <code>LogManager</code> properties are taken into consideration
* when a <code>ConsoleHandler</code> is initialized.
* If a property is not defined, or if it has an invalid
* value, a default is taken without an exception being thrown.
*
* <ul>
*
* <li><code>java.util.logging.ConsoleHandler.level</code> - specifies
* the initial severity level threshold. Default value:
* <code>Level.INFO</code>.</li>
*
* <li><code>java.util.logging.ConsoleHandler.filter</code> - specifies
* the name of a Filter class. Default value: No Filter.</li>
*
* <li><code>java.util.logging.ConsoleHandler.formatter</code> - specifies
* the name of a Formatter class. Default value:
* <code>java.util.logging.SimpleFormatter</code>.</li>
*
* <li><code>java.util.logging.ConsoleHandler.encoding</code> - specifies
* the name of the character encoding. Default value:
* the default platform encoding.
*
* </ul>
*
* @author Sascha Brawer (brawer@acm.org)
*/
public class ConsoleHandler
extends StreamHandler
{
/**
* Constructs a <code>StreamHandler</code> that publishes
* log records to <code>System.err</code>. The initial
* configuration is determined by the <code>LogManager</code>
* properties described above.
*/
public ConsoleHandler()
{
super(System.err, "java.util.logging.ConsoleHandler", Level.INFO,
/* formatter */ null, SimpleFormatter.class);
}
/**
* Forces any data that may have been buffered to the underlying
* output device, but does <i>not</i> close <code>System.err</code>.
*
* <p>In case of an I/O failure, the <code>ErrorManager</code>
* of this <code>ConsoleHandler</code> will be informed, but the caller
* of this method will not receive an exception.
*/
public void close()
{
flush();
}
/**
* Publishes a <code>LogRecord</code> to the console, provided the
* record passes all tests for being loggable.
*
* <p>Most applications do not need to call this method directly.
* Instead, they will use use a <code>Logger</code>, which will
* create LogRecords and distribute them to registered handlers.
*
* <p>In case of an I/O failure, the <code>ErrorManager</code>
* of this <code>SocketHandler</code> will be informed, but the caller
* of this method will not receive an exception.
*
* <p>The GNU implementation of <code>ConsoleHandler.publish</code>
* calls flush() for every request to publish a record, so
* they appear immediately on the console.
*
* @param record the log event to be published.
*/
public void publish(LogRecord record)
{
super.publish(record);
flush();
}
}
/* ErrorManager.java
-- a class for dealing with errors that a Handler encounters
during logging
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
/**
* An <code>ErrorManager</code> deals with errors that a <code>Handler</code>
* encounters while logging.
*
* @see Handler#setErrorManager(ErrorManager)
*
* @author Sascha Brawer (brawer@acm.org)
*/
public class ErrorManager
{
/* The values have been taken from Sun's public J2SE 1.4 API
* documentation.
* See http://java.sun.com/j2se/1.4/docs/api/constant-values.html
*/
/**
* Indicates that there was a failure that does not readily
* fall into any of the other categories.
*/
public static final int GENERIC_FAILURE = 0;
/**
* Indicates that there was a problem upon writing to
* an output stream.
*/
public static final int WRITE_FAILURE = 1;
/**
* Indicates that there was a problem upon flushing
* an output stream.
*/
public static final int FLUSH_FAILURE = 2;
/**
* Indicates that there was a problem upon closing
* an output stream.
*/
public static final int CLOSE_FAILURE = 3;
/**
* Indicates that there was a problem upon opening
* an output stream.
*/
public static final int OPEN_FAILURE = 4;
/**
* Indicates that there was a problem upon formatting
* the message of a log record.
*/
public static final int FORMAT_FAILURE = 5;
private boolean everUsed = false;
public ErrorManager()
{
}
/**
* Reports an error that occured upon logging. The default implementation
* emits the very first error to System.err, ignoring subsequent errors.
*
* @param message a message describing the error, or <code>null</code> if
* there is no suitable description.
*
* @param ex an exception, or <code>null</code> if the error is not
* related to an exception.
*
* @param errorCode one of the defined error codes, for example
* <code>ErrorManager.CLOSE_FAILURE</code>.
*/
public void error(String message, Exception ex, int errorCode)
{
if (everUsed)
return;
synchronized (ErrorManager.class)
{
/* The double check is intentional. If the first check was
* omitted, the monitor would have to be entered every time
* error() method was called. If the second check was
* omitted, the code below could be executed by multiple
* threads simultaneously.
*/
if (everUsed)
return;
everUsed = true;
}
String codeMsg;
switch (errorCode)
{
case GENERIC_FAILURE:
codeMsg = "GENERIC_FAILURE";
break;
case WRITE_FAILURE:
codeMsg = "WRITE_FAILURE";
break;
case FLUSH_FAILURE:
codeMsg = "FLUSH_FAILURE";
break;
case CLOSE_FAILURE:
codeMsg = "CLOSE_FAILURE";
break;
case OPEN_FAILURE:
codeMsg = "OPEN_FAILURE";
break;
case FORMAT_FAILURE:
codeMsg = "FORMAT_FAILURE";
break;
default:
codeMsg = String.valueOf(errorCode);
break;
}
System.err.println("Error upon logging: " + codeMsg);
if ((message != null) && (message.length() > 0))
System.err.println(message);
if (ex != null)
ex.printStackTrace();
}
}
/* Filter.java
-- an interface for filters that decide whether a LogRecord should
be published or discarded
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
/**
* By implementing the <code>Filter</code> interface, applications
* can control what is being logged based on arbitrary properties,
* not just the severity level. Both <code>Handler</code> and
* <code>Logger</code> allow to register Filters whose
* <code>isLoggable</code> method will be called when a
* <code>LogRecord</code> has passed the test based on the
* severity level.
*
* @author Sascha Brawer (brawer@acm.org)
*/
public interface Filter
{
/**
* Determines whether a LogRecord should be published or discarded.
*
* @param record the <code>LogRecord</code> to be inspected.
*
* @return <code>true</code> if the record should be published,
* <code>false</code> if it should be discarded.
*/
public boolean isLoggable(LogRecord record);
}
/* Formatter.java
-- a class for formatting log messages by localizing message texts
and performing substitution of parameters
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
import java.util.ResourceBundle;
import java.text.MessageFormat;
/**
* A <code>Formatter</code> supports handlers by localizing
* message texts and by subsituting parameter values for their
* placeholders.
*
* @author Sascha Brawer (brawer@acm.org)
*/
public abstract class Formatter
{
/**
* Constructs a new Formatter.
*/
protected Formatter()
{
}
/**
* Formats a LogRecord into a string. Usually called by handlers
* which need a string for a log record, for example to append
* a record to a log file or to transmit a record over the network.
*
* @param record the log record for which a string form is requested.
*/
public abstract String format(LogRecord record);
/**
* Returns a string that handlers are supposed to emit before
* the first log record. The base implementation returns an
* empty string, but subclasses such as {@link XMLFormatter}
* override this method in order to provide a suitable header.
*
* @return a string for the header.
*
* @param handler the handler which will prepend the returned
* string in front of the first log record. This method
* may inspect certain properties of the handler, for
* example its encoding, in order to construct the header.
*/
public String getHead(Handler handler)
{
return "";
}
/**
* Returns a string that handlers are supposed to emit after
* the last log record. The base implementation returns an
* empty string, but subclasses such as {@link XMLFormatter}
* override this method in order to provide a suitable tail.
*
* @return a string for the header.
*
* @param handler the handler which will append the returned
* string after the last log record. This method
* may inspect certain properties of the handler
* in order to construct the tail.
*/
public String getTail(Handler handler)
{
return "";
}
/**
* Formats the message part of a log record.
*
* <p>First, the Formatter localizes the record message to the
* default locale by looking up the message in the record's
* localization resource bundle. If this step fails because there
* is no resource bundle associated with the record, or because the
* record message is not a key in the bundle, the raw message is
* used instead.
*
* <p>Second, the Formatter substitutes appropriate strings for
* the message parameters. If the record returns a non-empty
* array for <code>getParameters()</code> and the localized
* message string contains the character sequence "{0", the
* formatter uses <code>java.text.MessageFormat</code> to format
* the message. Otherwise, no parameter substitution is performed.
*
* @param record the log record to be localized and formatted.
*
* @return the localized message text where parameters have been
* substituted by suitable strings.
*
* @throws NullPointerException if <code>record</code>
* is <code>null</code>.
*/
public String formatMessage(LogRecord record)
{
String msg;
ResourceBundle bundle;
Object[] params;
/* This will throw a NullPointerExceptionif record is null. */
msg = record.getMessage();
if (msg == null)
msg = "";
/* Try to localize the message. */
bundle = record.getResourceBundle();
if (bundle != null)
{
try
{
msg = bundle.getString(msg);
}
catch (java.util.MissingResourceException _)
{
}
}
/* Format the message if there are parameters. */
params = record.getParameters();
if ((params != null)
&& (params.length > 0)
&& (msg.indexOf("{0") >= 0))
{
msg = MessageFormat.format(msg, params);
}
return msg;
}
}
/* LoggingPermission.java -- a class for logging permissions.
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
public final class LoggingPermission
extends java.security.BasicPermission
{
/**
* Creates a new LoggingPermission.
*
* @param name the name of the permission, which must be "control".
*
* @param actions the list of actions for the permission, which
* must be either <code>null</code> or an empty
* string.
*
* @exception IllegalArgumentException if <code>name</code>
* is not "control", or <code>actions</code> is
* neither <code>null</code> nor empty.
*/
public LoggingPermission(String name, String actions)
{
super("control", "");
if (!"control".equals(name))
{
throw new IllegalArgumentException(
"name of LoggingPermission must be \"control\"");
}
if ((actions != null) && (actions.length() != 0))
{
throw new IllegalArgumentException(
"actions of LoggingPermissions must be null or empty");
}
}
}
/* SimpleFormatter.java
-- a class for formatting log records into short human-readable messages
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
import java.util.Date;
import java.text.DateFormat;
/**
* A <code>SimpleFormatter</code> formats log records into
* short human-readable messages, typically one or two lines.
*
* @author Sascha Brawer (brawer@acm.org)
*/
public class SimpleFormatter
extends Formatter
{
/**
* Constructs a SimpleFormatter.
*/
public SimpleFormatter()
{
}
/**
* An instance of a DateFormatter that is used for formatting
* the time of a log record into a human-readable string,
* according to the rules of the current locale. The value
* is set after the first invocation of format, since it is
* common that a JVM will instantiate a SimpleFormatter without
* ever using it.
*/
private DateFormat dateFormat;
/**
* The character sequence that is used to separate lines in the
* generated stream. Somewhat surprisingly, the Sun J2SE 1.4
* reference implementation always uses UNIX line endings, even on
* platforms that have different line ending conventions (i.e.,
* DOS). The GNU implementation does not replicate this bug.
*
* @see Sun bug parade, bug #4462871,
* "java.util.logging.SimpleFormatter uses hard-coded line separator".
*/
static final String lineSep = System.getProperty("line.separator");
/**
* Formats a log record into a String.
*
* @param the log record to be formatted.
*
* @return a short human-readable message, typically one or two
* lines. Lines are separated using the default platform line
* separator.
*
* @throws NullPointerException if <code>record</code>
* is <code>null</code>.
*/
public String format(LogRecord record)
{
StringBuffer buf = new StringBuffer(180);
if (dateFormat == null)
dateFormat = DateFormat.getDateTimeInstance();
buf.append(dateFormat.format(new Date(record.getMillis())));
buf.append(' ');
buf.append(record.getLoggerName());
buf.append(lineSep);
buf.append(record.getLevel());
buf.append(": ");
buf.append(formatMessage(record));
buf.append(lineSep);
return buf.toString();
}
}
/* SocketHandler.java
-- a class for publishing log messages to network sockets
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
/**
* A <code>SocketHandler</code> publishes log records to
* a TCP/IP socket.
*
* <p><strong>Configuration:</strong> Values of the subsequent
* <code>LogManager</code> properties are taken into consideration
* when a <code>SocketHandler</code> is initialized.
* If a property is not defined, or if it has an invalid
* value, a default is taken without an exception being thrown.
*
* <ul>
*
* <li><code>java.util.SocketHandler.level</code> - specifies
* the initial severity level threshold. Default value:
* <code>Level.ALL</code>.</li>
*
* <li><code>java.util.SocketHandler.filter</code> - specifies
* the name of a Filter class. Default value: No Filter.</li>
*
* <li><code>java.util.SocketHandler.formatter</code> - specifies
* the name of a Formatter class. Default value:
* <code>java.util.logging.XMLFormatter</code>.</li>
*
* <li><code>java.util.SocketHandler.encoding</code> - specifies
* the name of the character encoding. Default value:
* the default platform encoding.
*
* <li><code>java.util.SocketHandler.host</code> - specifies
* the name of the host to which records are published.
* There is no default value for this property; if it is
* not set, the SocketHandler constructor will throw
* an exception.</li>
*
* <li><code>java.util.SocketHandler.port</code> - specifies
* the TCP/IP port to which records are published.
* There is no default value for this property; if it is
* not set, the SocketHandler constructor will throw
* an exception.</li>
*
* </ul>
*
* @author Sascha Brawer (brawer@acm.org)
*/
public class SocketHandler
extends StreamHandler
{
/**
* Constructs a <code>SocketHandler</code> that publishes log
* records to a TCP/IP socket. Tthe initial configuration is
* determined by the <code>LogManager</code> properties described
* above.
*
* @throws java.io.IOException if the connection to the specified
* network host and port cannot be established.
*
* @throws java.lang.IllegalArgumentException if either the
* <code>java.util.logging.SocketHandler.host</code>
* or <code>java.util.logging.SocketHandler.port</code>
* LogManager properties is not defined, or specifies
* an invalid value.
*/
public SocketHandler()
throws java.io.IOException
{
this(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.host"),
getPortNumber());
}
/**
* Constructs a <code>SocketHandler</code> that publishes log
* records to a TCP/IP socket. With the exception of the internet
* host and port, the initial configuration is determined by the
* <code>LogManager</code> properties described above.
*
* @param host the Internet host to which log records will be
* forwarded.
*
* @param port the port at the host which will accept a request
* for a TCP/IP connection.
*
* @throws java.io.IOException if the connection to the specified
* network host and port cannot be established.
*
* @throws java.lang.IllegalArgumentException if either
* <code>host</code> or <code>port</code> specify
* an invalid value.
*/
public SocketHandler(String host, int port)
throws java.io.IOException
{
super(createSocket(host, port),
"java.util.logging.SocketHandler",
/* default level */ Level.ALL,
/* formatter */ null,
/* default formatter */ XMLFormatter.class);
}
/**
* Retrieves the port number from the java.util.logging.SocketHandler.port
* LogManager property.
*
* @throws IllegalArgumentException if the property is not defined or
* does not specify an integer value.
*/
private static int getPortNumber()
{
try {
return Integer.parseInt(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.port"));
} catch (Exception ex) {
throw new IllegalArgumentException();
}
}
/**
* Creates an OutputStream for publishing log records to an Internet
* host and port. This private method is a helper for use by the
* constructor of SocketHandler.
*
* @param host the Internet host to which log records will be
* forwarded.
*
* @param port the port at the host which will accept a request
* for a TCP/IP connection.
*
* @throws java.io.IOException if the connection to the specified
* network host and port cannot be established.
*
* @throws java.lang.IllegalArgumentException if either
* <code>host</code> or <code>port</code> specify
* an invalid value.
*/
private static java.io.OutputStream createSocket(String host, int port)
throws java.io.IOException, java.lang.IllegalArgumentException
{
java.net.Socket socket;
if ((host == null) || (port < 1))
throw new IllegalArgumentException();
socket = new java.net.Socket(host, port);
socket.shutdownInput();
/* The architecture of the logging framework provides replaceable
* formatters. Because these formatters perform their task by
* returning one single String for each LogRecord to be formatted,
* there is no need to buffer.
*/
socket.setTcpNoDelay(true);
return socket.getOutputStream();
}
/**
* Publishes a <code>LogRecord</code> to the network socket,
* provided the record passes all tests for being loggable.
* In addition, all data that may have been buffered will
* be forced to the network stream.
*
* <p>Most applications do not need to call this method directly.
* Instead, they will use a {@link Logger} instance, which will
* create LogRecords and distribute them to registered handlers.
*
* <p>In case of an I/O failure, the <code>ErrorManager</code>
* of this <code>SocketHandler</code> will be informed, but the caller
* of this method will not receive an exception.
*
* @param record the log event to be published.
*/
public void publish(LogRecord record)
{
super.publish(record);
flush();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment