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.Configuration;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
24  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
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.RFC5424Layout;
28  import org.apache.logging.log4j.core.layout.SyslogLayout;
29  import org.apache.logging.log4j.core.net.AbstractSocketManager;
30  import org.apache.logging.log4j.core.net.Advertiser;
31  import org.apache.logging.log4j.core.net.Protocol;
32  
33  import java.io.Serializable;
34  
35  /**
36   * The Syslog Appender.
37   */
38  @Plugin(name = "Syslog", category = "Core", elementType = "appender", printObject = true)
39  public class SyslogAppender<T extends Serializable> extends SocketAppender<T> {
40  
41      private static final String BSD = "bsd";
42  
43      private static final String RFC5424 = "RFC5424";
44  
45      protected SyslogAppender(final String name, final Layout<T> layout, final Filter filter,
46                               final boolean handleException, final boolean immediateFlush,
47                               final AbstractSocketManager manager, Advertiser advertiser) {
48          super(name, layout, filter, manager, handleException, immediateFlush, advertiser);
49  
50      }
51  
52      /**
53       * Create a SyslogAppender.
54       * @param host The name of the host to connect to.
55       * @param portNum The port to connect to on the target host.
56       * @param protocol The Protocol to use.
57       * @param delay The interval in which failed writes should be retried.
58       * @param immediateFail True if the write should fail if no socket is immediately available.
59       * @param name The name of the Appender.
60       * @param immediateFlush "true" if data should be flushed on each write.
61       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
62       * The default is "true".
63       * @param facility The Facility is used to try to classify the message.
64       * @param id The default structured data id to use when formatting according to RFC 5424.
65       * @param ein The IANA enterprise number.
66       * @param includeMDC Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
67       * record. Defaults to "true:.
68       * @param mdcId The id to use for the MDC Structured Data Element.
69       * @param mdcPrefix The prefix to add to MDC key names.
70       * @param eventPrefix The prefix to add to event key names.
71       * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false.
72       * @param escapeNL String that should be used to replace newlines within the message text.
73       * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
74       * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
75       * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
76       * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
77       * @param required A comma separated list of mdc keys that must be present in the MDC.
78       * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
79       * it will be formatted as a BSD Syslog record.
80       * @param filter A Filter to determine if the event should be handled by this Appender.
81       * @param config The Configuration.
82       * @param charsetName The character set to use when converting the syslog String to a byte array.
83       * @param exceptionPattern The converter pattern to use for formatting exceptions.
84       * @return A SyslogAppender.
85       */
86      @PluginFactory
87      public static <S extends Serializable> SyslogAppender<S> createAppender(@PluginAttr("host") final String host,
88                                                  @PluginAttr("port") final String portNum,
89                                                  @PluginAttr("protocol") final String protocol,
90                                                  @PluginAttr("reconnectionDelay") final String delay,
91                                                  @PluginAttr("immediateFail") final String immediateFail,
92                                                  @PluginAttr("name") final String name,
93                                                  @PluginAttr("immediateFlush") final String immediateFlush,
94                                                  @PluginAttr("suppressExceptions") final String suppress,
95                                                  @PluginAttr("facility") final String facility,
96                                                  @PluginAttr("id") final String id,
97                                                  @PluginAttr("enterpriseNumber") final String ein,
98                                                  @PluginAttr("includeMDC") final String includeMDC,
99                                                  @PluginAttr("mdcId") final String mdcId,
100                                                 @PluginAttr("mdcPrefix") final String mdcPrefix,
101                                                 @PluginAttr("eventPrefix") final String eventPrefix,
102                                                 @PluginAttr("newLine") final String includeNL,
103                                                 @PluginAttr("newLineEscape") final String escapeNL,
104                                                 @PluginAttr("appName") final String appName,
105                                                 @PluginAttr("messageId") final String msgId,
106                                                 @PluginAttr("mdcExcludes") final String excludes,
107                                                 @PluginAttr("mdcIncludes") final String includes,
108                                                 @PluginAttr("mdcRequired") final String required,
109                                                 @PluginAttr("format") final String format,
110                                                 @PluginElement("filters") final Filter filter,
111                                                 @PluginConfiguration final Configuration config,
112                                                 @PluginAttr("charset") final String charsetName,
113                                                 @PluginAttr("exceptionPattern") final String exceptionPattern,
114                                                 @PluginAttr("advertise") final String advertise) {
115 
116         final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
117         final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
118         final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
119         final boolean fail = immediateFail == null ? true : Boolean.valueOf(immediateFail);
120         final int port = portNum == null ? 0 : Integer.parseInt(portNum);
121         boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise);
122         @SuppressWarnings("unchecked")
123         final Layout<S> layout = (Layout<S>)(RFC5424.equalsIgnoreCase(format) ?
124             RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, mdcPrefix, eventPrefix, includeNL,
125                 escapeNL, appName, msgId, excludes, includes, required, charsetName, exceptionPattern, config) :
126             SyslogLayout.createLayout(facility, includeNL, escapeNL, charsetName));
127 
128         if (name == null) {
129             LOGGER.error("No name provided for SyslogAppender");
130             return null;
131         }
132         final String prot = protocol != null ? protocol : Protocol.UDP.name();
133         final AbstractSocketManager manager = createSocketManager(prot, host, port, reconnectDelay, fail);
134         if (manager == null) {
135             return null;
136         }
137 
138         return new SyslogAppender<S>(name, layout, filter, handleExceptions, isFlush, manager,
139                 isAdvertise ? config.getAdvertiser() : null);
140     }
141 }