Skip to content

Commit

Permalink
[ISSUE-1138] code updates to use java 8 time
Browse files Browse the repository at this point in the history
  • Loading branch information
florentos17 authored and chibenwa committed Oct 25, 2024
1 parent 36f364b commit fd8240e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
* <tr>
* <td nowrap="nowrap">{@code timestamp}</td>
* <td nowrap="nowrap">String value of <code>IAccessEvent.{@link ch.qos.logback.access.spi.IAccessEvent#getTimeStamp() getTimeStamp()}</code></td>
* <td>By default, the value is not formatted; it is simply {@code String.valueOf(timestamp)}. To format
* the string using a SimpleDateFormat, set the {@link #setTimestampFormat(String) timestampFormat}
* property with the corresponding SimpleDateFormat string, for example, {@code yyyy-MM-dd HH:mm:ss.SSS}</td>
* <td>By default, the value is not formatted; it is simply {@code String.valueOf(timestamp)}. To format
* the string using a DateTimeFormatter, set the {@link #setTimestampFormat(String) timestampFormat}
* property with the corresponding DateTimeFormatter pattern, for example, {@code yyyy-MM-dd HH:mm:ss.SSS}.
* You can also specify a timezone using the {@link #setTimestampFormatTimezoneId(String) timestampFormatTimezoneId} property.</td>
* <td>true</td>
* </tr>
* <tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
* <td nowrap="nowrap">{@code timestamp}</td>
* <td nowrap="nowrap">String value of <code>ILoggingEvent.{@link ch.qos.logback.classic.spi.ILoggingEvent#getTimeStamp() getTimeStamp()}</code></td>
* <td>By default, the value is not formatted; it is simply {@code String.valueOf(timestamp)}. To format
* the string using a SimpleDateFormat, set the {@link #setTimestampFormat(String) timestampFormat}
* property with the corresponding SimpleDateFormat string, for example, {@code yyyy-MM-dd HH:mm:ss.SSS}</td>
* the string using a DateTimeFormatter, set the {@link #setTimestampFormat(String) timestampFormat}
* property with the corresponding DateTimeFormatter pattern, for example, {@code yyyy-MM-dd HH:mm:ss.SSS}
* You can also specify a timezone using the {@link #setTimestampFormatTimezoneId(String) timestampFormatTimezoneId} property.</td>
* <td>true</td>
* </tr>
* <tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.containsString;

/**
Expand Down Expand Up @@ -139,18 +144,17 @@ public void jsonLayout() throws Exception {

private void assertTimestamp(String log) {
int timestamp = log.indexOf(JsonLayout.TIMESTAMP_ATTR_NAME);
if(timestamp == -1){
if (timestamp == -1) {
fail(String.format("No instance of %s found in log, there should be one.", JsonLayout.TIMESTAMP_ATTR_NAME));
}

int timestampStart = timestamp + JsonLayout.TIMESTAMP_ATTR_NAME.length() + 1;
int timestampEnd = log.indexOf(",", timestampStart);
String timestampValue = log.substring(timestampStart, timestampEnd);
try {
new Date(Long.parseLong(timestampValue));
} catch (NumberFormatException e) {
fail(String.format("Value of attribute %s could not be converted to a valid Date", JsonLayout.TIMESTAMP_ATTR_NAME));
Instant.parse(timestampValue);
} catch (Exception e) {
fail(String.format("Value of attribute %s could not be converted to a valid Instant", JsonLayout.TIMESTAMP_ATTR_NAME));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
* This file is licensed under the terms of the
* Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*
*
* This work is based on the work found at
* https://github.com/qos-ch/logback-contrib
* authored by the logback-contrib developers.
*/
package ch.qos.logback.contrib.json;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TimeZone;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
Expand All @@ -37,9 +36,24 @@ public abstract class JsonLayoutBase<E> extends LayoutBase<E> {

protected JsonFormatter jsonFormatter;

private DateTimeFormatter dateTimeFormatter;

public JsonLayoutBase() {
this.includeTimestamp = true;
this.appendLineSeparator = false;
updateDateTimeFormatter();
}

private void updateDateTimeFormatter() {
if (this.timestampFormat != null) {
if (this.timestampFormatTimezoneId != null) {
this.dateTimeFormatter = DateTimeFormatter.ofPattern(this.timestampFormat)
.withZone(ZoneId.of(this.timestampFormatTimezoneId));
} else {
this.dateTimeFormatter = DateTimeFormatter.ofPattern(this.timestampFormat)
.withZone(ZoneId.systemDefault());
}
}
}

@Override
Expand Down Expand Up @@ -71,15 +85,9 @@ protected String formatTimestamp(long timestamp) {
if (this.timestampFormat == null || timestamp < 0) {
return String.valueOf(timestamp);
}
Date date = new Date(timestamp);
DateFormat format = createDateFormat(this.timestampFormat);
Instant instant = Instant.ofEpochMilli(timestamp);

if (this.timestampFormatTimezoneId != null) {
TimeZone tz = TimeZone.getTimeZone(this.timestampFormatTimezoneId);
format.setTimeZone(tz);
}

return format(date, format);
return dateTimeFormatter.format(instant);
}

public void addMap(String key, boolean field, Map<String, ?> mapValue, Map<String, Object> map) {
Expand All @@ -103,14 +111,6 @@ public void add(String fieldName, boolean field, String value, Map<String, Objec
}
}

protected DateFormat createDateFormat(String timestampFormat) {
return new SimpleDateFormat(timestampFormat);
}

protected String format(Date date, DateFormat format) {
return format.format(date);
}

protected abstract Map toJsonMap(E e);

@Override
Expand Down Expand Up @@ -140,6 +140,7 @@ public String getTimestampFormat() {

public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
updateDateTimeFormatter();
}

public String getTimestampFormatTimezoneId() {
Expand All @@ -148,6 +149,7 @@ public String getTimestampFormatTimezoneId() {

public void setTimestampFormatTimezoneId(String timestampFormatTimezoneId) {
this.timestampFormatTimezoneId = timestampFormatTimezoneId;
updateDateTimeFormatter();
}

public boolean isAppendLineSeparator() {
Expand Down

0 comments on commit fd8240e

Please sign in to comment.