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.Locale;
20  
21  import org.apache.logging.log4j.core.Filter;
22  import org.apache.logging.log4j.core.Layout;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
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.DatagramSocketManager;
30  import org.apache.logging.log4j.core.net.Facility;
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  /**
36   * An Appender that delivers events over socket connections. Supports both TCP and UDP.
37   */
38  @Plugin(name = "Socket", type = "Core", elementType = "appender", printObject = true)
39  public class SocketAppender extends AbstractOutputStreamAppender {
40  
41  
42      protected SocketAppender(String name, Layout layout, Filter filter, AbstractSocketManager manager,
43                            boolean handleException, boolean immediateFlush) {
44          super(name, layout, filter, handleException, immediateFlush, manager);
45  
46      }
47  
48      /**
49       *
50       * @param host The name of the host to connect to.
51       * @param portNum The port to connect to on the target host.
52       * @param protocol The Protocol to use.
53       * @param delay The interval in which failed writes should be retried.
54       * @param name The name of the Appender.
55       * @param immediateFlush "true" if data should be flushed on each write.
56       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
57       * The default is "true".
58       * @param layout The layout to use (defaults to SerlializedLayout).
59       * @param filter The Filter or null.
60       * @return A SocketAppender.
61       */
62      @PluginFactory
63      public static SocketAppender createAppender(@PluginAttr("host") String host,
64                                                  @PluginAttr("port") String portNum,
65                                                  @PluginAttr("protocol") String protocol,
66                                                  @PluginAttr("reconnectionDelay") String delay,
67                                                  @PluginAttr("name") String name,
68                                                  @PluginAttr("immediateFlush") String immediateFlush,
69                                                  @PluginAttr("suppressExceptions") String suppress,
70                                                  @PluginElement("layout") Layout layout,
71                                                  @PluginElement("filters") Filter filter) {
72  
73          boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
74          boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
75          int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
76          int port = portNum == null ? 0 : Integer.parseInt(portNum);
77          if (layout == null) {
78              layout = SerializedLayout.createLayout();
79          }
80  
81          if (name == null) {
82              LOGGER.error("No name provided for SocketAppender");
83              return null;
84          }
85  
86          AbstractSocketManager manager = createSocketManager(protocol, host, port, reconnectDelay);
87          if (manager == null) {
88              return null;
89          }
90          return new SocketAppender(name, layout, filter, manager, handleExceptions, isFlush);
91      }
92  
93      protected static AbstractSocketManager createSocketManager(String protocol, String host, int port, int delay) {
94          Protocol p = EnglishEnums.valueOf(Protocol.class, protocol);
95          switch (p) {
96              case TCP:
97                  return TCPSocketManager.getSocketManager(host, port, delay);
98              case UDP:
99                  return DatagramSocketManager.getSocketManager(host, port);
100             default:
101                 return null;
102         }
103     }
104 }