View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import org.apache.logging.log4j.core.Filter;
20  import org.apache.logging.log4j.core.Layout;
21  import org.apache.logging.log4j.core.config.Configuration;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
24  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
25  import org.apache.logging.log4j.core.config.plugins.PluginElement;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  import org.apache.logging.log4j.core.layout.SerializedLayout;
28  import org.apache.logging.log4j.core.net.AbstractSocketManager;
29  import org.apache.logging.log4j.core.net.Advertiser;
30  import org.apache.logging.log4j.core.net.DatagramSocketManager;
31  import org.apache.logging.log4j.core.net.Protocol;
32  import org.apache.logging.log4j.core.net.TCPSocketManager;
33  import org.apache.logging.log4j.util.EnglishEnums;
34  
35  import java.io.Serializable;
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  /**
40   * An Appender that delivers events over socket connections. Supports both TCP and UDP.
41   */
42  @Plugin(name = "Socket", category = "Core", elementType = "appender", printObject = true)
43  public class SocketAppender<T extends Serializable> extends AbstractOutputStreamAppender<T> {
44      private Object advertisement;
45      private final Advertiser advertiser;
46  
47      protected SocketAppender(final String name, final Layout<T> layout, final Filter filter,
48                               final AbstractSocketManager manager, final boolean handleException,
49                               final boolean immediateFlush, Advertiser advertiser) {
50          super(name, layout, filter, handleException, immediateFlush, manager);
51          if (advertiser != null)
52          {
53              Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
54              configuration.putAll(manager.getContentFormat());
55              configuration.put("contentType", layout.getContentType());
56              configuration.put("name", name);
57              advertisement = advertiser.advertise(configuration);
58          }
59          this.advertiser = advertiser;
60      }
61  
62      @Override
63      public void stop() {
64          super.stop();
65          if (advertiser != null) {
66              advertiser.unadvertise(advertisement);
67          }
68      }
69  
70      /**
71       *
72       * @param host The name of the host to connect to.
73       * @param portNum The port to connect to on the target host.
74       * @param protocol The Protocol to use.
75       * @param delay The interval in which failed writes should be retried.
76       * @param immediateFail True if the write should fail if no socket is immediately available.
77       * @param name The name of the Appender.
78       * @param immediateFlush "true" if data should be flushed on each write.
79       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
80       * The default is "true".
81       * @param layout The layout to use (defaults to SerializedLayout).
82       * @param filter The Filter or null.
83       * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
84       * @param config The Configuration
85       * @return A SocketAppender.
86       */
87      @PluginFactory
88      public static <S extends Serializable> SocketAppender<S> createAppender(@PluginAttr("host") final String host,
89                                                  @PluginAttr("port") final String portNum,
90                                                  @PluginAttr("protocol") final String protocol,
91                                                  @PluginAttr("reconnectionDelay") final String delay,
92                                                  @PluginAttr("immediateFail") final String immediateFail,
93                                                  @PluginAttr("name") final String name,
94                                                  @PluginAttr("immediateFlush") final String immediateFlush,
95                                                  @PluginAttr("suppressExceptions") final String suppress,
96                                                  @PluginElement("layout") Layout<S> layout,
97                                                  @PluginElement("filters") final Filter filter,
98                                                  @PluginAttr("advertise") final String advertise,
99                                                  @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 }