Path: blob/master/src/java.logging/share/classes/java/util/logging/SocketHandler.java
41159 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package java.util.logging;2728import java.io.*;29import java.net.*;3031/**32* Simple network logging {@code Handler}.33* <p>34* {@code LogRecords} are published to a network stream connection. By default35* the {@code XMLFormatter} class is used for formatting.36* <p>37* <b>Configuration:</b>38* By default each {@code SocketHandler} is initialized using the following39* {@code LogManager} configuration properties where {@code <handler-name>}40* refers to the fully-qualified class name of the handler.41* If properties are not defined42* (or have invalid values) then the specified default values are used.43* <ul>44* <li> <handler-name>.level45* specifies the default level for the {@code Handler}46* (defaults to {@code Level.ALL}). </li>47* <li> <handler-name>.filter48* specifies the name of a {@code Filter} class to use49* (defaults to no {@code Filter}). </li>50* <li> <handler-name>.formatter51* specifies the name of a {@code Formatter} class to use52* (defaults to {@code java.util.logging.XMLFormatter}). </li>53* <li> <handler-name>.encoding54* the name of the character set encoding to use (defaults to55* the default platform encoding). </li>56* <li> <handler-name>.host57* specifies the target host name to connect to (no default). </li>58* <li> <handler-name>.port59* specifies the target TCP port to use (no default). </li>60* </ul>61* <p>62* For example, the properties for {@code SocketHandler} would be:63* <ul>64* <li> java.util.logging.SocketHandler.level=INFO </li>65* <li> java.util.logging.SocketHandler.formatter=java.util.logging.SimpleFormatter </li>66* </ul>67* <p>68* For a custom handler, e.g. com.foo.MyHandler, the properties would be:69* <ul>70* <li> com.foo.MyHandler.level=INFO </li>71* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>72* </ul>73* <p>74* The output IO stream is buffered, but is flushed after each75* {@code LogRecord} is written.76*77* @since 1.478*/7980public class SocketHandler extends StreamHandler {81private Socket sock;82private String host;83private int port;8485/**86* Create a {@code SocketHandler}, using only {@code LogManager} properties87* (or their defaults).88* @throws IllegalArgumentException if the host or port are invalid or89* are not specified as LogManager properties.90* @throws IOException if we are unable to connect to the target91* host and port.92*/93public SocketHandler() throws IOException {94// configure with specific defaults for SocketHandler95super(Level.ALL, new XMLFormatter(), null);9697LogManager manager = LogManager.getLogManager();98String cname = getClass().getName();99port = manager.getIntProperty(cname + ".port", 0);100host = manager.getStringProperty(cname + ".host", null);101102try {103connect();104} catch (IOException ix) {105System.err.println("SocketHandler: connect failed to " + host + ":" + port);106throw ix;107}108}109110/**111* Construct a {@code SocketHandler} using a specified host and port.112*113* The {@code SocketHandler} is configured based on {@code LogManager}114* properties (or their default values) except that the given target host115* and port arguments are used. If the host argument is empty, but not116* null String then the localhost is used.117*118* @param host target host.119* @param port target port.120*121* @throws IllegalArgumentException if the host or port are invalid.122* @throws IOException if we are unable to connect to the target123* host and port.124*/125public SocketHandler(String host, int port) throws IOException {126// configure with specific defaults for SocketHandler127super(Level.ALL, new XMLFormatter(), null);128129this.port = port;130this.host = host;131132connect();133}134135private void connect() throws IOException {136// Check the arguments are valid.137if (port == 0) {138throw new IllegalArgumentException("Bad port: " + port);139}140if (host == null) {141throw new IllegalArgumentException("Null host name: " + host);142}143144// Try to open a new socket.145sock = new Socket(host, port);146OutputStream out = sock.getOutputStream();147BufferedOutputStream bout = new BufferedOutputStream(out);148setOutputStreamPrivileged(bout);149}150151/**152* Close this output stream.153*154* @throws SecurityException if a security manager exists and if155* the caller does not have {@code LoggingPermission("control")}.156*/157@Override158public synchronized void close() throws SecurityException {159super.close();160if (sock != null) {161try {162sock.close();163} catch (IOException ix) {164// drop through.165}166}167sock = null;168}169170/**171* Format and publish a {@code LogRecord}.172*173* @param record description of the log event. A null record is174* silently ignored and is not published175*/176@Override177public synchronized void publish(LogRecord record) {178if (!isLoggable(record)) {179return;180}181super.publish(record);182flush();183}184}185186187