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.jms; 018 019 import java.io.Serializable; 020 021 import org.apache.logging.log4j.core.Filter; 022 import org.apache.logging.log4j.core.Layout; 023 import org.apache.logging.log4j.core.LogEvent; 024 import org.apache.logging.log4j.core.appender.AbstractAppender; 025 import org.apache.logging.log4j.core.appender.AppenderLoggingException; 026 import org.apache.logging.log4j.core.config.plugins.Plugin; 027 import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 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.SerializedLayout; 031 import org.apache.logging.log4j.core.net.jms.JmsQueueManager; 032 import org.apache.logging.log4j.core.util.Booleans; 033 034 /** 035 * Appender to write to a JMS Queue. 036 */ 037 @Plugin(name = "JMSQueue", category = "Core", elementType = "appender", printObject = true) 038 public final class JmsQueueAppender extends AbstractAppender { 039 040 private final JmsQueueManager manager; 041 042 private JmsQueueAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, 043 final JmsQueueManager manager, final boolean ignoreExceptions) { 044 super(name, filter, layout, ignoreExceptions); 045 this.manager = manager; 046 } 047 048 /** 049 * Actual writing occurs here. 050 * 051 * @param event The LogEvent. 052 */ 053 @Override 054 public void append(final LogEvent event) { 055 try { 056 manager.send(getLayout().toSerializable(event)); 057 } catch (final Exception ex) { 058 throw new AppenderLoggingException(ex); 059 } 060 } 061 062 /** 063 * Create a JmsQueueAppender. 064 * @param name The name of the Appender. 065 * @param factoryName The fully qualified class name of the InitialContextFactory. 066 * @param providerURL The URL of the provider to use. 067 * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that 068 * will create a URL context factory 069 * @param securityPrincipalName The name of the identity of the Principal. 070 * @param securityCredentials The security credentials of the Principal. 071 * @param factoryBindingName The name to locate in the Context that provides the QueueConnectionFactory. 072 * @param queueBindingName The name to use to locate the Queue. 073 * @param userName The user ID to use to create the Queue Connection. 074 * @param password The password to use to create the Queue Connection. 075 * @param layout The layout to use (defaults to SerializedLayout). 076 * @param filter The Filter or null. 077 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 078 * they are propagated to the caller. 079 * @return The JmsQueueAppender. 080 */ 081 @PluginFactory 082 public static JmsQueueAppender createAppender( 083 @PluginAttribute("name") final String name, 084 @PluginAttribute("factoryName") final String factoryName, 085 @PluginAttribute("providerURL") final String providerURL, 086 @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes, 087 @PluginAttribute("securityPrincipalName") final String securityPrincipalName, 088 @PluginAttribute("securityCredentials") final String securityCredentials, 089 @PluginAttribute("factoryBindingName") final String factoryBindingName, 090 @PluginAttribute("queueBindingName") final String queueBindingName, 091 @PluginAttribute("userName") final String userName, 092 @PluginAttribute("password") final String password, 093 @PluginElement("Layout") Layout<? extends Serializable> layout, 094 @PluginElement("Filter") final Filter filter, 095 @PluginAttribute("ignoreExceptions") final String ignore) { 096 if (name == null) { 097 LOGGER.error("No name provided for JmsQueueAppender"); 098 return null; 099 } 100 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 101 final JmsQueueManager manager = JmsQueueManager.getJmsQueueManager(factoryName, providerURL, urlPkgPrefixes, 102 securityPrincipalName, securityCredentials, factoryBindingName, queueBindingName, userName, password); 103 if (manager == null) { 104 return null; 105 } 106 if (layout == null) { 107 layout = SerializedLayout.createLayout(); 108 } 109 return new JmsQueueAppender(name, filter, layout, manager, ignoreExceptions); 110 } 111 }