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.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.Layout;
25  import org.apache.logging.log4j.core.config.Configuration;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
29  import org.apache.logging.log4j.core.config.plugins.PluginElement;
30  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
31  import org.apache.logging.log4j.core.helpers.Booleans;
32  import org.apache.logging.log4j.core.layout.SerializedLayout;
33  import org.apache.logging.log4j.core.net.AbstractSocketManager;
34  import org.apache.logging.log4j.core.net.Advertiser;
35  import org.apache.logging.log4j.core.net.DatagramSocketManager;
36  import org.apache.logging.log4j.core.net.Protocol;
37  import org.apache.logging.log4j.core.net.TCPSocketManager;
38  import org.apache.logging.log4j.util.EnglishEnums;
39  
40  /**
41   * An Appender that delivers events over socket connections. Supports both TCP and UDP.
42   */
43  @Plugin(name = "Socket", category = "Core", elementType = "appender", printObject = true)
44  public class SocketAppender extends AbstractOutputStreamAppender<AbstractSocketManager> {
45      private Object advertisement;
46      private final Advertiser advertiser;
47  
48      protected SocketAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
49                               final AbstractSocketManager manager, final boolean ignoreExceptions,
50                               final boolean immediateFlush, final Advertiser advertiser) {
51          super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
52          if (advertiser != null) {
53              final 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 ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
80       *               they are propagated to the caller.
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 SocketAppender createAppender(
89              @PluginAttribute("host") final String host,
90              @PluginAttribute("port") final String portNum,
91              @PluginAttribute("protocol") final String protocol,
92              @PluginAttribute("reconnectionDelay") final String delay,
93              @PluginAttribute("immediateFail") final String immediateFail,
94              @PluginAttribute("name") final String name,
95              @PluginAttribute("immediateFlush") final String immediateFlush,
96              @PluginAttribute("ignoreExceptions") final String ignore,
97              @PluginElement("Layout") Layout<? extends Serializable> layout,
98              @PluginElement("Filters") final Filter filter,
99              @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 }