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.Protocol;
31  
32  /**
33   * The Syslog Appender.
34   */
35  @Plugin(name = "Syslog", type = "Core", elementType = "appender", printObject = true)
36  public class SyslogAppender extends SocketAppender {
37  
38      private static final String BSD = "bsd";
39  
40      private static final String RFC5424 = "RFC5424";
41  
42      protected SyslogAppender(final String name, final Layout layout, final Filter filter, final boolean handleException,
43                               final boolean immediateFlush, final AbstractSocketManager manager) {
44          super(name, layout, filter, manager, handleException, immediateFlush);
45  
46      }
47  
48      /**
49       * Create a SyslogAppender.
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 facility The Facility is used to try to classify the message.
59       * @param id The default structured data id to use when formatting according to RFC 5424.
60       * @param ein The IANA enterprise number.
61       * @param includeMDC Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
62       * record. Defaults to "true:.
63       * @param mdcId The id to use for the MDC Structured Data Element.
64       * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false.
65       * @param escapeNL String that should be used to replace newlines within the message text.
66       * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
67       * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
68       * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
69       * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
70       * @param required A comma separated list of mdc keys that must be present in the MDC.
71       * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
72       * it will be formatted as a BSD Syslog record.
73       * @param filter A Filter to determine if the event should be handled by this Appender.
74       * @param config The Configuration.
75       * @param charsetName The character set to use when converting the syslog String to a byte array.
76       * @param exceptionPattern The converter pattern to use for formatting exceptions.
77       * @return A SyslogAppender.
78       */
79      @PluginFactory
80      public static SyslogAppender createAppender(@PluginAttr("host") final String host,
81                                                  @PluginAttr("port") final String portNum,
82                                                  @PluginAttr("protocol") final String protocol,
83                                                  @PluginAttr("reconnectionDelay") final String delay,
84                                                  @PluginAttr("name") final String name,
85                                                  @PluginAttr("immediateFlush") final String immediateFlush,
86                                                  @PluginAttr("suppressExceptions") final String suppress,
87                                                  @PluginAttr("facility") final String facility,
88                                                  @PluginAttr("id") final String id,
89                                                  @PluginAttr("enterpriseNumber") final String ein,
90                                                  @PluginAttr("includeMDC") final String includeMDC,
91                                                  @PluginAttr("mdcId") final String mdcId,
92                                                  @PluginAttr("newLine") final String includeNL,
93                                                  @PluginAttr("newLineEscape") final String escapeNL,
94                                                  @PluginAttr("appName") final String appName,
95                                                  @PluginAttr("messageId") final String msgId,
96                                                  @PluginAttr("mdcExcludes") final String excludes,
97                                                  @PluginAttr("mdcIncludes") final String includes,
98                                                  @PluginAttr("mdcRequired") final String required,
99                                                  @PluginAttr("format") final String format,
100                                                 @PluginElement("filters") final Filter filter,
101                                                 @PluginConfiguration final Configuration config,
102                                                 @PluginAttr("charset") final String charsetName,
103                                                 @PluginAttr("exceptionPattern") final String exceptionPattern) {
104 
105         final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
106         final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
107         final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
108         final int port = portNum == null ? 0 : Integer.parseInt(portNum);
109         final Layout<String> layout = RFC5424.equalsIgnoreCase(format) ?
110             RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, includeNL, escapeNL, appName,
111                 msgId, excludes, includes, required, charsetName, exceptionPattern, config) :
112             SyslogLayout.createLayout(facility, includeNL, escapeNL, charsetName);
113 
114         if (name == null) {
115             LOGGER.error("No name provided for SyslogAppender");
116             return null;
117         }
118         final String prot = protocol != null ? protocol : Protocol.UDP.name();
119         final AbstractSocketManager manager = createSocketManager(prot, host, port, reconnectDelay);
120         if (manager == null) {
121             return null;
122         }
123 
124         return new SyslogAppender(name, layout, filter, handleExceptions, isFlush, manager);
125     }
126 }