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.mongodb3; 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.core.appender.nosql.AbstractNoSqlConnection; 023import org.apache.logging.log4j.core.appender.nosql.NoSqlConnection; 024import org.apache.logging.log4j.core.appender.nosql.NoSqlObject; 025import org.apache.logging.log4j.status.StatusLogger; 026import org.bson.BSON; 027import org.bson.Document; 028import org.bson.Transformer; 029 030import com.mongodb.MongoClient; 031import com.mongodb.MongoException; 032import com.mongodb.client.MongoCollection; 033import com.mongodb.client.MongoDatabase; 034import com.mongodb.client.model.CreateCollectionOptions; 035 036/** 037 * The MongoDB implementation of {@link NoSqlConnection}. 038 */ 039public final class MongoDbConnection extends AbstractNoSqlConnection<Document, MongoDbDocumentObject> { 040 041 private static final Logger LOGGER = StatusLogger.getLogger(); 042 043 static { 044 BSON.addEncodingHook(Level.class, new Transformer() { 045 @Override 046 public Object transform(final Object o) { 047 if (o instanceof Level) { 048 return ((Level) o).name(); 049 } 050 return o; 051 } 052 }); 053 } 054 055 private static MongoCollection<Document> getOrCreateMongoCollection(final MongoDatabase database, 056 final String collectionName, final boolean isCapped, final Integer sizeInBytes) { 057 try { 058 LOGGER.debug("Gettting collection '{}'...", collectionName); 059 // throws IllegalArgumentException if collectionName is invalid 060 return database.getCollection(collectionName); 061 } catch (final IllegalStateException e) { 062 LOGGER.debug("Collection '{}' does not exist.", collectionName); 063 final CreateCollectionOptions options = new CreateCollectionOptions() 064 // @formatter:off 065 .capped(isCapped) 066 .sizeInBytes(sizeInBytes); 067 // @formatter:on 068 LOGGER.debug("Creating collection {} (capped = {}, sizeInBytes = {})", collectionName, isCapped, 069 sizeInBytes); 070 database.createCollection(collectionName, options); 071 return database.getCollection(collectionName); 072 } 073 074 } 075 076 private final MongoCollection<Document> collection; 077 private final MongoClient mongoClient; 078 079 public MongoDbConnection(final MongoClient mongoClient, final MongoDatabase mongoDatabase, 080 final String collectionName, final boolean isCapped, final Integer sizeInBytes) { 081 this.mongoClient = mongoClient; 082 this.collection = getOrCreateMongoCollection(mongoDatabase, collectionName, isCapped, sizeInBytes); 083 } 084 085 @Override 086 public void closeImpl() { 087 // LOG4J2-1196 088 mongoClient.close(); 089 } 090 091 @Override 092 public MongoDbDocumentObject[] createList(final int length) { 093 return new MongoDbDocumentObject[length]; 094 } 095 096 @Override 097 public MongoDbDocumentObject createObject() { 098 return new MongoDbDocumentObject(); 099 } 100 101 @Override 102 public void insertObject(final NoSqlObject<Document> object) { 103 try { 104 final Document unwrapped = object.unwrap(); 105 LOGGER.debug("Inserting object {}", unwrapped); 106 this.collection.insertOne(unwrapped); 107 } catch (final MongoException e) { 108 throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(), 109 e); 110 } 111 } 112 113}