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 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.logging.log4j.core.Filter;
024import org.apache.logging.log4j.core.Layout;
025import org.apache.logging.log4j.core.config.Configuration;
026import org.apache.logging.log4j.core.config.plugins.Plugin;
027import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
028import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
029import org.apache.logging.log4j.core.config.plugins.PluginElement;
030import org.apache.logging.log4j.core.config.plugins.PluginFactory;
031import org.apache.logging.log4j.core.helpers.Booleans;
032import org.apache.logging.log4j.core.layout.SerializedLayout;
033import org.apache.logging.log4j.core.net.AbstractSocketManager;
034import org.apache.logging.log4j.core.net.Advertiser;
035import org.apache.logging.log4j.core.net.DatagramSocketManager;
036import org.apache.logging.log4j.core.net.Protocol;
037import org.apache.logging.log4j.core.net.TCPSocketManager;
038import org.apache.logging.log4j.util.EnglishEnums;
039
040/**
041 * An Appender that delivers events over socket connections. Supports both TCP and UDP.
042 */
043@Plugin(name = "Socket", category = "Core", elementType = "appender", printObject = true)
044public class SocketAppender extends AbstractOutputStreamAppender<AbstractSocketManager> {
045    private Object advertisement;
046    private final Advertiser advertiser;
047
048    protected SocketAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
049                             final AbstractSocketManager manager, final boolean ignoreExceptions,
050                             final boolean immediateFlush, final Advertiser advertiser) {
051        super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
052        if (advertiser != null) {
053            final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
054            configuration.putAll(manager.getContentFormat());
055            configuration.put("contentType", layout.getContentType());
056            configuration.put("name", name);
057            advertisement = advertiser.advertise(configuration);
058        }
059        this.advertiser = advertiser;
060    }
061
062    @Override
063    public void stop() {
064        super.stop();
065        if (advertiser != null) {
066            advertiser.unadvertise(advertisement);
067        }
068    }
069
070    /**
071     *
072     * @param host The name of the host to connect to.
073     * @param portNum The port to connect to on the target host.
074     * @param protocol The Protocol to use.
075     * @param delay The interval in which failed writes should be retried.
076     * @param immediateFail True if the write should fail if no socket is immediately available.
077     * @param name The name of the Appender.
078     * @param immediateFlush "true" if data should be flushed on each write.
079     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
080     *               they are propagated to the caller.
081     * @param layout The layout to use (defaults to SerializedLayout).
082     * @param filter The Filter or null.
083     * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
084     * @param config The Configuration
085     * @return A SocketAppender.
086     */
087    @PluginFactory
088    public static SocketAppender createAppender(
089            @PluginAttribute("host") final String host,
090            @PluginAttribute("port") final String portNum,
091            @PluginAttribute("protocol") final String protocol,
092            @PluginAttribute("reconnectionDelay") final String delay,
093            @PluginAttribute("immediateFail") final String immediateFail,
094            @PluginAttribute("name") final String name,
095            @PluginAttribute("immediateFlush") final String immediateFlush,
096            @PluginAttribute("ignoreExceptions") final String ignore,
097            @PluginElement("Layout") Layout<? extends Serializable> layout,
098            @PluginElement("Filters") final Filter filter,
099            @PluginAttribute("advertise") final String advertise,
100            @PluginConfiguration final Configuration config) {
101
102        boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
103        final boolean isAdvertise = Boolean.parseBoolean(advertise);
104        final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
105        final boolean fail = Booleans.parseBoolean(immediateFail, true);
106        final int reconnectDelay = AbstractAppender.parseInt(delay, 0);
107        final int port = AbstractAppender.parseInt(portNum, 0);
108        if (layout == null) {
109            layout = SerializedLayout.createLayout();
110        }
111
112        if (name == null) {
113            LOGGER.error("No name provided for SocketAppender");
114            return null;
115        }
116
117        final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol != null ? protocol : Protocol.TCP.name());
118        if (p.equals(Protocol.UDP)) {
119            isFlush = true;
120        }
121
122        final AbstractSocketManager manager = createSocketManager(p, host, port, reconnectDelay, fail, layout);
123        if (manager == null) {
124            return null;
125        }
126
127        return new SocketAppender(name, layout, filter, manager, ignoreExceptions, isFlush,
128                isAdvertise ? config.getAdvertiser() : null);
129    }
130
131    protected static AbstractSocketManager createSocketManager(final Protocol p, final String host, final int port,
132                                                               final int delay, final boolean immediateFail,
133                                                               final Layout<? extends Serializable> layout) {
134        switch (p) {
135            case TCP:
136                return TCPSocketManager.getSocketManager(host, port, delay, immediateFail, layout);
137            case UDP:
138                return DatagramSocketManager.getSocketManager(host, port, layout);
139            default:
140                return null;
141        }
142    }
143}