All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nu.mine.mosher.io.slf4j.ServletLoggerPrinter Maven / Gradle / Ivy

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