Skip to content

Commit 07590bc

Browse files
vyjesmith17
andcommitted
Add collectionName and databaseName attributes to MongoDbProvider (#3467)
Co-authored-by: Josh Smith <[email protected]>
1 parent 5aac7d6 commit 07590bc

26 files changed

+655
-63
lines changed
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+
}
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+
}
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("MongoDbNoDatabaseAndCollectionNameIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbNoDatabaseAndCollectionNameIT extends AbstractMongoDbCappedIT {
30+
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
36+
}
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-2.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>
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-2.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>
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-2.xsd">
23+
<Appenders>
24+
<NoSql name="MONGO">
25+
<MongoDb
26+
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbNoDatabaseAndCollectionNameIT"
27+
capped="true"
28+
collectionSize="1073741824"
29+
/>
30+
</NoSql>
31+
</Appenders>
32+
<Loggers>
33+
<Root level="ALL">
34+
<AppenderRef ref="MONGO" />
35+
</Root>
36+
</Loggers>
37+
</Configuration>

log4j-mongodb4/pom.xml

-15
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@
134134
<plugin>
135135
<groupId>org.apache.maven.plugins</groupId>
136136
<artifactId>maven-surefire-plugin</artifactId>
137-
<configuration>
138-
<skip>true</skip>
139-
</configuration>
140-
<dependencies>
141-
<dependency>
142-
<groupId>org.junit.jupiter</groupId>
143-
<artifactId>junit-jupiter-engine</artifactId>
144-
<version>${junit-jupiter.version}</version>
145-
</dependency>
146-
</dependencies>
147137
</plugin>
148138

149139
</plugins>
@@ -236,11 +226,6 @@
236226
<include>**/*IT.java</include>
237227
</includes>
238228
<systemPropertyVariables>
239-
<!--
240-
~ Silence the tests.
241-
~ Annotate tests with `@UsingStatusListener` to see debug output on error
242-
-->
243-
<log4j.statusLogger.level>OFF</log4j.statusLogger.level>
244229
<!-- The `mongo.port` variable is created by `docker-maven-plugin` -->
245230
<log4j.mongo.port>${mongo.port}</log4j.mongo.port>
246231
</systemPropertyVariables>

log4j-mongodb4/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Connection.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class MongoDb4Connection extends AbstractNoSqlConnection<Document,
4141
private static MongoCollection<Document> getOrCreateMongoCollection(
4242
final MongoDatabase database, final String collectionName, final boolean isCapped, final Long sizeInBytes) {
4343
try {
44-
LOGGER.debug("Gettting collection '{}'...", collectionName);
44+
LOGGER.debug("Getting collection '{}'...", collectionName);
4545
// throws IllegalArgumentException if collectionName is invalid
4646
final MongoCollection<Document> found = database.getCollection(collectionName);
4747
LOGGER.debug("Got collection {}", found);
@@ -63,15 +63,29 @@ private static MongoCollection<Document> getOrCreateMongoCollection(
6363
private final MongoCollection<Document> collection;
6464
private final MongoClient mongoClient;
6565

66+
/**
67+
* @deprecated Use {@link #MongoDb4Connection(ConnectionString, MongoClient, MongoDatabase, String, boolean, Long)} instead
68+
*/
69+
@Deprecated
6670
public MongoDb4Connection(
6771
final ConnectionString connectionString,
6872
final MongoClient mongoClient,
6973
final MongoDatabase mongoDatabase,
7074
final boolean isCapped,
7175
final Integer sizeInBytes) {
72-
this(connectionString, mongoClient, mongoDatabase, isCapped, Long.valueOf(sizeInBytes));
76+
this(
77+
connectionString,
78+
mongoClient,
79+
mongoDatabase,
80+
connectionString.getCollection(),
81+
isCapped,
82+
Long.valueOf(sizeInBytes));
7383
}
7484

85+
/**
86+
* @deprecated Use {@link #MongoDb4Connection(ConnectionString, MongoClient, MongoDatabase, String, boolean, Long)} instead
87+
*/
88+
@Deprecated
7589
public MongoDb4Connection(
7690
final ConnectionString connectionString,
7791
final MongoClient mongoClient,
@@ -84,6 +98,18 @@ public MongoDb4Connection(
8498
getOrCreateMongoCollection(mongoDatabase, connectionString.getCollection(), isCapped, sizeInBytes);
8599
}
86100

101+
public MongoDb4Connection(
102+
final ConnectionString connectionString,
103+
final MongoClient mongoClient,
104+
final MongoDatabase mongoDatabase,
105+
final String collectionName,
106+
final boolean isCapped,
107+
final Long sizeInBytes) {
108+
this.connectionString = connectionString;
109+
this.mongoClient = mongoClient;
110+
this.collection = getOrCreateMongoCollection(mongoDatabase, collectionName, isCapped, sizeInBytes);
111+
}
112+
87113
@Override
88114
public void closeImpl() {
89115
// LOG4J2-1196

0 commit comments

Comments
 (0)