001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.Serializable;
020import java.nio.charset.Charset;
021
022import org.apache.logging.log4j.core.Appender;
023import org.apache.logging.log4j.core.Filter;
024import org.apache.logging.log4j.core.Layout;
025import org.apache.logging.log4j.core.config.Configuration;
026import org.apache.logging.log4j.core.config.plugins.Plugin;
027import org.apache.logging.log4j.core.config.plugins.PluginAliases;
028import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
029import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
030import org.apache.logging.log4j.core.config.plugins.PluginElement;
031import org.apache.logging.log4j.core.config.plugins.PluginFactory;
032import org.apache.logging.log4j.core.layout.LoggerFields;
033import org.apache.logging.log4j.core.layout.Rfc5424Layout;
034import org.apache.logging.log4j.core.layout.SyslogLayout;
035import org.apache.logging.log4j.core.net.AbstractSocketManager;
036import org.apache.logging.log4j.core.net.Advertiser;
037import org.apache.logging.log4j.core.net.Facility;
038import org.apache.logging.log4j.core.net.Protocol;
039import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
040import org.apache.logging.log4j.core.util.Constants;
041import org.apache.logging.log4j.util.EnglishEnums;
042
043/**
044 * The Syslog Appender.
045 */
046@Plugin(name = "Syslog", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
047public class SyslogAppender extends SocketAppender {
048
049    protected static final String RFC5424 = "RFC5424";
050
051    protected SyslogAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
052                             final boolean ignoreExceptions, final boolean immediateFlush,
053                             final AbstractSocketManager manager, final Advertiser advertiser) {
054        super(name, layout, filter, manager, ignoreExceptions, immediateFlush, advertiser);
055
056    }
057
058    /**
059     * Create a SyslogAppender.
060     * @param host The name of the host to connect to.
061     * @param port The port to connect to on the target host.
062     * @param protocolStr The Protocol to use.
063     * @param sslConfig TODO
064     * @param connectTimeoutMillis the connect timeout in milliseconds.
065     * @param reconnectionDelayMillis The interval in which failed writes should be retried.
066     * @param immediateFail True if the write should fail if no socket is immediately available.
067     * @param name The name of the Appender.
068     * @param immediateFlush "true" if data should be flushed on each write.
069     * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged;
070     *                         otherwise they are propagated to the caller.
071     * @param facility The Facility is used to try to classify the message.
072     * @param id The default structured data id to use when formatting according to RFC 5424.
073     * @param enterpriseNumber The IANA enterprise number.
074     * @param includeMdc Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
075     * record. Defaults to "true:.
076     * @param mdcId The id to use for the MDC Structured Data Element.
077     * @param mdcPrefix The prefix to add to MDC key names.
078     * @param eventPrefix The prefix to add to event key names.
079     * @param newLine If true, a newline will be appended to the end of the syslog record. The default is false.
080     * @param escapeNL String that should be used to replace newlines within the message text.
081     * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
082     * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
083     * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
084     * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
085     * @param required A comma separated list of mdc keys that must be present in the MDC.
086     * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
087     * it will be formatted as a BSD Syslog record.
088     * @param filter A Filter to determine if the event should be handled by this Appender.
089     * @param config The Configuration.
090     * @param charsetName The character set to use when converting the syslog String to a byte array.
091     * @param exceptionPattern The converter pattern to use for formatting exceptions.
092     * @param loggerFields The logger fields
093     * @param advertise Whether to advertise
094     * @return A SyslogAppender.
095     */
096    @PluginFactory
097    public static SyslogAppender createAppender(
098            // @formatter:off
099            @PluginAttribute("host") final String host,
100            @PluginAttribute(value = "port", defaultInt = 0) final int port,
101            @PluginAttribute("protocol") final String protocolStr,
102            @PluginElement("SSL") final SslConfiguration sslConfig,
103            @PluginAttribute(value = "connectTimeoutMillis", defaultInt = 0) final int connectTimeoutMillis,
104            @PluginAliases("reconnectionDelay") // deprecated
105            @PluginAttribute(value = "reconnectionDelayMillis", defaultInt = 0) final int reconnectionDelayMillis,
106            @PluginAttribute(value = "immediateFail", defaultBoolean = true) final boolean immediateFail,
107            @PluginAttribute("name") final String name,
108            @PluginAttribute(value = "immediateFlush", defaultBoolean = true) final boolean immediateFlush,
109            @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions,
110            @PluginAttribute(value = "facility", defaultString = "LOCAL0") final Facility facility,
111            @PluginAttribute("id") final String id,
112            @PluginAttribute(value = "enterpriseNumber", defaultInt = Rfc5424Layout.DEFAULT_ENTERPRISE_NUMBER) final int enterpriseNumber,
113            @PluginAttribute(value = "includeMdc", defaultBoolean = true) final boolean includeMdc,
114            @PluginAttribute("mdcId") final String mdcId,
115            @PluginAttribute("mdcPrefix") final String mdcPrefix,
116            @PluginAttribute("eventPrefix") final String eventPrefix,
117            @PluginAttribute(value = "newLine", defaultBoolean = false) final boolean newLine,
118            @PluginAttribute("newLineEscape") final String escapeNL,
119            @PluginAttribute("appName") final String appName,
120            @PluginAttribute("messageId") final String msgId,
121            @PluginAttribute("mdcExcludes") final String excludes,
122            @PluginAttribute("mdcIncludes") final String includes,
123            @PluginAttribute("mdcRequired") final String required,
124            @PluginAttribute("format") final String format,
125            @PluginElement("Filter") final Filter filter,
126            @PluginConfiguration final Configuration config,
127            @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charsetName,
128            @PluginAttribute("exceptionPattern") final String exceptionPattern,
129            @PluginElement("LoggerFields") final LoggerFields[] loggerFields, 
130            @PluginAttribute(value = "advertise", defaultBoolean = false) final boolean advertise) {
131        // @formatter:on
132
133        final Protocol protocol = EnglishEnums.valueOf(Protocol.class, protocolStr);
134        final boolean useTlsMessageFormat = sslConfig != null || protocol == Protocol.SSL;
135        final Layout<? extends Serializable> layout = RFC5424.equalsIgnoreCase(format) ?
136            Rfc5424Layout.createLayout(facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix, eventPrefix, newLine,
137                escapeNL, appName, msgId, excludes, includes, required, exceptionPattern, useTlsMessageFormat, loggerFields,
138                config) :
139            SyslogLayout.createLayout(facility, newLine, escapeNL, charsetName);
140
141        if (name == null) {
142            LOGGER.error("No name provided for SyslogAppender");
143            return null;
144        }
145        final AbstractSocketManager manager = createSocketManager(name, protocol, host, port, connectTimeoutMillis,
146                sslConfig, reconnectionDelayMillis, immediateFail, layout, Constants.ENCODER_BYTE_BUFFER_SIZE);
147
148        return new SyslogAppender(name, layout, filter, ignoreExceptions, immediateFlush, manager,
149                advertise ? config.getAdvertiser() : null);
150    }
151}