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.nosql.appender.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.nosql.appender.AbstractNoSqlConnection;
23  import org.apache.logging.log4j.nosql.appender.NoSqlConnection;
24  import org.apache.logging.log4j.nosql.appender.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          this.collection = database.getCollection(collectionName);
59          this.writeConcern = writeConcern;
60      }
61  
62      @Override
63      public MongoDbObject createObject() {
64          return new MongoDbObject();
65      }
66  
67      @Override
68      public MongoDbObject[] createList(final int length) {
69          return new MongoDbObject[length];
70      }
71  
72      @Override
73      public void insertObject(final NoSqlObject<BasicDBObject> object) {
74          try {
75              this.collection.insert(object.unwrap(), this.writeConcern);
76          } catch (final MongoException e) {
77              throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
78                      e);
79          }
80      }
81  
82      @Override
83      public void closeImpl() {
84          // there's no need to call this.mongo.close() since that literally closes the connection.
85          // MongoDBClient uses internal connection pooling.
86          // For more details, see LOG4J2-591.
87      }
88  
89  }