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.nosql;
18  
19  import org.apache.logging.log4j.Marker;
20  import org.apache.logging.log4j.ThreadContext;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.appender.AppenderLoggingException;
23  import org.apache.logging.log4j.core.appender.ManagerFactory;
24  import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
25  import org.apache.logging.log4j.core.util.Closer;
26  import org.apache.logging.log4j.util.BiConsumer;
27  import org.apache.logging.log4j.util.ReadOnlyStringMap;
28  
29  /**
30   * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
31   *
32   * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}.
33   */
34  public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager {
35      private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
36  
37      private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider;
38  
39      private NoSqlConnection<W, ? extends NoSqlObject<W>> connection;
40  
41      private NoSqlDatabaseManager(final String name, final int bufferSize,
42              final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider) {
43          super(name, bufferSize);
44          this.provider = provider;
45      }
46  
47      @Override
48      protected void startupInternal() {
49          // nothing to see here
50      }
51  
52      @Override
53      protected boolean shutdownInternal() {
54          // NoSQL doesn't use transactions, so all we need to do here is simply close the client
55          return Closer.closeSilently(this.connection);
56      }
57  
58      @Override
59      protected void connectAndStart() {
60          try {
61              this.connection = this.provider.getConnection();
62          } catch (final Exception e) {
63              throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e);
64          }
65      }
66  
67      @Override
68      protected void writeInternal(final LogEvent event) {
69          if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
70              throw new AppenderLoggingException(
71                      "Cannot write logging event; NoSQL manager not connected to the database.");
72          }
73  
74          final NoSqlObject<W> entity = this.connection.createObject();
75          entity.set("level", event.getLevel());
76          entity.set("loggerName", event.getLoggerName());
77          entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
78  
79          final StackTraceElement source = event.getSource();
80          if (source == null) {
81              entity.set("source", (Object) null);
82          } else {
83              entity.set("source", this.convertStackTraceElement(source));
84          }
85  
86          final Marker marker = event.getMarker();
87          if (marker == null) {
88              entity.set("marker", (Object) null);
89          } else {
90              entity.set("marker", buildMarkerEntity(marker));
91          }
92  
93          entity.set("threadId", event.getThreadId());
94          entity.set("threadName", event.getThreadName());
95          entity.set("threadPriority", event.getThreadPriority());
96          entity.set("millis", event.getTimeMillis());
97          entity.set("date", new java.util.Date(event.getTimeMillis()));
98  
99          @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
100         Throwable thrown = event.getThrown();
101         if (thrown == null) {
102             entity.set("thrown", (Object) null);
103         } else {
104             final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
105             NoSqlObject<W> exceptionEntity = originalExceptionEntity;
106             exceptionEntity.set("type", thrown.getClass().getName());
107             exceptionEntity.set("message", thrown.getMessage());
108             exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
109             while (thrown.getCause() != null) {
110                 thrown = thrown.getCause();
111                 final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
112                 causingExceptionEntity.set("type", thrown.getClass().getName());
113                 causingExceptionEntity.set("message", thrown.getMessage());
114                 causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
115                 exceptionEntity.set("cause", causingExceptionEntity);
116                 exceptionEntity = causingExceptionEntity;
117             }
118 
119             entity.set("thrown", originalExceptionEntity);
120         }
121 
122         final ReadOnlyStringMap contextMap = event.getContextData();
123         if (contextMap == null) {
124             entity.set("contextMap", (Object) null);
125         } else {
126             final NoSqlObject<W> contextMapEntity = this.connection.createObject();
127             contextMap.forEach(new BiConsumer<String, String>() {
128                 @Override
129                 public void accept(final String key, final String val) {
130                     contextMapEntity.set(key, val);
131                 }
132             });
133             entity.set("contextMap", contextMapEntity);
134         }
135 
136         final ThreadContext.ContextStack contextStack = event.getContextStack();
137         if (contextStack == null) {
138             entity.set("contextStack", (Object) null);
139         } else {
140             entity.set("contextStack", contextStack.asList().toArray());
141         }
142 
143         this.connection.insertObject(entity);
144     }
145 
146     private NoSqlObject<W> buildMarkerEntity(final Marker marker) {
147         final NoSqlObject<W> entity = this.connection.createObject();
148         entity.set("name", marker.getName());
149 
150         final Marker[] parents = marker.getParents();
151         if (parents != null) {
152             @SuppressWarnings("unchecked")
153             final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length];
154             for (int i = 0; i < parents.length; i++) {
155                 parentEntities[i] = buildMarkerEntity(parents[i]);
156             }
157             entity.set("parents", parentEntities);
158         }
159         return entity;
160     }
161 
162     @Override
163     protected boolean commitAndClose() {
164         // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions).
165         // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections.
166         // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL.
167         // see LOG4J2-591 and LOG4J2-676
168     	return true;
169     }
170 
171     private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
172         final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
173         for (int i = 0; i < stackTrace.length; i++) {
174             stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
175         }
176         return stackTraceEntities;
177     }
178 
179     private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) {
180         final NoSqlObject<W> elementEntity = this.connection.createObject();
181         elementEntity.set("className", element.getClassName());
182         elementEntity.set("methodName", element.getMethodName());
183         elementEntity.set("fileName", element.getFileName());
184         elementEntity.set("lineNumber", element.getLineNumber());
185         return elementEntity;
186     }
187 
188     /**
189      * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists.
190      *
191      * @param name The name of the manager, which should include connection details and hashed passwords where possible.
192      * @param bufferSize The size of the log event buffer.
193      * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
194      * @return a new or existing NoSQL manager as applicable.
195      */
196     public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize,
197                                                                   final NoSqlProvider<?> provider) {
198         return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
199     }
200 
201     /**
202      * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
203      */
204     private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
205         private final NoSqlProvider<?> provider;
206 
207         protected FactoryData(final int bufferSize, final NoSqlProvider<?> provider) {
208             super(bufferSize);
209             this.provider = provider;
210         }
211     }
212 
213     /**
214      * Creates managers.
215      */
216     private static final class NoSQLDatabaseManagerFactory implements
217             ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> {
218         @Override
219         @SuppressWarnings("unchecked")
220         public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) {
221             return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider);
222         }
223     }
224 }