mirror of https://github.com/qos-ch/slf4j
testing caller info extraction with slf4j-jpl
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
This commit is contained in:
parent
eaa9fae39d
commit
e240abe9b1
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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ülcü
|
||||
* @since 2.0.0
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue