testing caller info extraction with slf4j-jpl

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
This commit is contained in:
Ceki Gulcu 2021-08-12 22:28:39 +02:00
parent eaa9fae39d
commit e240abe9b1
6 changed files with 61 additions and 2 deletions

View File

@ -27,6 +27,8 @@ public class DefaultLoggingEvent implements LoggingEvent {
Throwable throwable;
String threadName;
long timeStamp;
String callerBoundary;
public DefaultLoggingEvent(Level level, Logger logger) {
this.logger = logger;
@ -123,4 +125,12 @@ public class DefaultLoggingEvent implements LoggingEvent {
public long getTimeStamp() {
return timeStamp;
}
public void setCallerBoundary(String fqcn) {
this.callerBoundary = fqcn;
}
public String getCallerBoundary() {
return callerBoundary;
}
}

View File

@ -32,4 +32,14 @@ public interface LoggingEvent {
long getTimeStamp();
String getThreadName();
/**
* Returns the presumed caller boundary provided by the logging library (not the user of the library).
* Null by default.
*
* @return presumed caller, null by default.
*/
default String getCallerBoundary() {
return null;
}
}

View File

@ -0,0 +1,23 @@
package org.slf4j.spi;
/**
* Additional interface to {@link LoggingEventBuilder} and {@link LoggingEvent}.
*
* Implementations of {@link LoggingEventBuilder} and {@link LoggingEvent} may optionally
* implement {@link CallerBoundaryAware} in order to support caller info extraction.
*
* This interface is intended for use by logging backends or logging bridges.
*
* @author Ceki Gulcu
*
*/
public interface CallerBoundaryAware {
/**
* Add a fqcn (fully qualified class name) to this event, presumed to be the caller boundary.
*
* @param fqcn
*/
void setCallerBoundary(String fqcn);
}

View File

@ -9,7 +9,7 @@ import org.slf4j.event.KeyValuePair;
import org.slf4j.event.Level;
import org.slf4j.event.LoggingEvent;
public class DefaultLoggingEventBuilder implements LoggingEventBuilder {
public class DefaultLoggingEventBuilder implements LoggingEventBuilder, CallerBoundaryAware {
protected DefaultLoggingEvent loggingEvent;
protected Logger logger;
@ -50,6 +50,11 @@ public class DefaultLoggingEventBuilder implements LoggingEventBuilder {
return this;
}
@Override
public void setCallerBoundary(String fqcn) {
loggingEvent.setCallerBoundary(fqcn);
}
@Override
public void log(String message) {
loggingEvent.setMessage(message);
@ -187,4 +192,8 @@ public class DefaultLoggingEventBuilder implements LoggingEventBuilder {
return this;
}
}

View File

@ -29,7 +29,7 @@ import java.util.function.Supplier;
import org.slf4j.Marker;
/**
* A fluent API for creating logging events.
* This is main interface in slf4j's fluent API for creating logging events.
*
* @author Ceki G&uuml;lc&uuml;
* @since 2.0.0

View File

@ -30,6 +30,7 @@ import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.spi.CallerBoundaryAware;
import org.slf4j.spi.LoggingEventBuilder;
/**
@ -38,6 +39,8 @@ import org.slf4j.spi.LoggingEventBuilder;
*/
class SLF4JPlarformLogger implements System.Logger {
static private String PRESUMED_CALLER_BOUNDARY = System.Logger.class.getName();
private final Logger slf4jLogger;
public SLF4JPlarformLogger(Logger logger) {
@ -141,6 +144,10 @@ class SLF4JPlarformLogger implements System.Logger {
// The JDK uses a different formatting convention. We must invoke it now.
message = String.format(message, params);
}
if(leb instanceof CallerBoundaryAware) {
CallerBoundaryAware cba = (CallerBoundaryAware) leb;
cba.setCallerBoundary(PRESUMED_CALLER_BOUNDARY);
}
leb.log(message);
}