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 org.apache.logging.log4j.core.Filter;
020 import org.apache.logging.log4j.core.Layout;
021 import org.apache.logging.log4j.core.config.Configuration;
022 import org.apache.logging.log4j.core.config.plugins.Plugin;
023 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
024 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
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.Advertiser;
030 import org.apache.logging.log4j.core.net.DatagramSocketManager;
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 import java.io.Serializable;
036 import java.util.HashMap;
037 import java.util.Map;
038
039 /**
040 * An Appender that delivers events over socket connections. Supports both TCP and UDP.
041 */
042 @Plugin(name = "Socket", category = "Core", elementType = "appender", printObject = true)
043 public class SocketAppender<T extends Serializable> extends AbstractOutputStreamAppender<T> {
044 private Object advertisement;
045 private final Advertiser advertiser;
046
047 protected SocketAppender(final String name, final Layout<T> layout, final Filter filter,
048 final AbstractSocketManager manager, final boolean handleException,
049 final boolean immediateFlush, Advertiser advertiser) {
050 super(name, layout, filter, handleException, immediateFlush, manager);
051 if (advertiser != null)
052 {
053 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 suppress "true" if exceptions should be hidden from the application, "false" otherwise.
080 * The default is "true".
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 <S extends Serializable> SocketAppender<S> createAppender(@PluginAttr("host") final String host,
089 @PluginAttr("port") final String portNum,
090 @PluginAttr("protocol") final String protocol,
091 @PluginAttr("reconnectionDelay") final String delay,
092 @PluginAttr("immediateFail") final String immediateFail,
093 @PluginAttr("name") final String name,
094 @PluginAttr("immediateFlush") final String immediateFlush,
095 @PluginAttr("suppressExceptions") final String suppress,
096 @PluginElement("layout") Layout<S> layout,
097 @PluginElement("filters") final Filter filter,
098 @PluginAttr("advertise") final String advertise,
099 @PluginConfiguration final Configuration config) {
100
101 boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
102 boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise);
103 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
104 final boolean fail = immediateFail == null ? true : Boolean.valueOf(immediateFail);
105 final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
106 final int port = portNum == null ? 0 : Integer.parseInt(portNum);
107 if (layout == null) {
108 @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
109 Layout<S> l = (Layout<S>) SerializedLayout.createLayout();
110 layout = l;
111 }
112
113 if (name == null) {
114 LOGGER.error("No name provided for SocketAppender");
115 return null;
116 }
117
118 final String prot = protocol != null ? protocol : Protocol.TCP.name();
119 final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol);
120 if (p.equals(Protocol.UDP)) {
121 isFlush = true;
122 }
123
124 final AbstractSocketManager manager = createSocketManager(p, host, port, reconnectDelay, fail, layout);
125 if (manager == null) {
126 return null;
127 }
128
129 return new SocketAppender<S>(name, layout, filter, manager, handleExceptions, isFlush,
130 isAdvertise ? config.getAdvertiser() : null);
131 }
132
133 protected static AbstractSocketManager createSocketManager(final Protocol p, final String host, final int port,
134 final int delay, final boolean immediateFail,
135 final Layout layout) {
136 switch (p) {
137 case TCP:
138 return TCPSocketManager.getSocketManager(host, port, delay, immediateFail, layout);
139 case UDP:
140 return DatagramSocketManager.getSocketManager(host, port, layout);
141 default:
142 return null;
143 }
144 }
145 }