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