no.entur.logging.cloud.logback.logstash.test.CompositeConsoleAppender Maven / Gradle / Ivy
The newest version!
package no.entur.logging.cloud.logback.logstash.test;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.Encoder;
import org.slf4j.Marker;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class CompositeConsoleAppender extends ch.qos.logback.core.ConsoleAppender {
protected Encoder humanReadablePlainEncoder;
protected Encoder humanReadableJsonEncoder;
protected Encoder machineReadableJsonEncoder;
public Encoder getHumanReadablePlainEncoder() {
return humanReadablePlainEncoder;
}
public void useHumanReadablePlainEncoder() {
setEncoder(humanReadablePlainEncoder);
}
public void useHumanReadableJsonEncoder() {
setEncoder(humanReadableJsonEncoder);
}
public void useMachineReadableJsonEncoder() {
setEncoder(machineReadableJsonEncoder);
}
public void setHumanReadablePlainEncoder(Encoder humanReadablePlainEncoder) {
this.humanReadablePlainEncoder = humanReadablePlainEncoder;
if(this.encoder == null) {
this.setEncoder(humanReadablePlainEncoder);
CompositeConsoleOutputControl.useHumanReadablePlainEncoder();
}
}
public Encoder getHumanReadableJsonEncoder() {
return humanReadableJsonEncoder;
}
public void setHumanReadableJsonEncoder(Encoder humanReadableJsonEncoder) {
this.humanReadableJsonEncoder = humanReadableJsonEncoder;
if(this.encoder == null) {
this.setEncoder(humanReadableJsonEncoder);
CompositeConsoleOutputControl.useHumanReadableJsonEncoder();
}
}
public Encoder getMachineReadableJsonEncoder() {
return machineReadableJsonEncoder;
}
public void setMachineReadableJsonEncoder(Encoder machineReadableJsonEncoder) {
this.machineReadableJsonEncoder = machineReadableJsonEncoder;
if(this.encoder == null) {
this.setEncoder(machineReadableJsonEncoder);
CompositeConsoleOutputControl.useMachineReadableJsonEncoder();
}
}
protected void writeOut(E event) throws IOException {
// TODO should messages be flushed before the output type changes?
CompositeConsoleOutputType output = getOutputType(event);
switch (output) {
case humanReadablePlain: {
writeBytes(this.humanReadablePlainEncoder.encode(event));
break;
}
case humanReadableJson: {
writeBytes(this.humanReadableJsonEncoder.encode(event));
break;
}
case machineReadableJson: {
writeBytes(this.machineReadableJsonEncoder.encode(event));
break;
}
default: {
writeBytes(this.encoder.encode(event));
break;
}
}
}
private CompositeConsoleOutputType getOutputType(E event) {
if(event instanceof CompositeConsoleOutputDelegateILoggingEvent) {
CompositeConsoleOutputDelegateILoggingEvent composite = (CompositeConsoleOutputDelegateILoggingEvent)event;
return composite.getCompositeConsoleOutputType();
}
return CompositeConsoleOutputControl.getOutput();
}
// copy of superclass method
private void writeBytes(byte[] byteArray) throws IOException {
if (byteArray == null || byteArray.length == 0)
return;
streamWriteLock.lock();
try {
OutputStream outputStream = getOutputStream();
outputStream.write(byteArray);
if (isImmediateFlush()) {
outputStream.flush();
}
} finally {
streamWriteLock.unlock();
}
}
}