View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.db.jpa;
18  
19  import java.lang.reflect.Constructor;
20  
21  import org.apache.logging.log4j.core.Filter;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.appender.AbstractAppender;
24  import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginElement;
28  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29  import org.apache.logging.log4j.core.util.Booleans;
30  import org.apache.logging.log4j.core.util.Loader;
31  import org.apache.logging.log4j.util.Strings;
32  
33  /**
34   * This Appender writes logging events to a relational database using the Java Persistence API. It requires a
35   * pre-configured JPA persistence unit and a concrete implementation of the abstract
36   * {@link AbstractLogEventWrapperEntity} class decorated with JPA annotations.
37   *
38   * @see AbstractLogEventWrapperEntity
39   */
40  @Plugin(name = "JPA", category = "Core", elementType = "appender", printObject = true)
41  public final class JpaAppender extends AbstractDatabaseAppender<JpaDatabaseManager> {
42  
43      private final String description;
44  
45      private JpaAppender(final String name, final Filter filter, final boolean ignoreExceptions,
46              final JpaDatabaseManager manager) {
47          super(name, filter, ignoreExceptions, manager);
48          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
49      }
50  
51      @Override
52      public String toString() {
53          return this.description;
54      }
55  
56      /**
57       * Factory method for creating a JPA appender within the plugin manager.
58       *
59       * @param name The name of the appender.
60       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
61       *               they are propagated to the caller.
62       * @param filter The filter, if any, to use.
63       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
64       *                   the buffer reaches this size.
65       * @param entityClassName The fully qualified name of the concrete {@link AbstractLogEventWrapperEntity}
66       *                        implementation that has JPA annotations mapping it to a database table.
67       * @param persistenceUnitName The name of the JPA persistence unit that should be used for persisting log events.
68       * @return a new JPA appender.
69       */
70      @PluginFactory
71      public static JpaAppender createAppender(
72              @PluginAttribute("name") final String name,
73              @PluginAttribute("ignoreExceptions") final String ignore,
74              @PluginElement("Filter") final Filter filter,
75              @PluginAttribute("bufferSize") final String bufferSize,
76              @PluginAttribute("entityClassName") final String entityClassName,
77              @PluginAttribute("persistenceUnitName") final String persistenceUnitName) {
78          if (Strings.isEmpty(entityClassName) || Strings.isEmpty(persistenceUnitName)) {
79              LOGGER.error("Attributes entityClassName and persistenceUnitName are required for JPA Appender.");
80              return null;
81          }
82  
83          final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
84          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
85  
86          try {
87              final Class<? extends AbstractLogEventWrapperEntity> entityClass =
88                  Loader.loadClass(entityClassName).asSubclass(AbstractLogEventWrapperEntity.class);
89  
90              try {
91                  entityClass.getConstructor();
92              } catch (final NoSuchMethodException e) {
93                  LOGGER.error("Entity class [{}] does not have a no-arg constructor. The JPA provider will reject it.",
94                          entityClassName);
95                  return null;
96              }
97  
98              final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor =
99                      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 }