mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-02-01 10:24:30 +07:00
Added support for automatically flushing MDC after each entry
Riiablo's console was not written to handle multiline -- this seems easier
This commit is contained in:
parent
ba78d5d487
commit
14f78e0ca3
@ -1,10 +1,13 @@
|
|||||||
package com.riiablo.logger;
|
package com.riiablo.logger;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
|
||||||
public class RiiabloEncoder extends SimpleEncoder {
|
public class RiiabloEncoder extends SimpleEncoder {
|
||||||
|
private static final boolean FLUSH_COMPACT_MDCS = true;
|
||||||
|
|
||||||
private static final int MAX_DEPTH = 256;
|
private static final int MAX_DEPTH = 256;
|
||||||
private static final int DEPTH_STEP = 2;
|
private static final int DEPTH_STEP = 2;
|
||||||
private final CharSequence spaces = StringUtils.repeat(' ', MAX_DEPTH * DEPTH_STEP);
|
private final CharSequence spaces = StringUtils.repeat(' ', MAX_DEPTH * DEPTH_STEP);
|
||||||
@ -28,9 +31,9 @@ public class RiiabloEncoder extends SimpleEncoder {
|
|||||||
public void encode(LogEvent event, OutputStream out) {
|
public void encode(LogEvent event, OutputStream out) {
|
||||||
try {
|
try {
|
||||||
if (fullMode) {
|
if (fullMode) {
|
||||||
encodeFullMode(event, buffer);
|
encodeFullMode(event, out, buffer);
|
||||||
} else {
|
} else {
|
||||||
encodeCompactMode(event, buffer);
|
encodeCompactMode(event, out, buffer);
|
||||||
}
|
}
|
||||||
out.write(buffer.toString().getBytes(US_ASCII));
|
out.write(buffer.toString().getBytes(US_ASCII));
|
||||||
newLine(out);
|
newLine(out);
|
||||||
@ -42,19 +45,22 @@ public class RiiabloEncoder extends SimpleEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeFullMode(LogEvent event, StringBuilder buffer) {
|
private void encodeFullMode(LogEvent event, OutputStream out, StringBuilder buffer)
|
||||||
|
throws IOException {
|
||||||
final StringMap mdc = event.mdc();
|
final StringMap mdc = event.mdc();
|
||||||
encodeMessage(event, buffer);
|
encodeMessage(event, buffer);
|
||||||
encodeFullMDC(mdc, buffer);
|
encodeFullMDC(mdc, out, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeFullMDC(StringMap mdc, StringBuilder buffer) {
|
private void encodeFullMDC(StringMap mdc, OutputStream out, StringBuilder buffer)
|
||||||
|
throws IOException {
|
||||||
if (mdc.isEmpty()) return;
|
if (mdc.isEmpty()) return;
|
||||||
buffer.append(' ');
|
buffer.append(' ');
|
||||||
buffer.append(mdc.toString());
|
buffer.append(mdc.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeCompactMode(LogEvent event, StringBuilder buffer) {
|
private void encodeCompactMode(LogEvent event, OutputStream out, StringBuilder buffer)
|
||||||
|
throws IOException {
|
||||||
final StringMap mdc = event.mdc();
|
final StringMap mdc = event.mdc();
|
||||||
final int depth = mdc.size();
|
final int depth = mdc.size();
|
||||||
if (depth > 0) {
|
if (depth > 0) {
|
||||||
@ -80,7 +86,7 @@ public class RiiabloEncoder extends SimpleEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeCompactMDC(mdc, buffer, startingDepth, depth);
|
encodeCompactMDC(mdc, out, buffer, startingDepth, depth);
|
||||||
assert this.depth == depth;
|
assert this.depth == depth;
|
||||||
this.mdc = mdc;
|
this.mdc = mdc;
|
||||||
}
|
}
|
||||||
@ -91,14 +97,21 @@ public class RiiabloEncoder extends SimpleEncoder {
|
|||||||
encodeMessage(event, buffer);
|
encodeMessage(event, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeCompactMDC(StringMap mdc, StringBuilder buffer, int startingDepth, int depth) {
|
private void encodeCompactMDC(StringMap mdc, OutputStream out, StringBuilder buffer, int startingDepth, int depth)
|
||||||
|
throws IOException {
|
||||||
for (int d = startingDepth; d < depth; d++) {
|
for (int d = startingDepth; d < depth; d++) {
|
||||||
buffer.append(spaces, 0, d * DEPTH_STEP);
|
buffer.append(spaces, 0, d * DEPTH_STEP);
|
||||||
buffer.append(' ');
|
buffer.append(' ');
|
||||||
buffer.append('{');
|
buffer.append('{');
|
||||||
mdc.appendEntry(d, buffer);
|
mdc.appendEntry(d, buffer);
|
||||||
buffer.append('}');
|
buffer.append('}');
|
||||||
buffer.append(lineSeparator);
|
if (FLUSH_COMPACT_MDCS) {
|
||||||
|
out.write(buffer.toString().getBytes(US_ASCII));
|
||||||
|
newLine(out);
|
||||||
|
buffer.setLength(0);
|
||||||
|
} else {
|
||||||
|
buffer.append(lineSeparator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.depth = depth;
|
this.depth = depth;
|
||||||
|
Loading…
Reference in New Issue
Block a user