Skip to content

Commit 91a0db4

Browse files
committed
Added ability to configure database and collection name via the config file instead of just the connectionString
1 parent 7a1b04f commit 91a0db4

File tree

5 files changed

+180
-5
lines changed

5 files changed

+180
-5
lines changed

log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ public static class Builder<B extends Builder<B>> extends AbstractFilterable.Bui
5454
@PluginAttribute("capped")
5555
private boolean capped = false;
5656

57+
@PluginAttribute("collectionName")
58+
private String collectionName = null;
59+
60+
@PluginAttribute("datbaseName")
61+
private String databaseName = null;
62+
5763
@Override
5864
public MongoDbProvider build() {
59-
return new MongoDbProvider(connectionStringSource, capped, collectionSize);
65+
return new MongoDbProvider(connectionStringSource, capped, collectionSize, databaseName, collectionName);
6066
}
6167

6268
public B setConnectionStringSource(final String connectionStringSource) {
@@ -73,6 +79,16 @@ public B setCollectionSize(final long collectionSize) {
7379
this.collectionSize = collectionSize;
7480
return asBuilder();
7581
}
82+
83+
public B setCollectionName(final String collectionName) {
84+
this.collectionName = collectionName;
85+
return asBuilder();
86+
}
87+
88+
public B setDatabaseName(final String databaseName) {
89+
this.databaseName = databaseName;
90+
return asBuilder();
91+
}
7692
}
7793

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

95111
private final Long collectionSize;
96112
private final boolean isCapped;
113+
private final String collectionName;
114+
private final String databaseName;
97115
private final MongoClient mongoClient;
98116
private final MongoDatabase mongoDatabase;
99117
private final ConnectionString connectionString;
100118

101-
private MongoDbProvider(final String connectionStringSource, final boolean isCapped, final Long collectionSize) {
119+
private MongoDbProvider(final String connectionStringSource, final boolean isCapped, final Long collectionSize, final String databaseName, final String collectionName) {
102120
LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
103121
this.connectionString = new ConnectionString(connectionStringSource);
104122
LOGGER.debug("Created ConnectionString {}", connectionString);
@@ -113,10 +131,19 @@ private MongoDbProvider(final String connectionStringSource, final boolean isCap
113131
LOGGER.debug("Creating MongoClient {}...", settings);
114132
this.mongoClient = MongoClients.create(settings);
115133
LOGGER.debug("Created MongoClient {}", mongoClient);
116-
final String databaseName = this.connectionString.getDatabase();
117-
LOGGER.debug("Getting MongoDatabase {}...", databaseName);
118-
this.mongoDatabase = this.mongoClient.getDatabase(databaseName);
134+
if (databaseName == null || databaseName.isEmpty()) {
135+
this.databaseName = this.connectionString.getDatabase();
136+
} else {
137+
this.databaseName = databaseName;
138+
}
139+
LOGGER.debug("Getting MongoDatabase {}...", this.databaseName);
140+
this.mongoDatabase = this.mongoClient.getDatabase(this.databaseName);
119141
LOGGER.debug("Got MongoDatabase {}", mongoDatabase);
142+
if (collectionName == null || collectionName.isEmpty()) {
143+
this.collectionName = this.connectionString.getCollection();
144+
} else {
145+
this.collectionName = collectionName;
146+
}
120147
this.isCapped = isCapped;
121148
this.collectionSize = collectionSize;
122149
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 com.mongodb.client.MongoClient;
20+
import org.apache.logging.log4j.core.LoggerContext;
21+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.junit.jupiter.api.Test;
24+
25+
@UsingMongoDb
26+
@LoggerContextSource("MongoDbCollectionNameIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbCollectionNameIT extends AbstractMongoDbCappedIT {
30+
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 com.mongodb.client.MongoClient;
20+
import org.apache.logging.log4j.core.LoggerContext;
21+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.junit.jupiter.api.Test;
24+
25+
@UsingMongoDb
26+
@LoggerContextSource("MongoDbDatabaseAndCollectionNameIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbDatabaseAndCollectionNameIT extends AbstractMongoDbCappedIT {
30+
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
23+
<Appenders>
24+
<NoSql name="MONGO">
25+
<MongoDb
26+
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb"
27+
capped="true"
28+
collectionSize="1073741824"
29+
collectionName="MongoDBCollectionNameIT"/>
30+
</NoSql>
31+
</Appenders>
32+
<Loggers>
33+
<Root level="ALL">
34+
<AppenderRef ref="MONGO" />
35+
</Root>
36+
</Loggers>
37+
</Configuration>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
23+
<Appenders>
24+
<NoSql name="MONGO">
25+
<MongoDb
26+
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
27+
capped="true"
28+
collectionSize="1073741824"
29+
databaseName="testDb"
30+
collectionName="MongoDbDatabaseAndCollectionNameIT"
31+
/>
32+
</NoSql>
33+
</Appenders>
34+
<Loggers>
35+
<Root level="ALL">
36+
<AppenderRef ref="MONGO" />
37+
</Root>
38+
</Loggers>
39+
</Configuration>

0 commit comments

Comments
 (0)