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