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 */
017package org.apache.logging.log4j.nosql.appender.mongodb;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.core.appender.AppenderLoggingException;
022import org.apache.logging.log4j.nosql.appender.AbstractNoSqlConnection;
023import org.apache.logging.log4j.nosql.appender.NoSqlConnection;
024import org.apache.logging.log4j.nosql.appender.NoSqlObject;
025import org.apache.logging.log4j.status.StatusLogger;
026import org.bson.BSON;
027import org.bson.Transformer;
028
029import com.mongodb.BasicDBObject;
030import com.mongodb.DB;
031import com.mongodb.DBCollection;
032import com.mongodb.MongoException;
033import com.mongodb.WriteConcern;
034
035/**
036 * The MongoDB implementation of {@link NoSqlConnection}.
037 */
038public final class MongoDbConnection extends AbstractNoSqlConnection<BasicDBObject, MongoDbObject> {
039
040    private static final Logger LOGGER = StatusLogger.getLogger();
041
042    static {
043        BSON.addEncodingHook(Level.class, new Transformer() {
044            @Override
045            public Object transform(final Object o) {
046                if (o instanceof Level) {
047                    return ((Level) o).name();
048                }
049                return o;
050            }
051        });
052    }
053
054    private final DBCollection collection;
055    private final WriteConcern writeConcern;
056
057    public MongoDbConnection(final DB database, final WriteConcern writeConcern, final String collectionName) {
058        this.collection = database.getCollection(collectionName);
059        this.writeConcern = writeConcern;
060    }
061
062    @Override
063    public MongoDbObject createObject() {
064        return new MongoDbObject();
065    }
066
067    @Override
068    public MongoDbObject[] createList(final int length) {
069        return new MongoDbObject[length];
070    }
071
072    @Override
073    public void insertObject(final NoSqlObject<BasicDBObject> object) {
074        try {
075            this.collection.insert(object.unwrap(), this.writeConcern);
076        } catch (final MongoException e) {
077            throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
078                    e);
079        }
080    }
081
082    @Override
083    public void closeImpl() {
084        // LOG4J2-1196
085        this.collection.getDB().getMongo().close();
086    }
087
088}