package org.springframework.boot.logging;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
public class DeferredLog implements Log {
private volatile Log destination;
private final Supplier<Log> destinationSupplier;
private final Lines lines;
public DeferredLog() {
this.destinationSupplier = null;
this.lines = new Lines();
}
DeferredLog(Supplier<Log> destination, Lines lines) {
Assert.notNull(destination, "Destination must not be null");
this.destinationSupplier = destination;
this.lines = lines;
}
@Override
public boolean isTraceEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isTraceEnabled();
}
}
@Override
public boolean isDebugEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isDebugEnabled();
}
}
@Override
public boolean isInfoEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isInfoEnabled();
}
}
@Override
public boolean isWarnEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isWarnEnabled();
}
}
@Override
public boolean isErrorEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isErrorEnabled();
}
}
@Override
public boolean isFatalEnabled() {
synchronized (this.lines) {
return (this.destination == null) || this.destination.isFatalEnabled();
}
}
@Override
public void trace(Object message) {
log(LogLevel.TRACE, message, null);
}
@Override
public void trace(Object message, Throwable t) {
log(LogLevel.TRACE, message, t);
}
@Override
public void debug(Object message) {
log(LogLevel.DEBUG, message, null);
}
@Override
public void debug(Object message, Throwable t) {
log(LogLevel.DEBUG, message, t);
}
@Override
public void info(Object message) {
log(LogLevel.INFO, message, null);
}
@Override
public void info(Object message, Throwable t) {
log(LogLevel.INFO, message, t);
}
@Override
public void warn(Object message) {
log(LogLevel.WARN, message, null);
}
@Override
public void warn(Object message, Throwable t) {
log(LogLevel.WARN, message, t);
}
@Override
public void error(Object message) {
log(LogLevel.ERROR, message, null);
}
@Override
public void error(Object message, Throwable t) {
log(LogLevel.ERROR, message, t);
}
@Override
public void fatal(Object message) {
log(LogLevel.FATAL, message, null);
}
@Override
public void fatal(Object message, Throwable t) {
log(LogLevel.FATAL, message, t);
}
private void log(LogLevel level, Object message, Throwable t) {
synchronized (this.lines) {
if (this.destination != null) {
logTo(this.destination, level, message, t);
}
else {
this.lines.add(this.destinationSupplier, level, message, t);
}
}
}
void switchOver() {
this.destination = this.destinationSupplier.get();
}
public void switchTo(Class<?> destination) {
switchTo(LogFactory.getLog(destination));
}
public void switchTo(Log destination) {
synchronized (this.lines) {
replayTo(destination);
this.destination = destination;
}
}
public void replayTo(Class<?> destination) {
replayTo(LogFactory.getLog(destination));
}
public void replayTo(Log destination) {
synchronized (this.lines) {
for (Line line : this.lines) {
logTo(destination, line.getLevel(), line.getMessage(), line.getThrowable());
}
this.lines.clear();
}
}
public static Log replay(Log source, Class<?> destination) {
return replay(source, LogFactory.getLog(destination));
}
public static Log replay(Log source, Log destination) {
if (source instanceof DeferredLog) {
((DeferredLog) source).replayTo(destination);
}
return destination;
}
static void logTo(Log log, LogLevel level, Object message, Throwable throwable) {
switch (level) {
case TRACE:
log.trace(message, throwable);
return;
case DEBUG:
log.debug(message, throwable);
return;
case INFO:
log.info(message, throwable);
return;
case WARN:
log.warn(message, throwable);
return;
case ERROR:
log.error(message, throwable);
return;
case FATAL:
log.fatal(message, throwable);
}
}
static class Lines implements Iterable<Line> {
private final List<Line> lines = new ArrayList<>();
void add(Supplier<Log> destinationSupplier, LogLevel level, Object message, Throwable throwable) {
this.lines.add(new Line(destinationSupplier, level, message, throwable));
}
void clear() {
this.lines.clear();
}
@Override
public Iterator<Line> iterator() {
return this.lines.iterator();
}
}
static class Line {
private final Supplier<Log> destinationSupplier;
private final LogLevel level;
private final Object message;
private final Throwable throwable;
Line(Supplier<Log> destinationSupplier, LogLevel level, Object message, Throwable throwable) {
this.destinationSupplier = destinationSupplier;
this.level = level;
this.message = message;
this.throwable = throwable;
}
Log getDestination() {
return this.destinationSupplier.get();
}
LogLevel getLevel() {
return this.level;
}
Object getMessage() {
return this.message;
}
Throwable getThrowable() {
return this.throwable;
}
}
}