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.mongodb;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.core.appender.AppenderLoggingException;
22  import org.apache.logging.log4j.core.appender.nosql.AbstractNoSqlConnection;
23  import org.apache.logging.log4j.core.appender.nosql.NoSqlConnection;
24  import org.apache.logging.log4j.core.appender.nosql.NoSqlObject;
25  import org.apache.logging.log4j.status.StatusLogger;
26  import org.bson.BSON;
27  import org.bson.Transformer;
28  
29  import com.mongodb.BasicDBObject;
30  import com.mongodb.DB;
31  import com.mongodb.DBCollection;
32  import com.mongodb.MongoException;
33  import com.mongodb.WriteConcern;
34  
35  /**
36   * The MongoDB implementation of {@link NoSqlConnection}.
37   */
38  public final class MongoDbConnection extends AbstractNoSqlConnection<BasicDBObject, MongoDbObject> {
39  
40      private static final Logger LOGGER = StatusLogger.getLogger();
41  
42      static {
43          BSON.addEncodingHook(Level.class, new Transformer() {
44              @Override
45              public Object transform(final Object o) {
46                  if (o instanceof Level) {
47                      return ((Level) o).name();
48                  }
49                  return o;
50              }
51          });
52      }
53  
54      private final DBCollection collection;
55      private final WriteConcern writeConcern;
56  
57      public MongoDbConnection(final DB database, final WriteConcern writeConcern, final String collectionName,
58              final Boolean isCapped, final Integer collectionSize) {
59          if (database.collectionExists(collectionName)) {
60              collection = database.getCollection(collectionName);
61          } else {
62              final BasicDBObject options = new BasicDBObject();
63              options.put("capped", isCapped);
64              options.put("size", collectionSize);
65              this.collection = database.createCollection(collectionName, options);
66          }
67          this.writeConcern = writeConcern;
68      }
69  
70      @Override
71      public MongoDbObject createObject() {
72          return new MongoDbObject();
73      }
74  
75      @Override
76      public MongoDbObject[] createList(final int length) {
77          return new MongoDbObject[length];
78      }
79  
80      @Override
81      public void insertObject(final NoSqlObject<BasicDBObject> object) {
82          try {
83              this.collection.insert(object.unwrap(), this.writeConcern);
84          } catch (final MongoException e) {
85              throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
86                      e);
87          }
88      }
89  
90      @Override
91      public void closeImpl() {
92          // LOG4J2-1196
93          this.collection.getDB().getMongo().close();
94      }
95  
96  }