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 java.io.Serializable; 020 import java.nio.charset.Charset; 021 022 import org.apache.logging.log4j.core.Filter; 023 import org.apache.logging.log4j.core.Layout; 024 import org.apache.logging.log4j.core.config.Configuration; 025 import org.apache.logging.log4j.core.config.plugins.Plugin; 026 import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 027 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 028 import org.apache.logging.log4j.core.config.plugins.PluginElement; 029 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 030 import org.apache.logging.log4j.core.layout.LoggerFields; 031 import org.apache.logging.log4j.core.layout.Rfc5424Layout; 032 import org.apache.logging.log4j.core.layout.SyslogLayout; 033 import org.apache.logging.log4j.core.net.AbstractSocketManager; 034 import org.apache.logging.log4j.core.net.Advertiser; 035 import org.apache.logging.log4j.core.net.Facility; 036 import org.apache.logging.log4j.core.net.Protocol; 037 import org.apache.logging.log4j.core.net.ssl.SslConfiguration; 038 import org.apache.logging.log4j.util.EnglishEnums; 039 040 /** 041 * The Syslog Appender. 042 */ 043 @Plugin(name = "Syslog", category = "Core", elementType = "appender", printObject = true) 044 public class SyslogAppender extends SocketAppender { 045 046 protected static final String RFC5424 = "RFC5424"; 047 048 protected SyslogAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, 049 final boolean ignoreExceptions, final boolean immediateFlush, 050 final AbstractSocketManager manager, final Advertiser advertiser) { 051 super(name, layout, filter, manager, ignoreExceptions, immediateFlush, advertiser); 052 053 } 054 055 /** 056 * Create a SyslogAppender. 057 * @param host The name of the host to connect to. 058 * @param port The port to connect to on the target host. 059 * @param protocolStr The Protocol to use. 060 * @param sslConfig TODO 061 * @param reconnectionDelay The interval in which failed writes should be retried. 062 * @param immediateFail True if the write should fail if no socket is immediately available. 063 * @param name The name of the Appender. 064 * @param immediateFlush "true" if data should be flushed on each write. 065 * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged; 066 * otherwise they are propagated to the caller. 067 * @param facility The Facility is used to try to classify the message. 068 * @param id The default structured data id to use when formatting according to RFC 5424. 069 * @param enterpriseNumber The IANA enterprise number. 070 * @param includeMdc Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog 071 * record. Defaults to "true:. 072 * @param mdcId The id to use for the MDC Structured Data Element. 073 * @param mdcPrefix The prefix to add to MDC key names. 074 * @param eventPrefix The prefix to add to event key names. 075 * @param newLine If true, a newline will be appended to the end of the syslog record. The default is false. 076 * @param escapeNL String that should be used to replace newlines within the message text. 077 * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record. 078 * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records. 079 * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent. 080 * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent. 081 * @param required A comma separated list of mdc keys that must be present in the MDC. 082 * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise, 083 * it will be formatted as a BSD Syslog record. 084 * @param filter A Filter to determine if the event should be handled by this Appender. 085 * @param config The Configuration. 086 * @param charsetName The character set to use when converting the syslog String to a byte array. 087 * @param exceptionPattern The converter pattern to use for formatting exceptions. 088 * @param loggerFields The logger fields 089 * @param advertise Whether to advertise 090 * @return A SyslogAppender. 091 */ 092 @PluginFactory 093 public static SyslogAppender createAppender( 094 // @formatter:off 095 @PluginAttribute("host") final String host, 096 @PluginAttribute(value = "port", defaultInt = 0) final int port, 097 @PluginAttribute("protocol") final String protocolStr, 098 @PluginElement("SSL") final SslConfiguration sslConfig, 099 @PluginAttribute(value = "reconnectionDelay", defaultInt = 0) final int reconnectionDelay, 100 @PluginAttribute(value = "immediateFail", defaultBoolean = true) final boolean immediateFail, 101 @PluginAttribute("name") final String name, 102 @PluginAttribute(value = "immediateFlush", defaultBoolean = true) final boolean immediateFlush, 103 @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions, 104 @PluginAttribute(value = "facility", defaultString = "LOCAL0") final Facility facility, 105 @PluginAttribute("id") final String id, 106 @PluginAttribute(value = "enterpriseNumber", defaultInt = Rfc5424Layout.DEFAULT_ENTERPRISE_NUMBER) final int enterpriseNumber, 107 @PluginAttribute(value = "includeMdc", defaultBoolean = true) final boolean includeMdc, 108 @PluginAttribute("mdcId") final String mdcId, 109 @PluginAttribute("mdcPrefix") final String mdcPrefix, 110 @PluginAttribute("eventPrefix") final String eventPrefix, 111 @PluginAttribute(value = "newLine", defaultBoolean = false) final boolean newLine, 112 @PluginAttribute("newLineEscape") final String escapeNL, 113 @PluginAttribute("appName") final String appName, 114 @PluginAttribute("messageId") final String msgId, 115 @PluginAttribute("mdcExcludes") final String excludes, 116 @PluginAttribute("mdcIncludes") final String includes, 117 @PluginAttribute("mdcRequired") final String required, 118 @PluginAttribute("format") final String format, 119 @PluginElement("Filters") final Filter filter, 120 @PluginConfiguration final Configuration config, 121 @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charsetName, 122 @PluginAttribute("exceptionPattern") final String exceptionPattern, 123 @PluginElement("LoggerFields") final LoggerFields[] loggerFields, 124 @PluginAttribute(value = "advertise", defaultBoolean = false) final boolean advertise) { 125 // @formatter:on 126 127 // TODO: add Protocol to TypeConverters 128 final Protocol protocol = EnglishEnums.valueOf(Protocol.class, protocolStr); 129 final boolean useTlsMessageFormat = sslConfig != null || protocol == Protocol.SSL; 130 final Layout<? extends Serializable> layout = RFC5424.equalsIgnoreCase(format) ? 131 Rfc5424Layout.createLayout(facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix, eventPrefix, newLine, 132 escapeNL, appName, msgId, excludes, includes, required, exceptionPattern, useTlsMessageFormat, loggerFields, 133 config) : 134 SyslogLayout.createLayout(facility, newLine, escapeNL, charsetName); 135 136 if (name == null) { 137 LOGGER.error("No name provided for SyslogAppender"); 138 return null; 139 } 140 final AbstractSocketManager manager = createSocketManager(name, protocol, host, port, sslConfig, 141 reconnectionDelay, immediateFail, layout); 142 143 return new SyslogAppender(name, layout, filter, ignoreExceptions, immediateFlush, manager, 144 advertise ? config.getAdvertiser() : null); 145 } 146 }