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.util.HashMap; 020 import java.util.Map; 021 import org.apache.logging.log4j.core.Filter; 022 import org.apache.logging.log4j.core.Layout; 023 import org.apache.logging.log4j.core.config.Configuration; 024 import org.apache.logging.log4j.core.config.plugins.Plugin; 025 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 026 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 027 import org.apache.logging.log4j.core.config.plugins.PluginElement; 028 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 029 import org.apache.logging.log4j.core.layout.SerializedLayout; 030 import org.apache.logging.log4j.core.net.AbstractSocketManager; 031 import org.apache.logging.log4j.core.net.Advertiser; 032 import org.apache.logging.log4j.core.net.DatagramSocketManager; 033 import org.apache.logging.log4j.core.net.Protocol; 034 import org.apache.logging.log4j.core.net.TCPSocketManager; 035 import org.apache.logging.log4j.util.EnglishEnums; 036 037 /** 038 * An Appender that delivers events over socket connections. Supports both TCP and UDP. 039 */ 040 @Plugin(name = "Socket", type = "Core", elementType = "appender", printObject = true) 041 public class SocketAppender extends AbstractOutputStreamAppender { 042 private Object advertisement; 043 private final Advertiser advertiser; 044 045 protected SocketAppender(final String name, final Layout layout, final Filter filter, 046 final AbstractSocketManager manager, final boolean handleException, 047 final boolean immediateFlush, Advertiser advertiser) { 048 super(name, layout, filter, handleException, immediateFlush, manager); 049 if (advertiser != null) 050 { 051 Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat()); 052 configuration.putAll(manager.getContentFormat()); 053 configuration.put("contentType", layout.getContentType()); 054 configuration.put("name", name); 055 advertisement = advertiser.advertise(configuration); 056 } 057 this.advertiser = advertiser; 058 } 059 060 @Override 061 public void stop() { 062 super.stop(); 063 if (advertiser != null) { 064 advertiser.unadvertise(advertisement); 065 } 066 } 067 068 /** 069 * 070 * @param host The name of the host to connect to. 071 * @param portNum The port to connect to on the target host. 072 * @param protocol The Protocol to use. 073 * @param delay The interval in which failed writes should be retried. 074 * @param immediateFail True if the write should fail if no socket is immediately available. 075 * @param name The name of the Appender. 076 * @param immediateFlush "true" if data should be flushed on each write. 077 * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise. 078 * The default is "true". 079 * @param layout The layout to use (defaults to SerializedLayout). 080 * @param filter The Filter or null. 081 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise. 082 * @param config The Configuration 083 * @return A SocketAppender. 084 */ 085 @PluginFactory 086 public static SocketAppender createAppender(@PluginAttr("host") final String host, 087 @PluginAttr("port") final String portNum, 088 @PluginAttr("protocol") final String protocol, 089 @PluginAttr("reconnectionDelay") final String delay, 090 @PluginAttr("immediateFail") final String immediateFail, 091 @PluginAttr("name") final String name, 092 @PluginAttr("immediateFlush") final String immediateFlush, 093 @PluginAttr("suppressExceptions") final String suppress, 094 @PluginElement("layout") Layout layout, 095 @PluginElement("filters") final Filter filter, 096 @PluginAttr("advertise") final String advertise, 097 @PluginConfiguration final Configuration config) { 098 099 final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush); 100 boolean isAdvertise = advertise == null ? false : Boolean.valueOf(advertise); 101 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 102 final boolean fail = immediateFail == null ? true : Boolean.valueOf(immediateFail); 103 final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay); 104 final int port = portNum == null ? 0 : Integer.parseInt(portNum); 105 if (layout == null) { 106 layout = SerializedLayout.createLayout(); 107 } 108 109 if (name == null) { 110 LOGGER.error("No name provided for SocketAppender"); 111 return null; 112 } 113 114 final String prot = protocol != null ? protocol : Protocol.TCP.name(); 115 116 final AbstractSocketManager manager = createSocketManager(prot, host, port, reconnectDelay, fail); 117 if (manager == null) { 118 return null; 119 } 120 121 return new SocketAppender(name, layout, filter, manager, handleExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null); 122 } 123 124 protected static AbstractSocketManager createSocketManager(final String protocol, final String host, final int port, 125 final int delay, final boolean immediateFail) { 126 final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol); 127 switch (p) { 128 case TCP: 129 return TCPSocketManager.getSocketManager(host, port, delay, immediateFail); 130 case UDP: 131 return DatagramSocketManager.getSocketManager(host, port); 132 default: 133 return null; 134 } 135 } 136 }