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