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; 020 021import org.apache.logging.log4j.core.Filter; 022import org.apache.logging.log4j.core.Layout; 023import org.apache.logging.log4j.core.config.Configuration; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 026import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 027import org.apache.logging.log4j.core.config.plugins.PluginElement; 028import org.apache.logging.log4j.core.config.plugins.PluginFactory; 029import org.apache.logging.log4j.core.helpers.Booleans; 030import org.apache.logging.log4j.core.layout.LoggerFields; 031import org.apache.logging.log4j.core.layout.RFC5424Layout; 032import org.apache.logging.log4j.core.layout.SyslogLayout; 033import org.apache.logging.log4j.core.net.AbstractSocketManager; 034import org.apache.logging.log4j.core.net.Advertiser; 035import org.apache.logging.log4j.core.net.Protocol; 036import org.apache.logging.log4j.util.EnglishEnums; 037 038/** 039 * The Syslog Appender. 040 */ 041@Plugin(name = "Syslog", category = "Core", elementType = "appender", printObject = true) 042public class SyslogAppender extends SocketAppender { 043 044 protected static final String RFC5424 = "RFC5424"; 045 046 protected SyslogAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, 047 final boolean ignoreExceptions, final boolean immediateFlush, 048 final AbstractSocketManager manager, final Advertiser advertiser) { 049 super(name, layout, filter, manager, ignoreExceptions, immediateFlush, advertiser); 050 051 } 052 053 /** 054 * Create a SyslogAppender. 055 * @param host The name of the host to connect to. 056 * @param portNum The port to connect to on the target host. 057 * @param protocol The Protocol to use. 058 * @param delay The interval in which failed writes should be retried. 059 * @param immediateFail True if the write should fail if no socket is immediately available. 060 * @param name The name of the Appender. 061 * @param immediateFlush "true" if data should be flushed on each write. 062 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 063 * they are propagated to the caller. 064 * @param facility The Facility is used to try to classify the message. 065 * @param id The default structured data id to use when formatting according to RFC 5424. 066 * @param ein The IANA enterprise number. 067 * @param includeMDC Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog 068 * record. Defaults to "true:. 069 * @param mdcId The id to use for the MDC Structured Data Element. 070 * @param mdcPrefix The prefix to add to MDC key names. 071 * @param eventPrefix The prefix to add to event key names. 072 * @param includeNL If true, a newline will be appended to the end of the syslog record. The default is false. 073 * @param escapeNL String that should be used to replace newlines within the message text. 074 * @param appName The value to use as the APP-NAME in the RFC 5424 syslog record. 075 * @param msgId The default value to be used in the MSGID field of RFC 5424 syslog records. 076 * @param excludes A comma separated list of mdc keys that should be excluded from the LogEvent. 077 * @param includes A comma separated list of mdc keys that should be included in the FlumeEvent. 078 * @param required A comma separated list of mdc keys that must be present in the MDC. 079 * @param format If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise, 080 * it will be formatted as a BSD Syslog record. 081 * @param filter A Filter to determine if the event should be handled by this Appender. 082 * @param config The Configuration. 083 * @param charsetName The character set to use when converting the syslog String to a byte array. 084 * @param exceptionPattern The converter pattern to use for formatting exceptions. 085 * @param loggerFields The logger fields 086 * @param advertise Whether to advertise 087 * @return A SyslogAppender. 088 */ 089 @PluginFactory 090 public static SyslogAppender createAppender( 091 @PluginAttribute("host") final String host, 092 @PluginAttribute("port") final String portNum, 093 @PluginAttribute("protocol") final String protocol, 094 @PluginAttribute("reconnectionDelay") final String delay, 095 @PluginAttribute("immediateFail") final String immediateFail, 096 @PluginAttribute("name") final String name, 097 @PluginAttribute("immediateFlush") final String immediateFlush, 098 @PluginAttribute("ignoreExceptions") final String ignore, 099 @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}