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 043 private final String description; 044 045 private JpaAppender(final String name, final Filter filter, final boolean ignoreExceptions, 046 final JpaDatabaseManager manager) { 047 super(name, filter, ignoreExceptions, manager); 048 this.description = this.getName() + "{ manager=" + this.getManager() + " }"; 049 } 050 051 @Override 052 public String toString() { 053 return this.description; 054 } 055 056 /** 057 * Factory method for creating a JPA appender within the plugin manager. 058 * 059 * @param name The name of the appender. 060 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 061 * they are propagated to the caller. 062 * @param filter The filter, if any, to use. 063 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever 064 * the buffer reaches this size. 065 * @param entityClassName The fully qualified name of the concrete {@link AbstractLogEventWrapperEntity} 066 * implementation that has JPA annotations mapping it to a database table. 067 * @param persistenceUnitName The name of the JPA persistence unit that should be used for persisting log events. 068 * @return a new JPA appender. 069 */ 070 @PluginFactory 071 public static JpaAppender createAppender( 072 @PluginAttribute("name") final String name, 073 @PluginAttribute("ignoreExceptions") final String ignore, 074 @PluginElement("Filter") final Filter filter, 075 @PluginAttribute("bufferSize") final String bufferSize, 076 @PluginAttribute("entityClassName") final String entityClassName, 077 @PluginAttribute("persistenceUnitName") final String persistenceUnitName) { 078 if (Strings.isEmpty(entityClassName) || Strings.isEmpty(persistenceUnitName)) { 079 LOGGER.error("Attributes entityClassName and persistenceUnitName are required for JPA Appender."); 080 return null; 081 } 082 083 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); 084 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 085 086 try { 087 final Class<? extends AbstractLogEventWrapperEntity> entityClass = 088 Loader.loadClass(entityClassName).asSubclass(AbstractLogEventWrapperEntity.class); 089 090 try { 091 entityClass.getConstructor(); 092 } catch (final NoSuchMethodException e) { 093 LOGGER.error("Entity class [{}] does not have a no-arg constructor. The JPA provider will reject it.", 094 entityClassName); 095 return null; 096 } 097 098 final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor = 099 entityClass.getConstructor(LogEvent.class); 100 101 final String managerName = "jpaManager{ description=" + name + ", bufferSize=" + bufferSizeInt 102 + ", persistenceUnitName=" + persistenceUnitName + ", entityClass=" + entityClass.getName() + '}'; 103 104 final JpaDatabaseManager manager = JpaDatabaseManager.getJPADatabaseManager( 105 managerName, bufferSizeInt, entityClass, entityConstructor, persistenceUnitName 106 ); 107 if (manager == null) { 108 return null; 109 } 110 111 return new JpaAppender(name, filter, ignoreExceptions, manager); 112 } catch (final ClassNotFoundException e) { 113 LOGGER.error("Could not load entity class [{}].", entityClassName, e); 114 return null; 115 } catch (final NoSuchMethodException e) { 116 LOGGER.error("Entity class [{}] does not have a constructor with a single argument of type LogEvent.", 117 entityClassName); 118 return null; 119 } catch (final ClassCastException e) { 120 LOGGER.error("Entity class [{}] does not extend AbstractLogEventWrapperEntity.", entityClassName); 121 return null; 122 } 123 } 124}