Skip to content

Commit

Permalink
Fix LOGBACK-1599
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Dec 22, 2021
1 parent a1e94fd commit 18edd91
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 149 deletions.
Expand Up @@ -14,22 +14,29 @@
package ch.qos.logback.classic.pattern;

import ch.qos.logback.classic.spi.ILoggingEvent;

import ch.qos.logback.core.pattern.FormatInfo;

/**
* Outputs the number of microseconds of the timestamp.
*
*
*
* @author ceki
* @since 1.3.0
*/
public class MicrosecondConverter extends ClassicConverter {
public class MicrosecondConverter extends ClassicConverter {

@Override
public String convert(ILoggingEvent event) {
int nano = event.getNanoseconds();
int micro = nano/1000;
return Integer.toString(micro);
int nanos = event.getNanoseconds();
int millis_and_micros = nanos / 1000;
int micros = millis_and_micros % 1000;

if (micros >= 100)
return Integer.toString(micros);
else if (micros >= 10)
return "0" + Integer.toString(micros);
else
return "00" + Integer.toString(micros);
}

}
Expand Up @@ -13,6 +13,7 @@
*/
package ch.qos.logback.classic.spi;

import java.time.Instant;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -113,13 +114,43 @@ default Marker getMarker() {
*/
Map<String, String> getMdc();

/**
* Return the number of elapsed milliseconds since epoch.
*
* @return the number of elapsed milliseconds since epoch
* @since 1.3
*/
long getTimeStamp();

/**
* Return the number of elapsed nanoseconds found in {@link #getInstant()}
*
* Will return 0 if getInstant() returns null;
*
* @return the number of elapsed nanoseconds since epoch
* @since 1.3
*/
default int getNanoseconds() {
return 0;
Instant instant = getInstant();
if(instant == null)
return 0;
int nanoseconds = instant.getNano();
return nanoseconds;
}

/**
* Return the Instant the event was created.
*
* Default implementation returns null.
*
* @return the Instant the event was created.
* @since 1.3
*/
default Instant getInstant() {
return null;
}

/**
* The sequence number associated with this event.
*
* <p>Sequence numbers, if present, should be increasing monotonically.
Expand Down
Expand Up @@ -17,6 +17,7 @@
import java.io.ObjectOutputStream;
import java.time.Clock;
import java.time.Instant;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -107,8 +108,9 @@ public class LoggingEvent implements ILoggingEvent {
* The number of milliseconds elapsed from 1/1/1970 until logging event was
* created.
*/
private long timeStamp;
private Instant instant;

private long timeStamp;
private int nanoseconds;

private long sequenceNumber;
Expand All @@ -127,10 +129,11 @@ public LoggingEvent(String fqcn, Logger logger, Level level, String message, Thr
this.message = message;
this.argumentArray = argArray;
//List<Object> l = Arrays.asList(argArray);
Instant instant = Clock.systemUTC().instant();
timeStamp = instant.getEpochSecond();
nanoseconds = instant.getNano();


Instant instant = Clock.systemUTC().instant();
initTmestampFields(instant);


if(loggerContext != null) {
SequenceNumberGenerator sequenceNumberGenerator = loggerContext.getSequenceNumberGenerator();
if(sequenceNumberGenerator != null)
Expand All @@ -149,10 +152,16 @@ public LoggingEvent(String fqcn, Logger logger, Level level, String message, Thr
this.throwableProxy.calculatePackagingData();
}
}


}

void initTmestampFields(Instant instant) {
this.instant = instant;
long epochSecond = instant.getEpochSecond();
this.nanoseconds = instant.getNano();
long milliseconds = nanoseconds/1000_000;
this.timeStamp = (epochSecond*1000)+(milliseconds);
}

private Throwable extractThrowableAnRearrangeArguments(Object[] argArray) {
Throwable extractedThrowable = EventArgUtil.extractThrowable(argArray);
if (EventArgUtil.successfulExtraction(extractedThrowable)) {
Expand Down Expand Up @@ -277,38 +286,41 @@ public void setMessage(String message) {
this.message = message;
}



/**
* Return the number of elapsed seconds since epoch in UTC.
* Return the {@link Instant} corresponding to the creation of this event.
*
* @since 1.3
*/
public long getTimeStamp() {
return timeStamp;
public Instant getInstant() {
return instant;
}

/**
* Set the number of elapsed seconds since epoch in UTC.
* Set {@link Instant} corresponding to the creation of this event.
*/
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
public void setInstant(Instant instant) {
initTmestampFields(instant);
}


/**
* Return the number of nanoseconds after timestamp.
* @since 1.3
* Return the number of elapsed seconds since epoch in UTC.
*/
@Override
public int getNanoseconds() {
return nanoseconds;
public long getTimeStamp() {
return timeStamp;
}

/**
* Set the nanoseconds part of the timestamp.
*
* @since 1.3
* @param nanos
* Set the number of elapsed seconds since epoch in UTC.
*/
public void setNanoseconds(int nanos) {
this.nanoseconds = nanos;
public void setTimeStamp(long timeStamp) {
Instant instant = Instant.ofEpochMilli(timeStamp);
setInstant(instant);
}


@Override
public long getSequenceNumber() {
return sequenceNumber;
Expand Down
Expand Up @@ -34,6 +34,8 @@
import static ch.qos.logback.classic.ClassicTestConstants.MAIN_REGEX;
import static org.junit.Assert.*;

import java.time.Instant;

public class PatternLayoutTest extends AbstractPatternLayoutBaseTest<ILoggingEvent> {

private PatternLayout pl = new PatternLayout();
Expand Down Expand Up @@ -173,15 +175,21 @@ public void cnTest() {

@Test
public void micros() {
le.setNanoseconds(122091800);
verifyMicros(122_891_479, "2011-12-03 10:15:30.122 891 Some message");
verifyMicros(122_091_479, "2011-12-03 10:15:30.122 091 Some message");
verifyMicros(122_001_479, "2011-12-03 10:15:30.122 001 Some message");
}

void verifyMicros(int nanos, String expected) {
Instant instant = Instant.parse("2011-12-03T10:15:30Z");
instant = instant.plusNanos(nanos);
le.setInstant(instant);

pl.setPattern("%micros %message%nopex");
pl.setPattern("%date{yyyy-MM-dd HH:mm:ss.SSS, UTC} %micros %message%nopex");
pl.start();
getEventObject();

String val = pl.doLayout(le);
assertEquals("122091 Some message", val);

assertEquals(expected, val);
}

@Override
Expand Down

0 comments on commit 18edd91

Please sign in to comment.