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.layout.SerializedLayout;
32  import org.apache.logging.log4j.core.net.AbstractSocketManager;
33  import org.apache.logging.log4j.core.net.Advertiser;
34  import org.apache.logging.log4j.core.net.DatagramSocketManager;
35  import org.apache.logging.log4j.core.net.Protocol;
36  import org.apache.logging.log4j.core.net.SslSocketManager;
37  import org.apache.logging.log4j.core.net.TcpSocketManager;
38  import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
39  import org.apache.logging.log4j.core.util.Booleans;
40  import org.apache.logging.log4j.util.EnglishEnums;
41  
42  /**
43   * An Appender that delivers events over socket connections. Supports both TCP and UDP.
44   */
45  @Plugin(name = "Socket", category = "Core", elementType = "appender", printObject = true)
46  public class SocketAppender extends AbstractOutputStreamAppender<AbstractSocketManager> {
47      private Object advertisement;
48      private final Advertiser advertiser;
49  
50      protected SocketAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
51              final AbstractSocketManager manager, final boolean ignoreExceptions, final boolean immediateFlush,
52              final Advertiser advertiser) {
53          super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
54          if (advertiser != null) {
55              final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
56              configuration.putAll(manager.getContentFormat());
57              configuration.put("contentType", layout.getContentType());
58              configuration.put("name", name);
59              this.advertisement = advertiser.advertise(configuration);
60          }
61          this.advertiser = advertiser;
62      }
63  
64      @Override
65      public void stop() {
66          super.stop();
67          if (this.advertiser != null) {
68              this.advertiser.unadvertise(this.advertisement);
69          }
70      }
71  
72      /**
73       * 
74       * @param host
75       *        The name of the host to connect to.
76       * @param portNum
77       *        The port to connect to on the target host.
78       * @param protocolStr
79       *        The Protocol to use.
80       * @param sslConfig
81       *        The SSL configuration file for TCP/SSL, ignored for UPD.
82       * @param delay
83       *        The interval in which failed writes should be retried.
84       * @param immediateFail
85       *        True if the write should fail if no socket is immediately available.
86       * @param name
87       *        The name of the Appender.
88       * @param immediateFlush
89       *        "true" if data should be flushed on each write.
90       * @param ignore
91       *        If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they are
92       *        propagated to the caller.
93       * @param layout
94       *        The layout to use (defaults to SerializedLayout).
95       * @param filter
96       *        The Filter or null.
97       * @param advertise
98       *        "true" if the appender configuration should be advertised, "false" otherwise.
99       * @param config
100      *        The Configuration
101      * @return A SocketAppender.
102      */
103     @PluginFactory
104     public static SocketAppender createAppender(
105             // @formatter:off
106             @PluginAttribute("host") final String host,
107             @PluginAttribute("port") final String portNum,
108             @PluginAttribute("protocol") final String protocolStr,
109             @PluginElement("SSL") final SslConfiguration sslConfig,
110             @PluginAttribute("reconnectionDelay") final String delay,
111             @PluginAttribute("immediateFail") final String immediateFail,
112             @PluginAttribute("name") final String name,
113             @PluginAttribute("immediateFlush") final String immediateFlush,
114             @PluginAttribute("ignoreExceptions") final String ignore,
115             @PluginElement("Layout") Layout<? extends Serializable> layout,
116             @PluginElement("Filter") final Filter filter,
117             @PluginAttribute("advertise") final String advertise, 
118             @PluginConfiguration final Configuration config) {
119             // @formatter:on
120         boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
121         final boolean isAdvertise = Boolean.parseBoolean(advertise);
122         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
123         final boolean fail = Booleans.parseBoolean(immediateFail, true);
124         final int reconnectDelay = AbstractAppender.parseInt(delay, 0);
125         final int port = AbstractAppender.parseInt(portNum, 0);
126         if (layout == null) {
127             layout = SerializedLayout.createLayout();
128         }
129 
130         if (name == null) {
131             LOGGER.error("No name provided for SocketAppender");
132             return null;
133         }
134 
135         final Protocol protocol = EnglishEnums.valueOf(Protocol.class,
136                 protocolStr != null ? protocolStr : Protocol.TCP.name());
137         if (protocol == Protocol.UDP) {
138             isFlush = true;
139         }
140 
141         final AbstractSocketManager manager = createSocketManager(name, protocol, host, port, sslConfig,
142                 reconnectDelay, fail, layout);
143 
144         return new SocketAppender(name, layout, filter, manager, ignoreExceptions, isFlush,
145                 isAdvertise ? config.getAdvertiser() : null);
146     }
147 
148     /**
149      * Creates an AbstractSocketManager for TCP, UDP, and SSL.
150      * 
151      * @throws IllegalArgumentException
152      *         if the protocol cannot be handled.
153      */
154     protected static AbstractSocketManager createSocketManager(final String name, Protocol protocol, final String host,
155             final int port, final SslConfiguration sslConfig, final int delay, final boolean immediateFail,
156             final Layout<? extends Serializable> layout) {
157         if (protocol == Protocol.TCP && sslConfig != null) {
158             // Upgrade TCP to SSL if an SSL config is specified.
159             protocol = Protocol.SSL;
160         }
161         if (protocol != Protocol.SSL && sslConfig != null) {
162             LOGGER.info("Appender {} ignoring SSL configuration for {} protocol", name, protocol);
163         }
164         switch (protocol) {
165         case TCP:
166             return TcpSocketManager.getSocketManager(host, port, delay, immediateFail, layout);
167         case UDP:
168             return DatagramSocketManager.getSocketManager(host, port, layout);
169         case SSL:
170             return SslSocketManager.getSocketManager(sslConfig, host, port, delay, immediateFail, layout);
171         default:
172             throw new IllegalArgumentException(protocol.toString());
173         }
174     }
175 }