Skip to content

Commit b5741ac

Browse files
authored
Merge pull request #772 from metafacture/214-addLogLevelSetterForObjectAndStreamLoggers
Add setter for log level in object and stream (batch) loggers.
2 parents 7641cfc + b3f98da commit b5741ac

10 files changed

Lines changed: 516 additions & 44 deletions

File tree

metafacture-framework/src/main/java/org/metafacture/framework/MetafactureLogger.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ public class MetafactureLogger {
3232
private final Logger externalLogger;
3333
private final Logger internalLogger;
3434

35+
public enum Level {
36+
37+
ERROR((l, f, a) -> l.error(f, a)),
38+
WARN((l, f, a) -> l.warn(f, a)),
39+
INFO((l, f, a) -> l.info(f, a)),
40+
DEBUG((l, f, a) -> l.debug(f, a)),
41+
TRACE((l, f, a) -> l.trace(f, a));
42+
43+
private final LoggingConsumer consumer;
44+
45+
Level(final LoggingConsumer consumer) {
46+
this.consumer = consumer;
47+
}
48+
49+
private void log(final Logger logger, final String format, final Object... arguments) {
50+
consumer.accept(logger, format, arguments);
51+
}
52+
53+
}
54+
3555
/**
3656
* Creates an instance of {@link MetafactureLogger} with the given class.
3757
*
@@ -69,6 +89,97 @@ public Logger getExternalLogger() {
6989
return externalLogger;
7090
}
7191

92+
private void log(final Level level, final Logger logger, final String format, final Object... arguments) {
93+
level.log(logger, format, arguments);
94+
}
95+
96+
private void log(final String level, final Logger logger, final String format, final Object... arguments) {
97+
final Level levelValue;
98+
99+
try {
100+
levelValue = Level.valueOf(level);
101+
}
102+
catch (final IllegalArgumentException e) {
103+
throw new IllegalArgumentException("Unsupported log level: " + level, e);
104+
}
105+
106+
log(levelValue, logger, format, arguments);
107+
}
108+
109+
/**
110+
* Logs an <i>internal</i> message at the specified {@link Level level}
111+
* according to the specified format and arguments.
112+
*
113+
* @param level the log level
114+
* @param format the format string
115+
* @param arguments a list of arguments
116+
*/
117+
public void log(final Level level, final String format, final Object... arguments) {
118+
log(level, internalLogger, format, arguments);
119+
}
120+
121+
/**
122+
* Logs an <i>internal</i> message at the specified {@link Level level}
123+
* according to the specified format and arguments.
124+
*
125+
* @param level the log level
126+
* @param format the format string
127+
* @param arguments a list of arguments
128+
*/
129+
public void log(final String level, final String format, final Object... arguments) {
130+
log(level, internalLogger, format, arguments);
131+
}
132+
133+
/**
134+
* Logs an <i>external</i> message at the specified {@link Level level}
135+
* according to the specified format and arguments.
136+
*
137+
* @param level the log level
138+
* @param format the format string
139+
* @param arguments a list of arguments
140+
*/
141+
public void externalLog(final Level level, final String format, final Object... arguments) {
142+
log(level, externalLogger, format, arguments);
143+
}
144+
145+
/**
146+
* Logs an <i>external</i> message at the specified {@link Level level}
147+
* according to the specified format and arguments.
148+
*
149+
* @param level the log level
150+
* @param format the format string
151+
* @param arguments a list of arguments
152+
*/
153+
public void externalLog(final String level, final String format, final Object... arguments) {
154+
log(level, externalLogger, format, arguments);
155+
}
156+
157+
/**
158+
* Logs an <i>internal and external</i> message at the specified {@link Level level}
159+
* according to the specified format and arguments.
160+
*
161+
* @param level the log level
162+
* @param format the format string
163+
* @param arguments a list of arguments
164+
*/
165+
public void combinedLog(final Level level, final String format, final Object... arguments) {
166+
log(level, format, arguments);
167+
externalLog(level, format, arguments);
168+
}
169+
170+
/**
171+
* Logs an <i>internal and external</i> message at the specified {@link Level level}
172+
* according to the specified format and arguments.
173+
*
174+
* @param level the log level
175+
* @param format the format string
176+
* @param arguments a list of arguments
177+
*/
178+
public void combinedLog(final String level, final String format, final Object... arguments) {
179+
log(level, format, arguments);
180+
externalLog(level, format, arguments);
181+
}
182+
72183
/**
73184
* Logs an <i>internal</i> message at the ERROR level according to the
74185
* specified format and arguments.
@@ -239,4 +350,9 @@ public void combinedTrace(final String format, final Object... arguments) {
239350
externalTrace(format, arguments);
240351
}
241352

353+
@FunctionalInterface
354+
private interface LoggingConsumer {
355+
void accept(Logger logger, String format, Object... arguments);
356+
}
357+
242358
}

metafacture-monitoring/src/main/java/org/metafacture/monitoring/ObjectBatchLogger.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public final class ObjectBatchLogger<T> extends DefaultObjectPipe<T, ObjectRecei
4949
public static final String BATCH_COUNT_VAR = "batches";
5050
public static final String BATCH_SIZE_VAR = "batchSize";
5151
public static final String DEFAULT_FORMAT = "records processed: ${totalRecords}";
52+
public static final String DEFAULT_LEVEL = "INFO";
5253

5354
private static final MetafactureLogger LOG = new MetafactureLogger(ObjectBatchLogger.class);
5455

5556
private final Map<String, String> vars = new HashMap<String, String>();
5657
private final String format;
5758

59+
private String level = DEFAULT_LEVEL;
5860
private long batchSize = DEFAULT_BATCH_SIZE;
5961
private long recordCount;
6062
private long batchCount;
@@ -96,12 +98,21 @@ public void setBatchSize(final int batchSize) {
9698
this.batchSize = batchSize;
9799
}
98100

101+
/**
102+
* Sets the {@link MetafactureLogger.Level log level}.
103+
*
104+
* @param level the log level
105+
*/
106+
public void setLevel(final String level) {
107+
this.level = level;
108+
}
109+
99110
private void writeLog() {
100111
vars.put(RECORD_COUNT_VAR, Long.toString(recordCount));
101112
vars.put(BATCH_COUNT_VAR, Long.toString(batchCount));
102113
vars.put(BATCH_SIZE_VAR, Long.toString(batchSize));
103114
vars.put(TOTAL_RECORD_COUNT_VAR, Long.toString((batchSize * batchCount) + recordCount));
104-
LOG.externalInfo(StringUtil.format(format, vars));
115+
LOG.externalLog(level, StringUtil.format(format, vars));
105116
}
106117

107118
@Override

metafacture-monitoring/src/main/java/org/metafacture/monitoring/ObjectLogger.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import org.metafacture.framework.annotations.Out;
2525
import org.metafacture.framework.helpers.DefaultObjectPipe;
2626

27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
2731
/**
2832
* Logs the string representation of every object.
2933
*
@@ -39,8 +43,11 @@
3943
public final class ObjectLogger<T>
4044
extends DefaultObjectPipe<T, ObjectReceiver<T>> {
4145

46+
public static final String DEFAULT_LEVEL = "INFO";
47+
4248
private static final MetafactureLogger LOG = new MetafactureLogger(ObjectLogger.class);
4349

50+
private String level = DEFAULT_LEVEL;
4451
private String logPrefix = "";
4552

4653
/**
@@ -71,22 +78,40 @@ public void setPrefix(final String prefix) {
7178
this.logPrefix = prefix;
7279
}
7380

81+
/**
82+
* Sets the {@link MetafactureLogger.Level log level}.
83+
*
84+
* @param level the log level
85+
*/
86+
public void setLevel(final String level) {
87+
this.level = level;
88+
}
89+
7490
@Override
7591
public void process(final T obj) {
76-
LOG.externalDebug("{}{}", logPrefix, obj);
92+
writeLog("{}", obj);
7793
if (getReceiver() != null) {
7894
getReceiver().process(obj);
7995
}
8096
}
8197

8298
@Override
8399
protected void onResetStream() {
84-
LOG.externalDebug("{}resetStream", logPrefix);
100+
writeLog("resetStream");
85101
}
86102

87103
@Override
88104
protected void onCloseStream() {
89-
LOG.externalDebug("{}closeStream", logPrefix);
105+
writeLog("closeStream");
106+
}
107+
108+
private void writeLog(final String message, final Object... arguments) {
109+
final List<Object> argumentList = new ArrayList<>(arguments.length + 1);
110+
111+
argumentList.add(logPrefix);
112+
Arrays.stream(arguments).forEach(argumentList::add);
113+
114+
LOG.externalLog(level, "{}" + message, argumentList.toArray());
90115
}
91116

92117
}

metafacture-monitoring/src/main/java/org/metafacture/monitoring/StreamBatchLogger.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Markus Michael Geipel
3535
* @author Christoph Böhme
3636
*/
37-
@Description("Writes log info every BATCHSIZE records. ")
37+
@Description("Writes log info every BATCHSIZE records.")
3838
@In(StreamReceiver.class)
3939
@Out(StreamReceiver.class)
4040
@FluxCommand("batch-log")
@@ -45,6 +45,7 @@ public final class StreamBatchLogger extends ForwardingStreamPipe {
4545
public static final String BATCH_SIZE_VAR = "batchSize";
4646
public static final String TOTAL_RECORD_COUNT_VAR = "totalRecords";
4747
public static final String DEFAULT_FORMAT = "records processed: ${totalRecords}";
48+
public static final String DEFAULT_LEVEL = "INFO";
4849

4950
public static final long DEFAULT_BATCH_SIZE = 1000;
5051

@@ -53,6 +54,7 @@ public final class StreamBatchLogger extends ForwardingStreamPipe {
5354
private final Map<String, String> vars = new HashMap<>();
5455
private final String format;
5556

57+
private String level = DEFAULT_LEVEL;
5658
private long batchSize = DEFAULT_BATCH_SIZE;
5759
private long recordCount;
5860
private long batchCount;
@@ -94,6 +96,15 @@ public void setBatchSize(final int batchSize) {
9496
this.batchSize = batchSize;
9597
}
9698

99+
/**
100+
* Sets the {@link MetafactureLogger.Level log level}.
101+
*
102+
* @param level the log level
103+
*/
104+
public void setLevel(final String level) {
105+
this.level = level;
106+
}
107+
97108
/**
98109
* Gets the batch size.
99110
*
@@ -149,7 +160,7 @@ private void writeLog() {
149160
vars.put(BATCH_SIZE_VAR, Long.toString(batchSize));
150161
vars.put(TOTAL_RECORD_COUNT_VAR,
151162
Long.toString(batchSize * batchCount + recordCount));
152-
LOG.externalInfo(StringUtil.format(format, vars));
163+
LOG.externalLog(level, StringUtil.format(format, vars));
153164
}
154165

155166
}

metafacture-monitoring/src/main/java/org/metafacture/monitoring/StreamLogger.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import org.metafacture.framework.annotations.Out;
2525
import org.metafacture.framework.helpers.DefaultStreamPipe;
2626

27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
2731
/**
28-
* Leaves the event stream untouched but logs it to the debug log.
32+
* Leaves the event stream untouched but logs it to the info log.
2933
* The {@link StreamReceiver} may be {@code null}.
3034
* In this case {@link StreamLogger} behaves as a sink, just logging.
3135
*
@@ -39,8 +43,11 @@
3943
public final class StreamLogger
4044
extends DefaultStreamPipe<StreamReceiver> {
4145

46+
public static final String DEFAULT_LEVEL = "INFO";
47+
4248
private static final MetafactureLogger LOG = new MetafactureLogger(StreamLogger.class);
4349

50+
private String level = DEFAULT_LEVEL;
4451
private String logPrefix = "";
4552

4653
/**
@@ -71,10 +78,19 @@ public void setPrefix(final String prefix) {
7178
this.logPrefix = prefix;
7279
}
7380

81+
/**
82+
* Sets the {@link MetafactureLogger.Level log level}.
83+
*
84+
* @param level the log level
85+
*/
86+
public void setLevel(final String level) {
87+
this.level = level;
88+
}
89+
7490
@Override
7591
public void startRecord(final String identifier) {
7692
assert !isClosed();
77-
LOG.externalDebug("{}start record {}", logPrefix, identifier);
93+
writeLog("start record {}", identifier);
7894
if (null != getReceiver()) {
7995
getReceiver().startRecord(identifier);
8096
}
@@ -83,7 +99,7 @@ public void startRecord(final String identifier) {
8399
@Override
84100
public void endRecord() {
85101
assert !isClosed();
86-
LOG.externalDebug("{}end record", logPrefix);
102+
writeLog("end record");
87103
if (null != getReceiver()) {
88104
getReceiver().endRecord();
89105
}
@@ -92,7 +108,7 @@ public void endRecord() {
92108
@Override
93109
public void startEntity(final String name) {
94110
assert !isClosed();
95-
LOG.externalDebug("{}start entity {}", logPrefix, name);
111+
writeLog("start entity {}", name);
96112
if (null != getReceiver()) {
97113
getReceiver().startEntity(name);
98114
}
@@ -101,7 +117,7 @@ public void startEntity(final String name) {
101117
@Override
102118
public void endEntity() {
103119
assert !isClosed();
104-
LOG.externalDebug("{}end entity", logPrefix);
120+
writeLog("end entity");
105121
if (null != getReceiver()) {
106122
getReceiver().endEntity();
107123
}
@@ -111,20 +127,29 @@ public void endEntity() {
111127
@Override
112128
public void literal(final String name, final String value) {
113129
assert !isClosed();
114-
LOG.externalDebug("{}literal {}={}", logPrefix, name, value);
130+
writeLog("literal {}={}", name, value);
115131
if (null != getReceiver()) {
116132
getReceiver().literal(name, value);
117133
}
118134
}
119135

120136
@Override
121137
protected void onResetStream() {
122-
LOG.externalDebug("{}resetStream", logPrefix);
138+
writeLog("resetStream");
123139
}
124140

125141
@Override
126142
protected void onCloseStream() {
127-
LOG.externalDebug("{}closeStream", logPrefix);
143+
writeLog("closeStream");
144+
}
145+
146+
private void writeLog(final String message, final Object... arguments) {
147+
final List<Object> argumentList = new ArrayList<>(arguments.length + 1);
148+
149+
argumentList.add(logPrefix);
150+
Arrays.stream(arguments).forEach(argumentList::add);
151+
152+
LOG.externalLog(level, "{}" + message, argumentList.toArray());
128153
}
129154

130155
}

0 commit comments

Comments
 (0)