Skip to content

Commit 67c70b9

Browse files
committed
Merge branch '2.x' of github.com:jesmith17/logging-log4j2 into jesmith17-2.x-mongo-dbname
2 parents 8c0e3c6 + 180b050 commit 67c70b9

29 files changed

+635
-24
lines changed
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: 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("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+
}
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>
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.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/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Connection.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,23 @@ public MongoDb4Connection(
6767
final ConnectionString connectionString,
6868
final MongoClient mongoClient,
6969
final MongoDatabase mongoDatabase,
70+
final String collectionName,
7071
final boolean isCapped,
7172
final Integer sizeInBytes) {
72-
this(connectionString, mongoClient, mongoDatabase, isCapped, Long.valueOf(sizeInBytes));
73+
this(connectionString, mongoClient, mongoDatabase, collectionName, isCapped, Long.valueOf(sizeInBytes));
7374
}
7475

7576
public MongoDb4Connection(
7677
final ConnectionString connectionString,
7778
final MongoClient mongoClient,
7879
final MongoDatabase mongoDatabase,
80+
final String collectionName,
7981
final boolean isCapped,
8082
final Long sizeInBytes) {
8183
this.connectionString = connectionString;
8284
this.mongoClient = mongoClient;
8385
this.collection =
84-
getOrCreateMongoCollection(mongoDatabase, connectionString.getCollection(), isCapped, sizeInBytes);
86+
getOrCreateMongoCollection(mongoDatabase, collectionName, isCapped, sizeInBytes);
8587
}
8688

8789
@Override
@@ -119,4 +121,12 @@ public String toString() {
119121
"Mongo4Connection [connectionString=%s, collection=%s, mongoClient=%s]",
120122
connectionString, collection, mongoClient);
121123
}
124+
125+
/*
126+
* This method is exposed to help support unit tests for the MongoDbProvider class.
127+
*
128+
*/
129+
public MongoCollection<Document> getCollection() {
130+
return this.collection;
131+
}
122132
}

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

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.ConnectionString;
2020
import com.mongodb.MongoClientSettings;
21+
import com.mongodb.MongoNamespace;
2122
import com.mongodb.client.MongoClient;
2223
import com.mongodb.client.MongoClients;
2324
import com.mongodb.client.MongoDatabase;
@@ -60,14 +61,47 @@ public static class Builder<B extends Builder<B>> extends AbstractFilterable.Bui
6061
@PluginBuilderAttribute("capped")
6162
private boolean capped = false;
6263

64+
@PluginBuilderAttribute("collectionName")
65+
private String collectionName;
66+
67+
@PluginBuilderAttribute("databaseName")
68+
private String databaseName;
69+
70+
71+
6372
@Override
6473
public MongoDb4Provider build() {
6574
StatusLogger.getLogger().warn("The {} Appender is deprecated, use the MongoDb Appender.", PLUGIN_NAME);
75+
76+
ConnectionString connectionString;
77+
try {
78+
connectionString = new ConnectionString(connectionStringSource);
79+
} catch (final IllegalArgumentException e) {
80+
LOGGER.error("Invalid MongoDB connection string `{}`.", connectionStringSource, e);
81+
return null;
82+
}
83+
84+
String effectiveDatabaseName = databaseName != null ? databaseName : connectionString.getDatabase();
85+
String effectiveCollectionName = collectionName != null ? collectionName : connectionString.getCollection();
86+
// Validate the provided databaseName property
87+
try {
88+
MongoNamespace.checkDatabaseNameValidity(effectiveDatabaseName);
89+
} catch (final IllegalArgumentException e) {
90+
LOGGER.error("Invalid MongoDB database name `{}`.", effectiveDatabaseName, e);
91+
return null;
92+
}
93+
// Validate the provided collectionName property
94+
try {
95+
MongoNamespace.checkCollectionNameValidity(effectiveCollectionName);
96+
} catch (final IllegalArgumentException e) {
97+
LOGGER.error("Invalid MongoDB collection name `{}`.", effectiveCollectionName, e);
98+
return null;
99+
}
66100
return newMongoDb4Provider();
67101
}
68102

69103
protected MongoDb4Provider newMongoDb4Provider() {
70-
return new MongoDb4Provider(connectionStringSource, capped, collectionSize);
104+
return new MongoDb4Provider(connectionStringSource, databaseName, collectionName, capped, collectionSize);
71105
}
72106

73107
/**
@@ -113,6 +147,28 @@ public B setCollectionSize(final long sizeInBytes) {
113147
this.collectionSize = sizeInBytes;
114148
return asBuilder();
115149
}
150+
151+
/**
152+
* Sets name of the collection for the appender to output to
153+
*
154+
* @param collectionName the name of the collection for the appender to output to
155+
* @return this instance.
156+
*/
157+
public B setCollectionName(final String collectionName) {
158+
this.collectionName = collectionName;
159+
return asBuilder();
160+
}
161+
162+
/**
163+
* Sets the name of the logical database for the appender to output to.
164+
*
165+
* @param databaseName the name of the DB for the appender to output to
166+
* @return this instance.
167+
*/
168+
public B setDatabaseName(final String databaseName) {
169+
this.databaseName = databaseName;
170+
return asBuilder();
171+
}
116172
}
117173

118174
private static final Logger LOGGER = StatusLogger.getLogger();
@@ -140,11 +196,12 @@ public static <B extends Builder<B>> B newBuilder() {
140196

141197
private final Long collectionSize;
142198
private final boolean isCapped;
199+
private final String collectionName;
143200
private final MongoClient mongoClient;
144201
private final MongoDatabase mongoDatabase;
145202
private final ConnectionString connectionString;
146203

147-
private MongoDb4Provider(final String connectionStringSource, final boolean isCapped, final Long collectionSize) {
204+
private MongoDb4Provider(final String connectionStringSource, final String databaseName, final String collectionName, final boolean isCapped, final Long collectionSize) {
148205
LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
149206
this.connectionString = new ConnectionString(connectionStringSource);
150207
LOGGER.debug("Created ConnectionString {}", connectionString);
@@ -159,28 +216,29 @@ private MongoDb4Provider(final String connectionStringSource, final boolean isCa
159216
LOGGER.debug("Creating MongoClient {}...", settings);
160217
this.mongoClient = MongoClients.create(settings);
161218
LOGGER.debug("Created MongoClient {}", mongoClient);
162-
final String databaseName = this.connectionString.getDatabase();
163219
LOGGER.debug("Getting MongoDatabase {}...", databaseName);
164220
this.mongoDatabase = this.mongoClient.getDatabase(databaseName);
165221
LOGGER.debug("Got MongoDatabase {}", mongoDatabase);
166222
this.isCapped = isCapped;
167223
this.collectionSize = collectionSize;
224+
this.collectionName = collectionName;
168225
}
169226

170227
@Override
171228
public MongoDb4Connection getConnection() {
172-
return new MongoDb4Connection(connectionString, mongoClient, mongoDatabase, isCapped, collectionSize);
229+
return new MongoDb4Connection(connectionString, mongoClient, mongoDatabase, collectionName, isCapped, collectionSize);
173230
}
174231

175232
@Override
176233
public String toString() {
177234
return String.format(
178-
"%s [connectionString=%s, collectionSize=%s, isCapped=%s, mongoClient=%s, mongoDatabase=%s]",
235+
"%s [connectionString=%s, collectionSize=%s, isCapped=%s, mongoClient=%s, mongoDatabase=%s, collectionName=%s]",
179236
MongoDb4Provider.class.getSimpleName(),
180237
connectionString,
181238
collectionSize,
182239
isCapped,
183240
mongoClient,
184-
mongoDatabase);
241+
mongoDatabase,
242+
collectionName);
185243
}
186244
}

0 commit comments

Comments
 (0)