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.plugins.Plugin; 022 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 023 import org.apache.logging.log4j.core.config.plugins.PluginElement; 024 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 025 import org.apache.logging.log4j.core.layout.SerializedLayout; 026 import org.apache.logging.log4j.core.net.AbstractSocketManager; 027 import org.apache.logging.log4j.core.net.DatagramSocketManager; 028 import org.apache.logging.log4j.core.net.Protocol; 029 import org.apache.logging.log4j.core.net.TCPSocketManager; 030 import org.apache.logging.log4j.util.EnglishEnums; 031 032 /** 033 * An Appender that delivers events over socket connections. Supports both TCP and UDP. 034 */ 035 @Plugin(name = "Socket", type = "Core", elementType = "appender", printObject = true) 036 public class SocketAppender extends AbstractOutputStreamAppender { 037 038 039 protected SocketAppender(final String name, final Layout layout, final Filter filter, 040 final AbstractSocketManager manager, final boolean handleException, 041 final boolean immediateFlush) { 042 super(name, layout, filter, handleException, immediateFlush, manager); 043 044 } 045 046 /** 047 * 048 * @param host The name of the host to connect to. 049 * @param portNum The port to connect to on the target host. 050 * @param protocol The Protocol to use. 051 * @param delay The interval in which failed writes should be retried. 052 * @param name The name of the Appender. 053 * @param immediateFlush "true" if data should be flushed on each write. 054 * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise. 055 * The default is "true". 056 * @param layout The layout to use (defaults to SerializedLayout). 057 * @param filter The Filter or null. 058 * @return A SocketAppender. 059 */ 060 @PluginFactory 061 public static SocketAppender createAppender(@PluginAttr("host") final String host, 062 @PluginAttr("port") final String portNum, 063 @PluginAttr("protocol") final String protocol, 064 @PluginAttr("reconnectionDelay") final String delay, 065 @PluginAttr("name") final String name, 066 @PluginAttr("immediateFlush") final String immediateFlush, 067 @PluginAttr("suppressExceptions") final String suppress, 068 @PluginElement("layout") Layout layout, 069 @PluginElement("filters") final Filter filter) { 070 071 final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush); 072 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 073 final int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay); 074 final int port = portNum == null ? 0 : Integer.parseInt(portNum); 075 if (layout == null) { 076 layout = SerializedLayout.createLayout(); 077 } 078 079 if (name == null) { 080 LOGGER.error("No name provided for SocketAppender"); 081 return null; 082 } 083 084 final String prot = protocol != null ? protocol : Protocol.TCP.name(); 085 086 final AbstractSocketManager manager = createSocketManager(prot, host, port, reconnectDelay); 087 if (manager == null) { 088 return null; 089 } 090 return new SocketAppender(name, layout, filter, manager, handleExceptions, isFlush); 091 } 092 093 protected static AbstractSocketManager createSocketManager(final String protocol, final String host, final int port, 094 final int delay) { 095 final Protocol p = EnglishEnums.valueOf(Protocol.class, protocol); 096 switch (p) { 097 case TCP: 098 return TCPSocketManager.getSocketManager(host, port, delay); 099 case UDP: 100 return DatagramSocketManager.getSocketManager(host, port); 101 default: 102 return null; 103 } 104 } 105 }