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