src/share/classes/java/util/logging/SocketHandler.java

Print this page




  65  * <li>   java.util.logging.SocketHandler.level=INFO </li>
  66  * <li>   java.util.logging.SocketHandler.formatter=java.util.logging.SimpleFormatter </li>
  67  * </ul>
  68  * <p>
  69  * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
  70  * <ul>
  71  * <li>   com.foo.MyHandler.level=INFO </li>
  72  * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
  73  * </ul>
  74  * <p>
  75  * The output IO stream is buffered, but is flushed after each
  76  * <tt>LogRecord</tt> is written.
  77  *
  78  * @since 1.4
  79  */
  80 
  81 public class SocketHandler extends StreamHandler {
  82     private Socket sock;
  83     private String host;
  84     private int port;
  85     private String portProperty;
  86 
  87     // Private method to configure a SocketHandler from LogManager
  88     // properties and/or default values as specified in the class
  89     // javadoc.
  90     private void configure() {
  91         LogManager manager = LogManager.getLogManager();
  92         String cname = getClass().getName();
  93 
  94         setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
  95         setFilter(manager.getFilterProperty(cname +".filter", null));
  96         setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
  97         try {
  98             setEncoding(manager.getStringProperty(cname +".encoding", null));
  99         } catch (Exception ex) {
 100             try {
 101                 setEncoding(null);
 102             } catch (Exception ex2) {
 103                 // doing a setEncoding with null should always work.
 104                 // assert false;
 105             }


 160         if (port == 0) {
 161             throw new IllegalArgumentException("Bad port: " + port);
 162         }
 163         if (host == null) {
 164             throw new IllegalArgumentException("Null host name: " + host);
 165         }
 166 
 167         // Try to open a new socket.
 168         sock = new Socket(host, port);
 169         OutputStream out = sock.getOutputStream();
 170         BufferedOutputStream bout = new BufferedOutputStream(out);
 171         setOutputStream(bout);
 172     }
 173 
 174     /**
 175      * Close this output stream.
 176      *
 177      * @exception  SecurityException  if a security manager exists and if
 178      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 179      */

 180     public synchronized void close() throws SecurityException {
 181         super.close();
 182         if (sock != null) {
 183             try {
 184                 sock.close();
 185             } catch (IOException ix) {
 186                 // drop through.
 187             }
 188         }
 189         sock = null;
 190     }
 191 
 192     /**
 193      * Format and publish a <tt>LogRecord</tt>.
 194      *
 195      * @param  record  description of the log event. A null record is
 196      *                 silently ignored and is not published
 197      */

 198     public synchronized void publish(LogRecord record) {
 199         if (!isLoggable(record)) {
 200             return;
 201         }
 202         super.publish(record);
 203         flush();
 204     }
 205 }


  65  * <li>   java.util.logging.SocketHandler.level=INFO </li>
  66  * <li>   java.util.logging.SocketHandler.formatter=java.util.logging.SimpleFormatter </li>
  67  * </ul>
  68  * <p>
  69  * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
  70  * <ul>
  71  * <li>   com.foo.MyHandler.level=INFO </li>
  72  * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
  73  * </ul>
  74  * <p>
  75  * The output IO stream is buffered, but is flushed after each
  76  * <tt>LogRecord</tt> is written.
  77  *
  78  * @since 1.4
  79  */
  80 
  81 public class SocketHandler extends StreamHandler {
  82     private Socket sock;
  83     private String host;
  84     private int port;

  85 
  86     // Private method to configure a SocketHandler from LogManager
  87     // properties and/or default values as specified in the class
  88     // javadoc.
  89     private void configure() {
  90         LogManager manager = LogManager.getLogManager();
  91         String cname = getClass().getName();
  92 
  93         setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
  94         setFilter(manager.getFilterProperty(cname +".filter", null));
  95         setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
  96         try {
  97             setEncoding(manager.getStringProperty(cname +".encoding", null));
  98         } catch (Exception ex) {
  99             try {
 100                 setEncoding(null);
 101             } catch (Exception ex2) {
 102                 // doing a setEncoding with null should always work.
 103                 // assert false;
 104             }


 159         if (port == 0) {
 160             throw new IllegalArgumentException("Bad port: " + port);
 161         }
 162         if (host == null) {
 163             throw new IllegalArgumentException("Null host name: " + host);
 164         }
 165 
 166         // Try to open a new socket.
 167         sock = new Socket(host, port);
 168         OutputStream out = sock.getOutputStream();
 169         BufferedOutputStream bout = new BufferedOutputStream(out);
 170         setOutputStream(bout);
 171     }
 172 
 173     /**
 174      * Close this output stream.
 175      *
 176      * @exception  SecurityException  if a security manager exists and if
 177      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 178      */
 179     @Override
 180     public synchronized void close() throws SecurityException {
 181         super.close();
 182         if (sock != null) {
 183             try {
 184                 sock.close();
 185             } catch (IOException ix) {
 186                 // drop through.
 187             }
 188         }
 189         sock = null;
 190     }
 191 
 192     /**
 193      * Format and publish a <tt>LogRecord</tt>.
 194      *
 195      * @param  record  description of the log event. A null record is
 196      *                 silently ignored and is not published
 197      */
 198     @Override
 199     public synchronized void publish(LogRecord record) {
 200         if (!isLoggable(record)) {
 201             return;
 202         }
 203         super.publish(record);
 204         flush();
 205     }
 206 }