diff --git a/logback-contrib/json/access/src/main/java/ch/qos/logback/contrib/json/access/JsonLayout.java b/logback-contrib/json/access/src/main/java/ch/qos/logback/contrib/json/access/JsonLayout.java
index 8e73899009..2947a43831 100644
--- a/logback-contrib/json/access/src/main/java/ch/qos/logback/contrib/json/access/JsonLayout.java
+++ b/logback-contrib/json/access/src/main/java/ch/qos/logback/contrib/json/access/JsonLayout.java
@@ -32,9 +32,10 @@
*
* {@code timestamp} |
* String value of IAccessEvent.{@link ch.qos.logback.access.spi.IAccessEvent#getTimeStamp() getTimeStamp()} |
- * 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} |
+ * 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. |
* true |
*
*
diff --git a/logback-contrib/json/access/src/test/java/ch/qos/logback/contrib/json/access/JsonLayoutTest.java b/logback-contrib/json/access/src/test/java/ch/qos/logback/contrib/json/access/JsonLayoutTest.java
index 3f383e3a4e..2e976469b1 100644
--- a/logback-contrib/json/access/src/test/java/ch/qos/logback/contrib/json/access/JsonLayoutTest.java
+++ b/logback-contrib/json/access/src/test/java/ch/qos/logback/contrib/json/access/JsonLayoutTest.java
@@ -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;
/**
diff --git a/logback-contrib/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java b/logback-contrib/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java
index 8e237f8c5a..cf4ba76fa5 100755
--- a/logback-contrib/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java
+++ b/logback-contrib/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java
@@ -35,8 +35,9 @@
* {@code timestamp} |
* String value of ILoggingEvent.{@link ch.qos.logback.classic.spi.ILoggingEvent#getTimeStamp() getTimeStamp()} |
* 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} |
+ * 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.
* true |
*
*
diff --git a/logback-contrib/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java b/logback-contrib/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java
index 9e290324ad..1efb9a85f1 100644
--- a/logback-contrib/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java
+++ b/logback-contrib/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java
@@ -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;
/**
@@ -139,7 +144,7 @@ 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));
}
@@ -147,10 +152,9 @@ private void assertTimestamp(String log) {
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));
}
}
-
}
diff --git a/logback-contrib/json/core/src/main/java/ch/qos/logback/contrib/json/JsonLayoutBase.java b/logback-contrib/json/core/src/main/java/ch/qos/logback/contrib/json/JsonLayoutBase.java
index ac7ba50aa4..4ccfdcb6ff 100755
--- a/logback-contrib/json/core/src/main/java/ch/qos/logback/contrib/json/JsonLayoutBase.java
+++ b/logback-contrib/json/core/src/main/java/ch/qos/logback/contrib/json/JsonLayoutBase.java
@@ -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;
@@ -37,9 +36,24 @@ public abstract class JsonLayoutBase extends LayoutBase {
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
@@ -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 mapValue, Map map) {
@@ -103,14 +111,6 @@ public void add(String fieldName, boolean field, String value, Map