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 java.util.HashMap;
20  import java.util.Map;
21  import org.apache.logging.log4j.core.Filter;
22  import org.apache.logging.log4j.core.Layout;
23  import org.apache.logging.log4j.core.config.Configuration;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
26  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
27  import org.apache.logging.log4j.core.config.plugins.PluginElement;
28  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29  import org.apache.logging.log4j.core.layout.SerializedLayout;
30  import org.apache.logging.log4j.core.net.AbstractSocketManager;
31  import org.apache.logging.log4j.core.net.Advertiser;
32  import org.apache.logging.log4j.core.net.DatagramSocketManager;
33  import org.apache.logging.log4j.core.net.Protocol;
34  import org.apache.logging.log4j.core.net.TCPSocketManager;
35  import org.apache.logging.log4j.util.EnglishEnums;
36  
37  /**
38   * An Appender that delivers events over socket connections. Supports both TCP and UDP.
39   */
40  @Plugin(name = "Socket", type = "Core", elementType = "appender", printObject = true)
41  public class SocketAppender extends AbstractOutputStreamAppender {
42      private Object advertisement;
43      private final Advertiser advertiser;
44  
45      protected SocketAppender(final String name, final Layout layout, final Filter filter,
46                               final AbstractSocketManager manager, final boolean handleException,
47                               final boolean immediateFlush, Advertiser advertiser) {
48          super(name, layout, filter, handleException, immediateFlush, manager);
49          if (advertiser != null)
50          {
51              Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
52              configuration.putAll(manager.getContentFormat());
53              configuration.put("contentType", layout.getContentType());
54              configuration.put("name", name);
55              advertisement = advertiser.advertise(configuration);
56          }
57          this.advertiser = advertiser;
58      }
59  
60      @Override
61      public void stop() {
62          super.stop();
63          if (advertiser != null) {
64              advertiser.unadvertise(advertisement);
65          }
66      }
67  
68      /**
69       *
70       * @param host The name of the host to connect to.
71       * @param portNum The port to connect to on the target host.
72       * @param protocol The Protocol to use.
73       * @param delay The interval in which failed writes should be retried.
74       * @param immediateFail True if the write should fail if no socket is immediately available.
75       * @param name The name of the Appender.
76       * @param immediateFlush "true" if data should be flushed on each write.
77       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
78       * The default is "true".
79       * @param layout The layout to use (defaults to SerializedLayout).
80       * @param filter The Filter or null.
81       * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
82       * @param config The Configuration
83       * @return A SocketAppender.
84       */
85      @PluginFactory
86      public static SocketAppender createAppender(@PluginAttr("host") final String host,
87                                                  @PluginAttr("port") final String portNum,
88                                                  @PluginAttr("protocol") final String protocol,
89                                                  @PluginAttr("reconnectionDelay") final String delay,
90                                                  @PluginAttr("immediateFail") final String immediateFail,
91                                                  @PluginAttr("name") final String name,
92                                                  @PluginAttr("immediateFlush") final String immediateFlush,
93                                                  @PluginAttr("suppressExceptions") final String suppress,
94                                                  @PluginElement("layout") Layout layout,
95                                                  @PluginElement("filters") final Filter filter,
96                                                  @PluginAttr("advertise") final String advertise,
97                                                  @PluginConfiguration final Configuration config) {
98  
99          final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
100         boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise);
101         final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
102         final boolean fail = immediateFail == null ? true : Boolean.valueOf(immediateFail);
103         final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
104         final int port = portNum == null ? 0 : Integer.parseInt(portNum);
105         if (layout == null) {
106             layout = SerializedLayout.createLayout();
107         }
108 
109         if (name == null) {
110             LOGGER.error("No name provided for SocketAppender");
111             return null;
112         }
113 
114         final String prot = protocol != null ? protocol : Protocol.TCP.name();
115 
116         final AbstractSocketManager manager = createSocketManager(prot, host, port, reconnectDelay, fail);
117         if (manager == null) {
118             return null;
119         }
120 
121         return new SocketAppender(name, layout, filter, manager, handleExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null);
122     }
123 
124     protected static AbstractSocketManager createSocketManager(final String protocol, final String host, final int port,
125                                                                final int delay, final boolean immediateFail) {
126         final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol);
127         switch (p) {
128             case TCP:
129                 return TCPSocketManager.getSocketManager(host, port, delay, immediateFail);
130             case UDP:
131                 return DatagramSocketManager.getSocketManager(host, port);
132             default:
133                 return null;
134         }
135     }
136 }