Skip to content

Commit 1725c44

Browse files
committed
Enabled SLF4J logging in Bson
JAVA-2473
1 parent 945ea28 commit 1725c44

File tree

7 files changed

+414
-15
lines changed

7 files changed

+414
-15
lines changed

bson/src/main/org/bson/codecs/pojo/PojoCodec.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bson.codecs.EncoderContext;
2626
import org.bson.codecs.configuration.CodecConfigurationException;
2727
import org.bson.codecs.configuration.CodecRegistry;
28+
import org.bson.diagnostics.Logger;
2829
import org.bson.diagnostics.Loggers;
2930

3031
import java.util.ArrayList;
@@ -33,8 +34,6 @@
3334
import java.util.Map;
3435
import java.util.concurrent.ConcurrentHashMap;
3536
import java.util.concurrent.ConcurrentMap;
36-
import java.util.logging.Level;
37-
import java.util.logging.Logger;
3837

3938
import static java.lang.String.format;
4039
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs;
@@ -173,8 +172,8 @@ private <S> void decodeFieldModel(final BsonReader reader, final DecoderContext
173172
throw new CodecConfigurationException(format("Failed to decode '%s'. %s", name, e.getMessage()), e);
174173
}
175174
} else {
176-
if (LOGGER.isLoggable(Level.FINE)) {
177-
LOGGER.fine(format("Found field not present in the ClassModel: %s", name));
175+
if (LOGGER.isTraceEnabled()) {
176+
LOGGER.trace(format("Found field not present in the ClassModel: %s", name));
178177
}
179178
reader.skipValue();
180179
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2017 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.diagnostics;
18+
19+
import java.util.logging.Level;
20+
21+
import static java.util.logging.Level.FINE;
22+
import static java.util.logging.Level.FINER;
23+
import static java.util.logging.Level.INFO;
24+
import static java.util.logging.Level.SEVERE;
25+
import static java.util.logging.Level.WARNING;
26+
27+
class JULLogger implements Logger {
28+
29+
private final java.util.logging.Logger delegate;
30+
31+
JULLogger(final String name) {
32+
this.delegate = java.util.logging.Logger.getLogger(name);
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return delegate.getName();
38+
}
39+
40+
@Override
41+
public boolean isTraceEnabled() {
42+
return isEnabled(FINER);
43+
}
44+
45+
@Override
46+
public void trace(final String msg) {
47+
log(FINER, msg);
48+
}
49+
50+
@Override
51+
public void trace(final String msg, final Throwable t) {
52+
log(FINER, msg, t);
53+
}
54+
55+
@Override
56+
public boolean isDebugEnabled() {
57+
return isEnabled(FINE);
58+
}
59+
60+
@Override
61+
public void debug(final String msg) {
62+
log(FINE, msg);
63+
}
64+
65+
@Override
66+
public void debug(final String msg, final Throwable t) {
67+
log(FINE, msg, t);
68+
}
69+
70+
@Override
71+
public boolean isInfoEnabled() {
72+
return delegate.isLoggable(INFO);
73+
}
74+
75+
@Override
76+
public void info(final String msg) {
77+
log(INFO, msg);
78+
}
79+
80+
@Override
81+
public void info(final String msg, final Throwable t) {
82+
log(INFO, msg, t);
83+
}
84+
85+
@Override
86+
public boolean isWarnEnabled() {
87+
return delegate.isLoggable(WARNING);
88+
}
89+
90+
@Override
91+
public void warn(final String msg) {
92+
log(WARNING, msg);
93+
}
94+
95+
@Override
96+
public void warn(final String msg, final Throwable t) {
97+
log(WARNING, msg, t);
98+
}
99+
100+
101+
@Override
102+
public boolean isErrorEnabled() {
103+
return delegate.isLoggable(SEVERE);
104+
}
105+
106+
@Override
107+
public void error(final String msg) {
108+
log(SEVERE, msg);
109+
}
110+
111+
@Override
112+
public void error(final String msg, final Throwable t) {
113+
log(SEVERE, msg, t);
114+
}
115+
116+
117+
private boolean isEnabled(final Level level) {
118+
return delegate.isLoggable(level);
119+
}
120+
121+
private void log(final Level level, final String msg) {
122+
delegate.log(level, msg);
123+
}
124+
125+
public void log(final Level level, final String msg, final Throwable t) {
126+
delegate.log(level, msg, t);
127+
}
128+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2017 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.diagnostics;
18+
19+
/**
20+
* This class is not part of the public API. It may be removed or changed at any time.
21+
*
22+
*/
23+
public interface Logger {
24+
/**
25+
* Return the name of this <code>Logger</code> instance.
26+
*
27+
* @return name of this logger instance
28+
*/
29+
String getName();
30+
31+
/**
32+
* Is the logger instance enabled for the TRACE level?
33+
*
34+
* @return True if this Logger is enabled for the TRACE level, false otherwise.
35+
* @since 1.4
36+
*/
37+
boolean isTraceEnabled();
38+
39+
/**
40+
* Log a message at the TRACE level.
41+
*
42+
* @param msg the message string to be logged
43+
* @since 1.4
44+
*/
45+
void trace(String msg);
46+
47+
/**
48+
* Log an exception (throwable) at the TRACE level with an accompanying message.
49+
*
50+
* @param msg the message accompanying the exception
51+
* @param t the exception (throwable) to log
52+
* @since 1.4
53+
*/
54+
void trace(String msg, Throwable t);
55+
56+
/**
57+
* Is the logger instance enabled for the DEBUG level?
58+
*
59+
* @return True if this Logger is enabled for the DEBUG level, false otherwise.
60+
*/
61+
boolean isDebugEnabled();
62+
63+
64+
/**
65+
* Log a message at the DEBUG level.
66+
*
67+
* @param msg the message string to be logged
68+
*/
69+
void debug(String msg);
70+
71+
72+
/**
73+
* Log an exception (throwable) at the DEBUG level with an accompanying message.
74+
*
75+
* @param msg the message accompanying the exception
76+
* @param t the exception (throwable) to log
77+
*/
78+
void debug(String msg, Throwable t);
79+
80+
/**
81+
* Is the logger instance enabled for the INFO level?
82+
*
83+
* @return True if this Logger is enabled for the INFO level, false otherwise.
84+
*/
85+
boolean isInfoEnabled();
86+
87+
88+
/**
89+
* Log a message at the INFO level.
90+
*
91+
* @param msg the message string to be logged
92+
*/
93+
void info(String msg);
94+
95+
/**
96+
* Log an exception (throwable) at the INFO level with an accompanying message.
97+
*
98+
* @param msg the message accompanying the exception
99+
* @param t the exception (throwable) to log
100+
*/
101+
void info(String msg, Throwable t);
102+
103+
/**
104+
* Is the logger instance enabled for the WARN level?
105+
*
106+
* @return True if this Logger is enabled for the WARN level, false otherwise.
107+
*/
108+
boolean isWarnEnabled();
109+
110+
/**
111+
* Log a message at the WARN level.
112+
*
113+
* @param msg the message string to be logged
114+
*/
115+
void warn(String msg);
116+
117+
/**
118+
* Log an exception (throwable) at the WARN level with an accompanying message.
119+
*
120+
* @param msg the message accompanying the exception
121+
* @param t the exception (throwable) to log
122+
*/
123+
void warn(String msg, Throwable t);
124+
125+
/**
126+
* Is the logger instance enabled for the ERROR level?
127+
*
128+
* @return True if this Logger is enabled for the ERROR level, false otherwise.
129+
*/
130+
boolean isErrorEnabled();
131+
132+
/**
133+
* Log a message at the ERROR level.
134+
*
135+
* @param msg the message string to be logged
136+
*/
137+
void error(String msg);
138+
139+
/**
140+
* Log an exception (throwable) at the ERROR level with an accompanying message.
141+
*
142+
* @param msg the message accompanying the exception
143+
* @param t the exception (throwable) to log
144+
*/
145+
void error(String msg, Throwable t);
146+
}

bson/src/main/org/bson/diagnostics/Loggers.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2017 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.bson.diagnostics;
1818

19-
import java.util.logging.Logger;
19+
import static org.bson.assertions.Assertions.notNull;
2020

2121
/**
2222
* This class is not part of the public API.
@@ -29,23 +29,40 @@ public final class Loggers {
2929
*/
3030
public static final String PREFIX = "org.bson";
3131

32+
private static final boolean USE_SLF4J = shouldUseSLF4J();
33+
3234
/**
3335
* Gets a logger with the given suffix appended on to {@code PREFIX}, separated by a '.'.
3436
*
35-
* @param suffix the suffix for the logger.
37+
* @param suffix the suffix for the logger
3638
* @return the logger
3739
* @see Loggers#PREFIX
3840
*/
3941
public static Logger getLogger(final String suffix) {
40-
if (suffix == null) {
41-
throw new IllegalArgumentException("suffix can not be null");
42-
}
42+
notNull("suffix", suffix);
4343
if (suffix.startsWith(".") || suffix.endsWith(".")) {
4444
throw new IllegalArgumentException("The suffix can not start or end with a '.'");
4545
}
46-
return Logger.getLogger(PREFIX + "." + suffix);
46+
47+
String name = PREFIX + "." + suffix;
48+
49+
if (USE_SLF4J) {
50+
return new SLF4JLogger(name);
51+
} else {
52+
return new JULLogger(name);
53+
}
4754
}
4855

56+
private static boolean shouldUseSLF4J() {
57+
try {
58+
Class.forName("org.slf4j.LoggerFactory");
59+
// Don't use SLF4J unless a logging implementation has been configured for it
60+
Class.forName("org.slf4j.impl.StaticLoggerBinder");
61+
return true;
62+
} catch (ClassNotFoundException e) {
63+
return false;
64+
}
65+
}
4966
private Loggers() {
5067
}
5168
}

0 commit comments

Comments
 (0)