![JAR search and dependency download from the Maven repository](/logo.png)
nu.mine.mosher.io.slf4j.ServletLoggerPrinter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of adapt-slf4j-to-servlet Show documentation
Show all versions of adapt-slf4j-to-servlet Show documentation
Slf4j adaptation layer, that sends log messages to the servlet context.
The newest version!
package nu.mine.mosher.io.slf4j;
import jakarta.servlet.ServletContext;
import lombok.*;
import java.util.Optional;
@RequiredArgsConstructor
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
final class ServletLoggerPrinter {
@NonNull private final String message;
@NonNull private final Optional throwable;
public void write() {
val ctx = ServletLoggerContext.get();
if (ctx.isPresent()) {
writeToServletContext(ctx.get());
} else {
writeToSysErr();
}
}
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private void writeToServletContext(@NonNull final ServletContext context) {
synchronized (context) {
if (this.throwable.isPresent()) {
context.log(this.message, this.throwable.get());
} else {
context.log(this.message);
}
}
}
private void writeToSysErr() {
System.err.println("Servlet context not found while logging: " + this.message);
this.throwable.ifPresent(Throwable::printStackTrace);
System.err.flush();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy