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 import org.apache.logging.log4j.core.net.Advertiser; 031 import org.apache.logging.log4j.core.net.Protocol; 032 import org.apache.logging.log4j.util.EnglishEnums; 033 034 import java.io.Serializable; 035 036 /** 037 * The Syslog Appender. 038 */ 039 @Plugin(name = "Syslog", category = "Core", elementType = "appender", printObject = true) 040 public class SyslogAppender<T extends Serializable> extends SocketAppender<T> { 041 042 private static final String BSD = "bsd"; 043 044 private static final String RFC5424 = "RFC5424"; 045 046 protected SyslogAppender(final String name, final Layout<T> layout, final Filter filter, 047 final boolean handleException, final boolean immediateFlush, 048 final AbstractSocketManager manager, Advertiser advertiser) { 049 super(name, layout, filter, manager, handleException, 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 suppress "true" if exceptions should be hidden from the application, "false" otherwise. 063 * The default is "true". 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 * @return A SyslogAppender. 086 */ 087 @PluginFactory 088 public static <S extends Serializable> SyslogAppender<S> createAppender(@PluginAttr("host") final String host, 089 @PluginAttr("port") final String portNum, 090 @PluginAttr("protocol") final String protocol, 091 @PluginAttr("reconnectionDelay") final String delay, 092 @PluginAttr("immediateFail") final String immediateFail, 093 @PluginAttr("name") final String name, 094 @PluginAttr("immediateFlush") final String immediateFlush, 095 @PluginAttr("suppressExceptions") final String suppress, 096 @PluginAttr("facility") final String facility, 097 @PluginAttr("id") final String id, 098 @PluginAttr("enterpriseNumber") final String ein, 099 @PluginAttr("includeMDC") final String includeMDC, 100 @PluginAttr("mdcId") final String mdcId, 101 @PluginAttr("mdcPrefix") final String mdcPrefix, 102 @PluginAttr("eventPrefix") final String eventPrefix, 103 @PluginAttr("newLine") final String includeNL, 104 @PluginAttr("newLineEscape") final String escapeNL, 105 @PluginAttr("appName") final String appName, 106 @PluginAttr("messageId") final String msgId, 107 @PluginAttr("mdcExcludes") final String excludes, 108 @PluginAttr("mdcIncludes") final String includes, 109 @PluginAttr("mdcRequired") final String required, 110 @PluginAttr("format") final String format, 111 @PluginElement("filters") final Filter filter, 112 @PluginConfiguration final Configuration config, 113 @PluginAttr("charset") final String charsetName, 114 @PluginAttr("exceptionPattern") final String exceptionPattern, 115 @PluginAttr("advertise") final String advertise) { 116 117 final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush); 118 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 119 final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay); 120 final boolean fail = immediateFail == null ? true : Boolean.valueOf(immediateFail); 121 final int port = portNum == null ? 0 : Integer.parseInt(portNum); 122 boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise); 123 @SuppressWarnings("unchecked") 124 final Layout<S> layout = (Layout<S>)(RFC5424.equalsIgnoreCase(format) ? 125 RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, mdcPrefix, eventPrefix, includeNL, 126 escapeNL, appName, msgId, excludes, includes, required, exceptionPattern, config) : 127 SyslogLayout.createLayout(facility, includeNL, escapeNL, charsetName)); 128 129 if (name == null) { 130 LOGGER.error("No name provided for SyslogAppender"); 131 return null; 132 } 133 final String prot = protocol != null ? protocol : Protocol.UDP.name(); 134 final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol); 135 final AbstractSocketManager manager = createSocketManager(p, host, port, reconnectDelay, fail, layout); 136 if (manager == null) { 137 return null; 138 } 139 140 return new SyslogAppender<S>(name, layout, filter, handleExceptions, isFlush, manager, 141 isAdvertise ? config.getAdvertiser() : null); 142 } 143 }