Skip to content

Commit

Permalink
fix SLF4J-546, Fluent logging API doesn't populate timestamp with Rel…
Browse files Browse the repository at this point in the history
…oad4JLogger

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Sep 28, 2022
1 parent b500a6f commit 4b5bb41
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

public void setCallerBoundary(String fqcn) {
this.callerBoundary = fqcn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.log4j.spi.ThrowableInformation;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.event.DefaultLoggingEvent;
import org.slf4j.event.LoggingEvent;
import org.slf4j.event.SubstituteLoggingEvent;
import org.slf4j.helpers.LegacyAbstractLogger;
Expand Down Expand Up @@ -169,6 +170,11 @@ private org.apache.log4j.spi.LoggingEvent event2Log4jEvent(LoggingEvent event, L
if (t != null)
ti = new ThrowableInformation(t);

if(event instanceof DefaultLoggingEvent) {
DefaultLoggingEvent defaultLoggingEvent = (DefaultLoggingEvent) event;
defaultLoggingEvent.setTimeStamp(System.currentTimeMillis());
}

org.apache.log4j.spi.LoggingEvent log4jEvent = new org.apache.log4j.spi.LoggingEvent(fqcn, logger, event.getTimeStamp(), log4jLevel, formattedMessage,
event.getThreadName(), ti, null, locationInfo, null);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.slf4j.reload4j;

import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static org.junit.Assert.*;

public class EventFieldsTest {

// value of LogManager.DEFAULT_CONFIGURATION_KEY;
static String CONFIG_FILE_KEY = "log4j.configuration";

@After
public void tearDown() throws Exception {
System.clearProperty(CONFIG_FILE_KEY);
}

@Test
public void testWhetherEventsFieldsAreSet() {
System.setProperty(CONFIG_FILE_KEY, "eventFields.properties");
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info("hello");
logger.atInfo().setMessage("hello").log();

org.slf4j.reload4j.Reload4jLoggerAdapter rootReload4j = (org.slf4j.reload4j.Reload4jLoggerAdapter) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);


ListAppender listAppender = (ListAppender) rootReload4j.logger.getAppender("LIST");

assertNotNull(listAppender);
assertNotNull(listAppender.list);

List<LoggingEvent> eventList = listAppender.list;

assertEquals(2, eventList.size());

LoggingEvent loggingEvent0 = eventList.get(0);
long timeStamp0 = loggingEvent0.getTimeStamp();
String threadName0 = loggingEvent0.getThreadName();
assertTrue(timeStamp0 != 0);
assertNotNull(threadName0);
assertFalse(threadName0.isEmpty());

LoggingEvent loggingEvent1 = eventList.get(1);
long timeStamp1 = loggingEvent1.getTimeStamp();
String threadName1 = loggingEvent1.getThreadName();
assertTrue(timeStamp1 != 0);
assertTrue(timeStamp1 >= timeStamp0);
assertNotNull(threadName1);
assertFalse(threadName1.isEmpty());
assertEquals(threadName0, threadName1);

}


}
4 changes: 4 additions & 0 deletions slf4j-reload4j/src/test/resources/eventFields.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
log4j.debug=true
log4j.rootLogger=DEBUG, LIST

log4j.appender.LIST=org.slf4j.reload4j.ListAppender

0 comments on commit 4b5bb41

Please sign in to comment.