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.io.Serializable;
20  import java.nio.charset.Charset;
21  
22  import org.apache.logging.log4j.core.Filter;
23  import org.apache.logging.log4j.core.Layout;
24  import org.apache.logging.log4j.core.config.Configuration;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAliases;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
29  import org.apache.logging.log4j.core.config.plugins.PluginElement;
30  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
31  import org.apache.logging.log4j.core.layout.LoggerFields;
32  import org.apache.logging.log4j.core.layout.Rfc5424Layout;
33  import org.apache.logging.log4j.core.layout.SyslogLayout;
34  import org.apache.logging.log4j.core.net.AbstractSocketManager;
35  import org.apache.logging.log4j.core.net.Advertiser;
36  import org.apache.logging.log4j.core.net.Facility;
37  import org.apache.logging.log4j.core.net.Protocol;
38  import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
39  import org.apache.logging.log4j.util.EnglishEnums;
40  
41  /**
42   * The Syslog Appender.
43   */
44  @Plugin(name = "Syslog", category = "Core", elementType = "appender", printObject = true)
45  public class SyslogAppender extends SocketAppender {
46  
47      protected static final String RFC5424 = "RFC5424";
48  
49      protected SyslogAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
50                               final boolean ignoreExceptions, final boolean immediateFlush,
51                               final AbstractSocketManager manager, final Advertiser advertiser) {
52          super(name, layout, filter, manager, ignoreExceptions, immediateFlush, advertiser);
53  
54      }
55  
56      /**
57       * Create a SyslogAppender.
58       * @param host The name of the host to connect to.
59       * @param port The port to connect to on the target host.
60       * @param protocolStr The Protocol to use.
61       * @param sslConfig TODO
62       * @param connectTimeoutMillis the connect timeout in milliseconds.
63       * @param reconnectionDelayMillis The interval in which failed writes should be retried.
64       * @param immediateFail True if the write should fail if no socket is immediately available.
65       * @param name The name of the Appender.
66       * @param immediateFlush "true" if data should be flushed on each write.
67       * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged;
68       *                         otherwise they are propagated to the caller.
69       * @param facility The Facility is used to try to classify the message.
70       * @param id The default structured data id to use when formatting according to RFC 5424.
71       * @param enterpriseNumber The IANA enterprise number.
72       * @param includeMdc Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
73       * record. Defaults to "true:.
74       * @param mdcId The id to use for the MDC Structured Data Element.
75       * @param mdcPrefix The prefix to add to MDC key names.
76       * @param eventPrefix The prefix to add to event key names.
77       * @param newLine If true, a newline will be appended to the end of the syslog record. The default is false.
78       * @param escapeNL String that should be used to replace newlines within the message text.
79       * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
80       * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
81       * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
82       * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
83       * @param required A comma separated list of mdc keys that must be present in the MDC.
84       * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
85       * it will be formatted as a BSD Syslog record.
86       * @param filter A Filter to determine if the event should be handled by this Appender.
87       * @param config The Configuration.
88       * @param charsetName The character set to use when converting the syslog String to a byte array.
89       * @param exceptionPattern The converter pattern to use for formatting exceptions.
90       * @param loggerFields The logger fields
91       * @param advertise Whether to advertise
92       * @param connectTimeoutMillis the connect timeout in milliseconds.
93       * @return A SyslogAppender.
94       */
95      @PluginFactory
96      public static SyslogAppender createAppender(
97              // @formatter:off
98              @PluginAttribute("host") final String host,
99              @PluginAttribute(value = "port", defaultInt = 0) final int port,
100             @PluginAttribute("protocol") final String protocolStr,
101             @PluginElement("SSL") final SslConfiguration sslConfig,
102             @PluginAttribute(value = "connectTimeoutMillis", defaultInt = 0) final int connectTimeoutMillis,
103             @PluginAliases("reconnectionDelay") // deprecated
104             @PluginAttribute(value = "reconnectionDelayMillis", defaultInt = 0) final int reconnectionDelayMillis,
105             @PluginAttribute(value = "immediateFail", defaultBoolean = true) final boolean immediateFail,
106             @PluginAttribute("name") final String name,
107             @PluginAttribute(value = "immediateFlush", defaultBoolean = true) final boolean immediateFlush,
108             @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions,
109             @PluginAttribute(value = "facility", defaultString = "LOCAL0") final Facility facility,
110             @PluginAttribute("id") final String id,
111             @PluginAttribute(value = "enterpriseNumber", defaultInt = Rfc5424Layout.DEFAULT_ENTERPRISE_NUMBER) final int enterpriseNumber,
112             @PluginAttribute(value = "includeMdc", defaultBoolean = true) final boolean includeMdc,
113             @PluginAttribute("mdcId") final String mdcId,
114             @PluginAttribute("mdcPrefix") final String mdcPrefix,
115             @PluginAttribute("eventPrefix") final String eventPrefix,
116             @PluginAttribute(value = "newLine", defaultBoolean = false) final boolean newLine,
117             @PluginAttribute("newLineEscape") final String escapeNL,
118             @PluginAttribute("appName") final String appName,
119             @PluginAttribute("messageId") final String msgId,
120             @PluginAttribute("mdcExcludes") final String excludes,
121             @PluginAttribute("mdcIncludes") final String includes,
122             @PluginAttribute("mdcRequired") final String required,
123             @PluginAttribute("format") final String format,
124             @PluginElement("Filter") final Filter filter,
125             @PluginConfiguration final Configuration config,
126             @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charsetName,
127             @PluginAttribute("exceptionPattern") final String exceptionPattern,
128             @PluginElement("LoggerFields") final LoggerFields[] loggerFields, @PluginAttribute(value = "advertise", defaultBoolean = false) final boolean advertise) {
129         // @formatter:on
130 
131         // TODO: add Protocol to TypeConverters
132         final Protocol protocol = EnglishEnums.valueOf(Protocol.class, protocolStr);
133         final boolean useTlsMessageFormat = sslConfig != null || protocol == Protocol.SSL;
134         final Layout<? extends Serializable> layout = RFC5424.equalsIgnoreCase(format) ?
135             Rfc5424Layout.createLayout(facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix, eventPrefix, newLine,
136                 escapeNL, appName, msgId, excludes, includes, required, exceptionPattern, useTlsMessageFormat, loggerFields,
137                 config) :
138             SyslogLayout.createLayout(facility, newLine, escapeNL, charsetName);
139 
140         if (name == null) {
141             LOGGER.error("No name provided for SyslogAppender");
142             return null;
143         }
144         final AbstractSocketManager manager = createSocketManager(name, protocol, host, port, connectTimeoutMillis,
145                 sslConfig, reconnectionDelayMillis, immediateFail, layout);
146 
147         return new SyslogAppender(name, layout, filter, ignoreExceptions, immediateFlush, manager,
148                 advertise ? config.getAdvertiser() : null);
149     }
150 }