Skip to content

Commit 66b278c

Browse files
committed
Merge pull request dropwizard#770 from schlosna/lazy-logging
Simplify slf4j reporting when logging is disabled
2 parents dd0fca2 + 403cb61 commit 66b278c

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

metrics-core/src/main/java/com/codahale/metrics/Slf4jReporter.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,26 @@ public void report(SortedMap<String, Gauge> gauges,
182182
SortedMap<String, Histogram> histograms,
183183
SortedMap<String, Meter> meters,
184184
SortedMap<String, Timer> timers) {
185-
for (Entry<String, Gauge> entry : gauges.entrySet()) {
186-
logGauge(entry.getKey(), entry.getValue());
187-
}
185+
if (loggerProxy.isEnabled(marker)) {
186+
for (Entry<String, Gauge> entry : gauges.entrySet()) {
187+
logGauge(entry.getKey(), entry.getValue());
188+
}
188189

189-
for (Entry<String, Counter> entry : counters.entrySet()) {
190-
logCounter(entry.getKey(), entry.getValue());
191-
}
190+
for (Entry<String, Counter> entry : counters.entrySet()) {
191+
logCounter(entry.getKey(), entry.getValue());
192+
}
192193

193-
for (Entry<String, Histogram> entry : histograms.entrySet()) {
194-
logHistogram(entry.getKey(), entry.getValue());
195-
}
194+
for (Entry<String, Histogram> entry : histograms.entrySet()) {
195+
logHistogram(entry.getKey(), entry.getValue());
196+
}
196197

197-
for (Entry<String, Meter> entry : meters.entrySet()) {
198-
logMeter(entry.getKey(), entry.getValue());
199-
}
198+
for (Entry<String, Meter> entry : meters.entrySet()) {
199+
logMeter(entry.getKey(), entry.getValue());
200+
}
200201

201-
for (Entry<String, Timer> entry : timers.entrySet()) {
202-
logTimer(entry.getKey(), entry.getValue());
202+
for (Entry<String, Timer> entry : timers.entrySet()) {
203+
logTimer(entry.getKey(), entry.getValue());
204+
}
203205
}
204206
}
205207

@@ -286,6 +288,8 @@ public LoggerProxy(Logger logger) {
286288
}
287289

288290
abstract void log(Marker marker, String format, Object... arguments);
291+
292+
abstract boolean isEnabled(Marker marker);
289293
}
290294

291295
/* private class to allow logger configuration */
@@ -298,6 +302,11 @@ public DebugLoggerProxy(Logger logger) {
298302
public void log(Marker marker, String format, Object... arguments) {
299303
logger.debug(marker, format, arguments);
300304
}
305+
306+
@Override
307+
public boolean isEnabled(Marker marker) {
308+
return logger.isDebugEnabled(marker);
309+
}
301310
}
302311

303312
/* private class to allow logger configuration */
@@ -311,6 +320,10 @@ public void log(Marker marker, String format, Object... arguments) {
311320
logger.trace(marker, format, arguments);
312321
}
313322

323+
@Override
324+
public boolean isEnabled(Marker marker) {
325+
return logger.isTraceEnabled(marker);
326+
}
314327
}
315328

316329
/* private class to allow logger configuration */
@@ -323,6 +336,11 @@ public InfoLoggerProxy(Logger logger) {
323336
public void log(Marker marker, String format, Object... arguments) {
324337
logger.info(marker, format, arguments);
325338
}
339+
340+
@Override
341+
public boolean isEnabled(Marker marker) {
342+
return logger.isInfoEnabled(marker);
343+
}
326344
}
327345

328346
/* private class to allow logger configuration */
@@ -335,6 +353,11 @@ public WarnLoggerProxy(Logger logger) {
335353
public void log(Marker marker, String format, Object... arguments) {
336354
logger.warn(marker, format, arguments);
337355
}
356+
357+
@Override
358+
public boolean isEnabled(Marker marker) {
359+
return logger.isWarnEnabled(marker);
360+
}
338361
}
339362

340363
/* private class to allow logger configuration */
@@ -347,6 +370,11 @@ public ErrorLoggerProxy(Logger logger) {
347370
public void log(Marker marker, String format, Object... arguments) {
348371
logger.error(marker, format, arguments);
349372
}
373+
374+
@Override
375+
public boolean isEnabled(Marker marker) {
376+
return logger.isErrorEnabled(marker);
377+
}
350378
}
351379

352380
}

metrics-core/src/test/java/com/codahale/metrics/Slf4jReporterTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Slf4jReporterTest {
3535

3636
@Test
3737
public void reportsGaugeValuesAtError() throws Exception {
38+
when(logger.isErrorEnabled(marker)).thenReturn(true);
3839
errorReporter.report(map("gauge", gauge("value")),
3940
this.<Counter>map(),
4041
this.<Histogram>map(),
@@ -48,6 +49,7 @@ public void reportsGaugeValuesAtError() throws Exception {
4849
public void reportsCounterValuesAtError() throws Exception {
4950
final Counter counter = mock(Counter.class);
5051
when(counter.getCount()).thenReturn(100L);
52+
when(logger.isErrorEnabled(marker)).thenReturn(true);
5153

5254
errorReporter.report(this.<Gauge>map(),
5355
map("test.counter", counter),
@@ -76,6 +78,7 @@ public void reportsHistogramValuesAtError() throws Exception {
7678
when(snapshot.get999thPercentile()).thenReturn(11.0);
7779

7880
when(histogram.getSnapshot()).thenReturn(snapshot);
81+
when(logger.isErrorEnabled(marker)).thenReturn(true);
7982

8083
errorReporter.report(this.<Gauge>map(),
8184
this.<Counter>map(),
@@ -107,6 +110,7 @@ public void reportsMeterValuesAtError() throws Exception {
107110
when(meter.getOneMinuteRate()).thenReturn(3.0);
108111
when(meter.getFiveMinuteRate()).thenReturn(4.0);
109112
when(meter.getFifteenMinuteRate()).thenReturn(5.0);
113+
when(logger.isErrorEnabled(marker)).thenReturn(true);
110114

111115
errorReporter.report(this.<Gauge>map(),
112116
this.<Counter>map(),
@@ -150,6 +154,8 @@ public void reportsTimerValuesAtError() throws Exception {
150154

151155
when(timer.getSnapshot()).thenReturn(snapshot);
152156

157+
when(logger.isErrorEnabled(marker)).thenReturn(true);
158+
153159
errorReporter.report(this.<Gauge>map(),
154160
this.<Counter>map(),
155161
this.<Histogram>map(),
@@ -180,6 +186,7 @@ public void reportsTimerValuesAtError() throws Exception {
180186

181187
@Test
182188
public void reportsGaugeValues() throws Exception {
189+
when(logger.isInfoEnabled(marker)).thenReturn(true);
183190
infoReporter.report(map("gauge", gauge("value")),
184191
this.<Counter>map(),
185192
this.<Histogram>map(),
@@ -193,6 +200,7 @@ public void reportsGaugeValues() throws Exception {
193200
public void reportsCounterValues() throws Exception {
194201
final Counter counter = mock(Counter.class);
195202
when(counter.getCount()).thenReturn(100L);
203+
when(logger.isInfoEnabled(marker)).thenReturn(true);
196204

197205
infoReporter.report(this.<Gauge>map(),
198206
map("test.counter", counter),
@@ -221,6 +229,7 @@ public void reportsHistogramValues() throws Exception {
221229
when(snapshot.get999thPercentile()).thenReturn(11.0);
222230

223231
when(histogram.getSnapshot()).thenReturn(snapshot);
232+
when(logger.isInfoEnabled(marker)).thenReturn(true);
224233

225234
infoReporter.report(this.<Gauge>map(),
226235
this.<Counter>map(),
@@ -252,6 +261,7 @@ public void reportsMeterValues() throws Exception {
252261
when(meter.getOneMinuteRate()).thenReturn(3.0);
253262
when(meter.getFiveMinuteRate()).thenReturn(4.0);
254263
when(meter.getFifteenMinuteRate()).thenReturn(5.0);
264+
when(logger.isInfoEnabled(marker)).thenReturn(true);
255265

256266
infoReporter.report(this.<Gauge>map(),
257267
this.<Counter>map(),
@@ -294,6 +304,7 @@ public void reportsTimerValues() throws Exception {
294304
.toNanos(1000));
295305

296306
when(timer.getSnapshot()).thenReturn(snapshot);
307+
when(logger.isInfoEnabled(marker)).thenReturn(true);
297308

298309
infoReporter.report(this.<Gauge>map(),
299310
this.<Counter>map(),

0 commit comments

Comments
 (0)