Skip to content

Commit

Permalink
fix issue: qos-ch#409
Browse files Browse the repository at this point in the history
Signed-off-by: MzrW <[email protected]>
  • Loading branch information
MzrW committed Apr 9, 2024
1 parent 448bca1 commit 6b4e5f8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.event.DefaultLoggingEvent;
import org.slf4j.event.KeyValuePair;
import org.slf4j.event.Level;
Expand All @@ -38,11 +39,11 @@
*/
public class DefaultLoggingEventBuilder implements LoggingEventBuilder, CallerBoundaryAware {


// The caller boundary when the log() methods are invoked, is this class itself.

static String DLEB_FQCN = DefaultLoggingEventBuilder.class.getName();

protected DefaultLoggingEvent loggingEvent;
protected Logger logger;

Expand All @@ -53,7 +54,7 @@ public DefaultLoggingEventBuilder(Logger logger, Level level) {

/**
* Add a marker to the current logging event being built.
*
*
* It is possible to add multiple markers to the same logging event.
*
* @param marker the marker to add
Expand Down Expand Up @@ -140,9 +141,10 @@ public void log(Supplier<String> messageSupplier) {
log(messageSupplier.get());
}
}

protected void log(LoggingEvent aLoggingEvent) {
setCallerBoundary(DLEB_FQCN);
if (loggingEvent.getCallerBoundary() == null)
setCallerBoundary(DLEB_FQCN);
if (logger instanceof LoggingEventAware) {
((LoggingEventAware) logger).log(aLoggingEvent);
} else {
Expand Down Expand Up @@ -170,29 +172,32 @@ private void logViaPublicSLF4JLoggerAPI(LoggingEvent aLoggingEvent) {

msg = mergeMarkersAndKeyValuePairs(aLoggingEvent, msg);

switch (aLoggingEvent.getLevel()) {
case TRACE:
logger.trace(msg, combinedArguments);
break;
case DEBUG:
logger.debug(msg, combinedArguments);
break;
case INFO:
logger.info(msg, combinedArguments);
break;
case WARN:
logger.warn(msg, combinedArguments);
break;
case ERROR:
logger.error(msg, combinedArguments);
break;
if(logger instanceof LocationAwareLogger) {
((LocationAwareLogger) logger).log(null, aLoggingEvent.getCallerBoundary(), aLoggingEvent.getLevel().toInt(), msg, combinedArguments, aLoggingEvent.getThrowable());
} else {
switch (aLoggingEvent.getLevel()) {
case TRACE:
logger.trace(msg, combinedArguments);
break;
case DEBUG:
logger.debug(msg, combinedArguments);
break;
case INFO:
logger.info(msg, combinedArguments);
break;
case WARN:
logger.warn(msg, combinedArguments);
break;
case ERROR:
logger.error(msg, combinedArguments);
break;
}
}

}

/**
* Prepend markers and key-value pairs to the message.
*
*
* @param aLoggingEvent
* @param msg
* @return
Expand Down
100 changes: 100 additions & 0 deletions slf4j-jdk14/src/test/java/org/slf4j/jul/LocationAwareLoggingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.slf4j.jul;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.EventConstants;
import org.slf4j.spi.CallerBoundaryAware;
import org.slf4j.spi.LocationAwareLogger;
import org.slf4j.spi.LoggingEventBuilder;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static org.junit.Assert.assertEquals;

public class LocationAwareLoggingTest {
ListHandler listHandler = new ListHandler();
java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
Level oldLevel;

LoggerWrapper logger = new LoggerWrapper(getClass());

@Before
public void setUp() throws Exception {
oldLevel = root.getLevel();
root.setLevel(Level.FINE);
// removeAllHandlers(root);
root.addHandler(listHandler);
}

@After
public void tearDown() throws Exception {
root.setLevel(oldLevel);
removeListHandlers(root);
}

void removeListHandlers(java.util.logging.Logger logger) {
Handler[] handlers = logger.getHandlers();
for(Handler h : handlers) {
if(h instanceof ListHandler)
logger.removeHandler(h);
}
}

@Test
public void simpleMessage() {
String msg = "Hello world.";
logger.info(msg);
assertLogMessage("Hello world.", 0);
assertSourceClassName(LocationAwareLoggingTest.class.getName(), 0);
}

@Test
public void messageViaEvent() {
String msg = "Hello world.";
logger.infoWithEvent(msg);
assertLogMessage("Hello world.", 0);
assertSourceClassName(LocationAwareLoggingTest.class.getName(), 0);
}

private void assertSourceClassName(String expected, int index) {
LogRecord logRecord = listHandler.recordList.get(index);
Assert.assertNotNull(logRecord);
assertEquals(expected, logRecord.getSourceClassName());
}

private void assertLogMessage(String expected, int index) {
LogRecord logRecord = listHandler.recordList.get(index);
Assert.assertNotNull(logRecord);
assertEquals(expected, logRecord.getMessage());
}
}

class LoggerWrapper {
static final String FQCN_WRAPPER = LoggerWrapper.class.getName();
final Logger logger;

LoggerWrapper(Class<?> clazz) {
logger = LoggerFactory.getLogger(clazz);
}

public void info(String msg) {
if(logger instanceof LocationAwareLogger)
((LocationAwareLogger) logger).log(null, FQCN_WRAPPER, EventConstants.INFO_INT, msg, new Object[0], null);
else
logger.info(msg);
}

public void infoWithEvent(String msg) {
LoggingEventBuilder builder = logger.atInfo();
if(builder instanceof CallerBoundaryAware)
((CallerBoundaryAware) builder).setCallerBoundary(FQCN_WRAPPER);

builder.log(msg);
}
}

0 comments on commit 6b4e5f8

Please sign in to comment.