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     */
017    package org.apache.logging.log4j.core.appender;
018    
019    import org.apache.logging.log4j.core.Filter;
020    import org.apache.logging.log4j.core.Layout;
021    import org.apache.logging.log4j.core.config.Configuration;
022    import org.apache.logging.log4j.core.config.plugins.Plugin;
023    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
024    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
025    import org.apache.logging.log4j.core.config.plugins.PluginElement;
026    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027    import org.apache.logging.log4j.core.layout.RFC5424Layout;
028    import org.apache.logging.log4j.core.layout.SyslogLayout;
029    import org.apache.logging.log4j.core.net.AbstractSocketManager;
030    
031    import java.nio.charset.Charset;
032    
033    /**
034     * The Syslog Appender.
035     */
036    @Plugin(name = "Syslog", type = "Core", elementType = "appender", printObject = true)
037    public class SyslogAppender extends SocketAppender {
038    
039        private static final String BSD = "bsd";
040    
041        private static final String RFC5424 = "RFC5424";
042    
043        protected SyslogAppender(String name, Layout layout, Filter filter,
044                              boolean handleException, boolean immediateFlush, AbstractSocketManager manager) {
045            super(name, layout, filter, manager, handleException, immediateFlush);
046    
047        }
048    
049        /**
050         * Create a SyslogAppender.
051         * @param host The name of the host to connect to.
052         * @param portNum The port to connect to on the target host.
053         * @param protocol The Protocol to use.
054         * @param delay The interval in which failed writes should be retried.
055         * @param name The name of the Appender.
056         * @param immediateFlush "true" if data should be flushed on each write.
057         * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
058         * The default is "true".
059         * @param facility The Facility is used to try to classify the message.
060         * @param id The default structured data id to use when formatting according to RFC 5424.
061         * @param ein The IANA enterprise number.
062         * @param includeMDC Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
063         * record. Defaults to "true:.
064         * @param mdcId The id to use for the MDC Structured Data Element.
065         * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false.
066         * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record.
067         * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records.
068         * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent.
069         * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent.
070         * @param required A comma separated list of mdc keys that must be present in the MDC.
071         * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise,
072         * it will be formatted as a BSD Syslog record.
073         * @param filter A Filter to determine if the event should be handled by this Appender.
074         * @param config The Configuration.
075         * @param charset The character set to use when converting the syslog String to a byte array.
076         * @return A SyslogAppender.
077         */
078        @PluginFactory
079        public static SyslogAppender createAppender(@PluginAttr("host") String host,
080                                                    @PluginAttr("port") String portNum,
081                                                    @PluginAttr("protocol") String protocol,
082                                                    @PluginAttr("reconnectionDelay") String delay,
083                                                    @PluginAttr("name") String name,
084                                                    @PluginAttr("immediateFlush") String immediateFlush,
085                                                    @PluginAttr("suppressExceptions") String suppress,
086                                                    @PluginAttr("facility") String facility,
087                                                    @PluginAttr("id") String id,
088                                                    @PluginAttr("enterpriseNumber") String ein,
089                                                    @PluginAttr("includeMDC") String includeMDC,
090                                                    @PluginAttr("mdcId") String mdcId,
091                                                    @PluginAttr("newLine") String includeNL,
092                                                    @PluginAttr("appName") String appName,
093                                                    @PluginAttr("messageId") String msgId,
094                                                    @PluginAttr("mdcExcludes") String excludes,
095                                                    @PluginAttr("mdcIncludes") String includes,
096                                                    @PluginAttr("mdcRequired") String required,
097                                                    @PluginAttr("format") String format,
098                                                    @PluginElement("filters") Filter filter,
099                                                    @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 = (RFC5424.equalsIgnoreCase(format)) ?
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    }