Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collectionName and databaseName attributes to MongoDbProvider #3322

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ public static class Builder<B extends Builder<B>> extends AbstractFilterable.Bui
@PluginAttribute("capped")
private boolean capped = false;

@PluginAttribute("collectionName")
private String collectionName = null;

@PluginAttribute("datbaseName")
private String databaseName = null;

@Override
public MongoDbProvider build() {
return new MongoDbProvider(connectionStringSource, capped, collectionSize);
return new MongoDbProvider(connectionStringSource, capped, collectionSize, databaseName, collectionName);
}

public B setConnectionStringSource(final String connectionStringSource) {
Expand All @@ -73,6 +79,16 @@ public B setCollectionSize(final long collectionSize) {
this.collectionSize = collectionSize;
return asBuilder();
}

public B setCollectionName(final String collectionName) {
this.collectionName = collectionName;
return asBuilder();
}

public B setDatabaseName(final String databaseName) {
this.databaseName = databaseName;
return asBuilder();
}
}

private static final Logger LOGGER = StatusLogger.getLogger();
Expand All @@ -94,11 +110,18 @@ public static <B extends Builder<B>> B newBuilder() {

private final Long collectionSize;
private final boolean isCapped;
private final String collectionName;
private final String databaseName;
private final MongoClient mongoClient;
private final MongoDatabase mongoDatabase;
private final ConnectionString connectionString;

private MongoDbProvider(final String connectionStringSource, final boolean isCapped, final Long collectionSize) {
private MongoDbProvider(
final String connectionStringSource,
final boolean isCapped,
final Long collectionSize,
final String databaseName,
final String collectionName) {
LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
this.connectionString = new ConnectionString(connectionStringSource);
LOGGER.debug("Created ConnectionString {}", connectionString);
Expand All @@ -113,10 +136,19 @@ private MongoDbProvider(final String connectionStringSource, final boolean isCap
LOGGER.debug("Creating MongoClient {}...", settings);
this.mongoClient = MongoClients.create(settings);
LOGGER.debug("Created MongoClient {}", mongoClient);
final String databaseName = this.connectionString.getDatabase();
LOGGER.debug("Getting MongoDatabase {}...", databaseName);
this.mongoDatabase = this.mongoClient.getDatabase(databaseName);
if (databaseName == null || databaseName.isEmpty()) {
this.databaseName = this.connectionString.getDatabase();
} else {
this.databaseName = databaseName;
}
LOGGER.debug("Getting MongoDatabase {}...", this.databaseName);
this.mongoDatabase = this.mongoClient.getDatabase(this.databaseName);
LOGGER.debug("Got MongoDatabase {}", mongoDatabase);
if (collectionName == null || collectionName.isEmpty()) {
this.collectionName = this.connectionString.getCollection();
} else {
this.collectionName = collectionName;
}
this.isCapped = isCapped;
this.collectionSize = collectionSize;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.mongodb;

import com.mongodb.client.MongoClient;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("MongoDbCollectionNameIT.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbCollectionNameIT extends AbstractMongoDbCappedIT {

@Test
@Override
protected void test(LoggerContext ctx, MongoClient mongoClient) {
super.test(ctx, mongoClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.mongodb;

import com.mongodb.client.MongoClient;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("MongoDbDatabaseAndCollectionNameIT.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbDatabaseAndCollectionNameIT extends AbstractMongoDbCappedIT {

@Test
@Override
protected void test(LoggerContext ctx, MongoClient mongoClient) {
super.test(ctx, mongoClient);
}
}
37 changes: 37 additions & 0 deletions log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
<Appenders>
<NoSql name="MONGO">
<MongoDb
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb"
capped="true"
collectionSize="1073741824"
collectionName="MongoDBCollectionNameIT"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="MONGO" />
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
<Appenders>
<NoSql name="MONGO">
<MongoDb
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
capped="true"
collectionSize="1073741824"
databaseName="testDb"
collectionName="MongoDbDatabaseAndCollectionNameIT"
/>
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="MONGO" />
</Root>
</Loggers>
</Configuration>
8 changes: 8 additions & 0 deletions src/changelog/.3.x.x/update_org_log4j_mongodb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="updated">
<issue id="3321" link="https://github.com/apache/logging-log4j2/pull/3321"/>
<description format="asciidoc">Update log4j-mongodb to allow collectionName and databaseName to be defined via the config file</description>
</entry>
Loading