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.db.jpa; 018 019import java.lang.reflect.Constructor; 020 021import org.apache.logging.log4j.core.Filter; 022import org.apache.logging.log4j.core.LogEvent; 023import org.apache.logging.log4j.core.appender.AbstractAppender; 024import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender; 025import org.apache.logging.log4j.core.config.plugins.Plugin; 026import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 027import org.apache.logging.log4j.core.config.plugins.PluginElement; 028import org.apache.logging.log4j.core.config.plugins.PluginFactory; 029import org.apache.logging.log4j.core.util.Booleans; 030import org.apache.logging.log4j.core.util.Loader; 031import org.apache.logging.log4j.util.Strings; 032 033/** 034 * This Appender writes logging events to a relational database using the Java Persistence API. It requires a 035 * pre-configured JPA persistence unit and a concrete implementation of the abstract 036 * {@link AbstractLogEventWrapperEntity} class decorated with JPA annotations. 037 * 038 * @see AbstractLogEventWrapperEntity 039 */ 040@Plugin(name = "JPA", category = "Core", elementType = "appender", printObject = true) 041public final class JpaAppender extends AbstractDatabaseAppender<JpaDatabaseManager> { 042 private static final long serialVersionUID = 1L; 043 044 private final String description; 045 046 private JpaAppender(final String name, final Filter filter, final boolean ignoreExceptions, 047 final JpaDatabaseManager manager) { 048 super(name, filter, ignoreExceptions, manager); 049 this.description = this.getName() + "{ manager=" + this.getManager() + " }"; 050 } 051 052 @Override 053 public String toString() { 054 return this.description; 055 } 056 057 /** 058 * Factory method for creating a JPA appender within the plugin manager. 059 * 060 * @param name The name of the appender. 061 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 062 * they are propagated to the caller. 063 * @param filter The filter, if any, to use. 064 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever 065 * the buffer reaches this size. 066 * @param entityClassName The fully qualified name of the concrete {@link AbstractLogEventWrapperEntity} 067 * implementation that has JPA annotations mapping it to a database table. 068 * @param persistenceUnitName The name of the JPA persistence unit that should be used for persisting log events. 069 * @return a new JPA appender. 070 */ 071 @PluginFactory 072 public static JpaAppender createAppender( 073 @PluginAttribute("name") final String name, 074 @PluginAttribute("ignoreExceptions") final String ignore, 075 @PluginElement("Filter") final Filter filter, 076 @PluginAttribute("bufferSize") final String bufferSize, 077 @PluginAttribute("entityClassName") final String entityClassName, 078 @PluginAttribute("persistenceUnitName") final String persistenceUnitName) { 079 if (Strings.isEmpty(entityClassName) || Strings.isEmpty(persistenceUnitName)) { 080 LOGGER.error("Attributes entityClassName and persistenceUnitName are required for JPA Appender."); 081 return null; 082 } 083 084 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); 085 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 086 087 try { 088 @SuppressWarnings("unchecked") 089 final Class<? extends AbstractLogEventWrapperEntity> entityClass = 090 (Class<? extends AbstractLogEventWrapperEntity>) Loader.loadClass(entityClassName); 091 092 if (!AbstractLogEventWrapperEntity.class.isAssignableFrom(entityClass)) { 093 LOGGER.error("Entity class [{}] does not extend AbstractLogEventWrapperEntity.", entityClassName); 094 return null; 095 } 096 097 try { 098 entityClass.getConstructor(); 099 } catch (final NoSuchMethodException e) { 100 LOGGER.error("Entity class [{}] does not have a no-arg constructor. The JPA provider will reject it.", 101 entityClassName); 102 return null; 103 } 104 105 final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor = 106 entityClass.getConstructor(LogEvent.class); 107 108 final String managerName = "jpaManager{ description=" + name + ", bufferSize=" + bufferSizeInt 109 + ", persistenceUnitName=" + persistenceUnitName + ", entityClass=" + entityClass.getName() + '}'; 110 111 final JpaDatabaseManager manager = JpaDatabaseManager.getJPADatabaseManager( 112 managerName, bufferSizeInt, entityClass, entityConstructor, persistenceUnitName 113 ); 114 if (manager == null) { 115 return null; 116 } 117 118 return new JpaAppender(name, filter, ignoreExceptions, manager); 119 } catch (final ClassNotFoundException e) { 120 LOGGER.error("Could not load entity class [{}].", entityClassName, e); 121 return null; 122 } catch (final NoSuchMethodException e) { 123 LOGGER.error("Entity class [{}] does not have a constructor with a single argument of type LogEvent.", 124 entityClassName); 125 return null; 126 } 127 } 128}