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  
31  import java.nio.charset.Charset;
32  
33  /**
34   * The Syslog Appender.
35   */
36  @Plugin(name = "Syslog", type = "Core", elementType = "appender", printObject = true)
37  public class SyslogAppender extends SocketAppender {
38  
39      private static final String BSD = "bsd";
40  
41      private static final String RFC5424 = "RFC5424";
42  
43      protected SyslogAppender(String name, Layout layout, Filter filter,
44                            boolean handleException, boolean immediateFlush, AbstractSocketManager manager) {
45          super(name, layout, filter, manager, handleException, immediateFlush);
46  
47      }
48  
49      /**
50       * Create a SyslogAppender.
51       * @param host The name of the host to connect to.
52       * @param portNum The port to connect to on the target host.
53       * @param protocol The Protocol to use.
54       * @param delay The interval in which failed writes should be retried.
55       * @param name The name of the Appender.
56       * @param immediateFlush "true" if data should be flushed on each write.
57       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
58       * The default is "true".
59       * @param facility The Facility is used to try to classify the message.
60       * @param id The default structured data id to use when formatting according to RFC 5424.
61       * @param ein The IANA enterprise number.
62       * @param includeMDC Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
63       * record. Defaults to "true:.
64       * @param mdcId The id to use for the MDC Structured Data Element.
65       * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false.
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 charset The character set to use when converting the syslog String to a byte array.
76       * @return A SyslogAppender.
77       */
78      @PluginFactory
79      public static SyslogAppender createAppender(@PluginAttr("host") String host,
80                                                  @PluginAttr("port") String portNum,
81                                                  @PluginAttr("protocol") String protocol,
82                                                  @PluginAttr("reconnectionDelay") String delay,
83                                                  @PluginAttr("name") String name,
84                                                  @PluginAttr("immediateFlush") String immediateFlush,
85                                                  @PluginAttr("suppressExceptions") String suppress,
86                                                  @PluginAttr("facility") String facility,
87                                                  @PluginAttr("id") String id,
88                                                  @PluginAttr("enterpriseNumber") String ein,
89                                                  @PluginAttr("includeMDC") String includeMDC,
90                                                  @PluginAttr("mdcId") String mdcId,
91                                                  @PluginAttr("newLine") String includeNL,
92                                                  @PluginAttr("appName") String appName,
93                                                  @PluginAttr("messageId") String msgId,
94                                                  @PluginAttr("mdcExcludes") String excludes,
95                                                  @PluginAttr("mdcIncludes") String includes,
96                                                  @PluginAttr("mdcRequired") String required,
97                                                  @PluginAttr("format") String format,
98                                                  @PluginElement("filters") Filter filter,
99                                                  @PluginConfiguration Configuration config,
100                                                 @PluginAttr("charset") String charset) {
101 
102         boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
103         boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
104         int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
105         int port = portNum == null ? 0 : Integer.parseInt(portNum);
106         Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
107         if (charset != null) {
108             if (Charset.isSupported(charset)) {
109                 c = Charset.forName(charset);
110             } else {
111                 LOGGER.error("Charset " + charset + " is not supported for layout, using " + c.displayName());
112             }
113         }
114         Layout layout = (format.equalsIgnoreCase(RFC5424)) ?
115             RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, includeNL, appName,  msgId,
116                 excludes, includes, required, charset, config) :
117             SyslogLayout.createLayout(facility, includeNL, charset);
118 
119         if (name == null) {
120             LOGGER.error("No name provided for SyslogAppender");
121             return null;
122         }
123         AbstractSocketManager manager = createSocketManager(protocol, host, port, reconnectDelay);
124         if (manager == null) {
125             return null;
126         }
127 
128         return new SyslogAppender(name, layout, filter, handleExceptions, isFlush, manager);
129     }
130 
131 
132 }