001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.appender;
018    
019    import java.util.Locale;
020    
021    import org.apache.logging.log4j.core.Filter;
022    import org.apache.logging.log4j.core.Layout;
023    import org.apache.logging.log4j.core.config.plugins.Plugin;
024    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
025    import org.apache.logging.log4j.core.config.plugins.PluginElement;
026    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027    import org.apache.logging.log4j.core.layout.SerializedLayout;
028    import org.apache.logging.log4j.core.net.AbstractSocketManager;
029    import org.apache.logging.log4j.core.net.DatagramSocketManager;
030    import org.apache.logging.log4j.core.net.Facility;
031    import org.apache.logging.log4j.core.net.Protocol;
032    import org.apache.logging.log4j.core.net.TCPSocketManager;
033    import org.apache.logging.log4j.util.EnglishEnums;
034    
035    /**
036     * An Appender that delivers events over socket connections. Supports both TCP and UDP.
037     */
038    @Plugin(name = "Socket", type = "Core", elementType = "appender", printObject = true)
039    public class SocketAppender extends AbstractOutputStreamAppender {
040    
041    
042        protected SocketAppender(String name, Layout layout, Filter filter, AbstractSocketManager manager,
043                              boolean handleException, boolean immediateFlush) {
044            super(name, layout, filter, handleException, immediateFlush, manager);
045    
046        }
047    
048        /**
049         *
050         * @param host The name of the host to connect to.
051         * @param portNum The port to connect to on the target host.
052         * @param protocol The Protocol to use.
053         * @param delay The interval in which failed writes should be retried.
054         * @param name The name of the Appender.
055         * @param immediateFlush "true" if data should be flushed on each write.
056         * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
057         * The default is "true".
058         * @param layout The layout to use (defaults to SerlializedLayout).
059         * @param filter The Filter or null.
060         * @return A SocketAppender.
061         */
062        @PluginFactory
063        public static SocketAppender createAppender(@PluginAttr("host") String host,
064                                                    @PluginAttr("port") String portNum,
065                                                    @PluginAttr("protocol") String protocol,
066                                                    @PluginAttr("reconnectionDelay") String delay,
067                                                    @PluginAttr("name") String name,
068                                                    @PluginAttr("immediateFlush") String immediateFlush,
069                                                    @PluginAttr("suppressExceptions") String suppress,
070                                                    @PluginElement("layout") Layout layout,
071                                                    @PluginElement("filters") Filter filter) {
072    
073            boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
074            boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
075            int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
076            int port = portNum == null ? 0 : Integer.parseInt(portNum);
077            if (layout == null) {
078                layout = SerializedLayout.createLayout();
079            }
080    
081            if (name == null) {
082                LOGGER.error("No name provided for SocketAppender");
083                return null;
084            }
085    
086            AbstractSocketManager manager = createSocketManager(protocol, host, port, reconnectDelay);
087            if (manager == null) {
088                return null;
089            }
090            return new SocketAppender(name, layout, filter, manager, handleExceptions, isFlush);
091        }
092    
093        protected static AbstractSocketManager createSocketManager(String protocol, String host, int port, int delay) {
094            Protocol p = EnglishEnums.valueOf(Protocol.class, protocol);
095            switch (p) {
096                case TCP:
097                    return TCPSocketManager.getSocketManager(host, port, delay);
098                case UDP:
099                    return DatagramSocketManager.getSocketManager(host, port);
100                default:
101                    return null;
102            }
103        }
104    }