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.*;
23  import org.apache.logging.log4j.core.helpers.Booleans;
24  import org.apache.logging.log4j.core.layout.LoggerFields;
25  import org.apache.logging.log4j.core.layout.RFC5424Layout;
26  import org.apache.logging.log4j.core.layout.SyslogLayout;
27  import org.apache.logging.log4j.core.net.AbstractSocketManager;
28  import org.apache.logging.log4j.core.net.Advertiser;
29  import org.apache.logging.log4j.core.net.TLSSocketManager;
30  import org.apache.logging.log4j.core.net.ssl.SSLConfiguration;
31  
32  import java.io.Serializable;
33  
34  /**
35   *
36   * Secure Syslog Appender.
37   */
38  @Plugin(name = "TLSSyslog", category = "Core", elementType = "appender", printObject = true)
39  public final class TLSSyslogAppender extends SyslogAppender {
40  
41  
42      protected TLSSyslogAppender(String name, Layout<? extends Serializable> layout, Filter filter,
43                                  boolean ignoreExceptions, boolean immediateFlush, AbstractSocketManager manager,
44                                  Advertiser advertiser) {
45          super(name, layout, filter, ignoreExceptions, immediateFlush, manager, advertiser);
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 sslConfig   The SSL configuration
53       * @param delay The interval in which failed writes should be retried.
54       * @param immediateFail True if the write should fail if no socket is immediately available.
55       * @param name The name of the Appender.
56       * @param immediateFlush "true" if data should be flushed on each write.
57       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
58       *               they are propagated to the caller.
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 mdcPrefix The prefix to add to MDC key names.
66       * @param eventPrefix The prefix to add to event key names.
67       * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false.
68       * @param escapeNL String that should be used to replace newlines within the message text.
69       * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
70       * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
71       * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
72       * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
73       * @param required A comma separated list of mdc keys that must be present in the MDC.
74       * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
75       * it will be formatted as a BSD Syslog record.
76       * @param filter A Filter to determine if the event should be handled by this Appender.
77       * @param config The Configuration.
78       * @param charsetName The character set to use when converting the syslog String to a byte array.
79       * @param exceptionPattern The converter pattern to use for formatting exceptions.
80       * @param loggerFields The logger fields
81       * @param advertise Whether to advertise
82       * @return A TLSSyslogAppender.
83       */
84      @PluginFactory
85      public static TLSSyslogAppender createAppender(@PluginAttribute("host") final String host,
86                                                     @PluginAttribute("port") final String portNum,
87                                                     @PluginElement("ssl") final SSLConfiguration sslConfig,
88                                                     @PluginAttribute("reconnectionDelay") final String delay,
89                                                     @PluginAttribute("immediateFail") final String immediateFail,
90                                                     @PluginAttribute("name") final String name,
91                                                     @PluginAttribute("immediateFlush") final String immediateFlush,
92                                                     @PluginAttribute("ignoreExceptions") final String ignore,
93                                                     @PluginAttribute("facility") final String facility,
94                                                     @PluginAttribute("id") final String id,
95                                                     @PluginAttribute("enterpriseNumber") final String ein,
96                                                     @PluginAttribute("includeMDC") final String includeMDC,
97                                                     @PluginAttribute("mdcId") final String mdcId,
98                                                     @PluginAttribute("mdcPrefix") final String mdcPrefix,
99                                                     @PluginAttribute("eventPrefix") final String eventPrefix,
100                                                    @PluginAttribute("newLine") final String includeNL,
101                                                    @PluginAttribute("newLineEscape") final String escapeNL,
102                                                    @PluginAttribute("appName") final String appName,
103                                                    @PluginAttribute("messageId") final String msgId,
104                                                    @PluginAttribute("mdcExcludes") final String excludes,
105                                                    @PluginAttribute("mdcIncludes") final String includes,
106                                                    @PluginAttribute("mdcRequired") final String required,
107                                                    @PluginAttribute("format") final String format,
108                                                    @PluginElement("filters") final Filter filter,
109                                                    @PluginConfiguration final Configuration config,
110                                                    @PluginAttribute("charset") final String charsetName,
111                                                    @PluginAttribute("exceptionPattern") final String exceptionPattern,
112                                                    @PluginElement("LoggerFields") final LoggerFields[] loggerFields,
113                                                    @PluginAttribute("advertise") final String advertise) {
114         final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
115         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
116         final int reconnectDelay = AbstractAppender.parseInt(delay, 0);
117         final boolean fail = Booleans.parseBoolean(immediateFail, true);
118         final int port = AbstractAppender.parseInt(portNum, 0);
119         final boolean isAdvertise = Boolean.parseBoolean(advertise);
120         @SuppressWarnings("unchecked")
121         final Layout<? extends Serializable> layout = (RFC5424.equalsIgnoreCase(format) ?
122                 RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, mdcPrefix, eventPrefix, includeNL,
123                     escapeNL, appName, msgId, excludes, includes, required, exceptionPattern, "true" ,loggerFields,
124                     config) :
125                 SyslogLayout.createLayout(facility, includeNL, escapeNL, charsetName));
126 
127         if (name == null) {
128             LOGGER.error("No name provided for TLSSyslogAppender");
129             return null;
130         }
131         final AbstractSocketManager manager = createSocketManager(sslConfig, host, port, reconnectDelay, fail, layout);
132         if (manager == null) {
133             return null;
134         }
135 
136         return new TLSSyslogAppender(name, layout, filter, ignoreExceptions, isFlush, manager,
137                 isAdvertise ? config.getAdvertiser() : null);
138     }
139 
140     public static AbstractSocketManager createSocketManager(SSLConfiguration sslConf, String host, int port,
141                                                             int reconnectDelay, boolean fail,
142                                                             Layout<? extends Serializable> layout) {
143         return TLSSocketManager.getSocketManager(sslConf, host, port, reconnectDelay, fail, layout);
144     }
145 }