Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,9 @@ You can change the timezone like this:
</encoder>
```

The value of the `timeZone` element can be any string accepted by java's `TimeZone.getTimeZone(String id)` method.
The value of the `timeZone` element can be any string accepted by java's `TimeZone.getTimeZone(String id)` method.
For example `America/Los_Angeles`, `GMT+10` or `UTC`.
Use the special value `[DEFAULT]` to use the default TimeZone of the system.


## Customizing LoggingEvent Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.TimeZone;

import net.logstash.logback.fieldnames.LogstashCommonFieldNames;
import net.logstash.logback.util.TimeZoneUtils;

import ch.qos.logback.core.spi.DeferredProcessingAware;
import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -31,8 +32,8 @@
* Writes the timestamp field as either:
* <ul>
* <li>A string value formatted by a {@link DateTimeFormatter} pattern</li>
* <li>A string value representing the number of milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_STRING})</li>
* <li>A number value of the milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_NUMBER})</li>
* <li>A string value representing the number of milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_STRING})</li>
* <li>A number value of the milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_NUMBER})</li>
* </ul>
*/
public abstract class FormattedTimestampJsonProvider<Event extends DeferredProcessingAware, FieldNames extends LogstashCommonFieldNames> extends AbstractFieldJsonProvider<Event> implements FieldNamesAware<FieldNames> {
Expand All @@ -51,9 +52,16 @@ public abstract class FormattedTimestampJsonProvider<Event extends DeferredProce
*/
public static final String UNIX_TIMESTAMP_AS_STRING = "[UNIX_TIMESTAMP_AS_STRING]";

/**
* The default {@link #pattern} value.
*/
private static final String DEFAULT_PATTERN = "[ISO_OFFSET_DATE_TIME]";
private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault();

/**
* Keyword used by {@link #setTimeZone(String)} to denote the system default time zone.
*/
public static final String DEFAULT_TIMEZONE_KEYWORD = "[DEFAULT]";

/**
* The pattern in which to format the timestamp.
*
Expand All @@ -72,7 +80,7 @@ public abstract class FormattedTimestampJsonProvider<Event extends DeferredProce
* The timezone for which to write the timestamp.
* Only applicable if the pattern is not {@value #UNIX_TIMESTAMP_AS_NUMBER} or {@value #UNIX_TIMESTAMP_AS_STRING}
*/
private TimeZone timeZone = DEFAULT_TIMEZONE;
private TimeZone timeZone = TimeZone.getDefault();

/**
* Writes the timestamp to the JsonGenerator.
Expand Down Expand Up @@ -204,11 +212,35 @@ public void setPattern(String pattern) {
this.pattern = pattern;
updateTimestampWriter();
}

/**
* Get the time zone used to write the timestamp.
*
* @return the time zone used to write the timestamp
*/
public String getTimeZone() {
return timeZone.getID();
}
public void setTimeZone(String timeZoneId) {
this.timeZone = TimeZone.getTimeZone(timeZoneId);


/**
* Set the timezone for which to write the timestamp.
* Only applicable if the pattern is not {@value #UNIX_TIMESTAMP_AS_NUMBER} or {@value #UNIX_TIMESTAMP_AS_STRING}.
*
* <p>The value of the {@code timeZone} can be any string accepted by java's {@link TimeZone#getTimeZone(String)} method.
* For example "America/Los_Angeles" or "GMT+10".
*
* <p>Use a blank string, {@code null} or the value {@value #DEFAULT_TIMEZONE_KEYWORD} to use the default TimeZone of the system.
*
* @param timeZone the textual representation of the desired time zone
* @throws IllegalArgumentException if the input string is not a valid TimeZone representation
*/
public void setTimeZone(String timeZone) {
if (timeZone == null || timeZone.trim().isEmpty() || DEFAULT_TIMEZONE_KEYWORD.equalsIgnoreCase(timeZone)) {
this.timeZone = TimeZone.getDefault();
} else {
this.timeZone = TimeZoneUtils.parseTimeZone(timeZone);
}
updateTimestampWriter();
}
}
56 changes: 56 additions & 0 deletions src/main/java/net/logstash/logback/util/TimeZoneUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.logstash.logback.util;

import java.util.Objects;
import java.util.TimeZone;

public class TimeZoneUtils {

private TimeZoneUtils() {
// utility class
}


/**
* Parse a string into the corresponding {@link TimeZone} using the format described by
* {@link TimeZone#getTimeZone(String)}.
*
* <p>The value of the {@code timeZone} can be any string accepted by java's {@link TimeZone#getTimeZone(String)}
* method. For example "America/Los_Angeles" or "GMT+10".
*
* @param str the string to parse into a valid {@link TimeZone}.
* @return the {@link TimeZone} corresponding to the input string
* @throws IllegalArgumentException thrown when the string is not a valid TimeZone textual
* representation.
*/
public static TimeZone parseTimeZone(String str) {
TimeZone tz = TimeZone.getTimeZone(Objects.requireNonNull(str));

/*
* Instead of throwing an exception when it fails to parse the string into a valid
* TimeZone, getTimeZone() returns a TimeZone with id "GMT".
*
* If the returned TimeZone is GMT but the input string is not, then the input string
* was not a valid time zone representation.
*/
if ("GMT".equals(tz.getID()) && !"GMT".equals(str)) {
throw new IllegalArgumentException("Invalid TimeZone value (was '" + str + "')");
}

return tz;
}
}
54 changes: 54 additions & 0 deletions src/test/java/net/logstash/logback/util/TimeZoneUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.logstash.logback.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

/**
* @author brenuart
*
*/
public class TimeZoneUtilsTest {

@Test
public void standardTimeZones() {
assertThat(TimeZoneUtils.parseTimeZone("GMT").getID()).isEqualTo("GMT");
assertThat(TimeZoneUtils.parseTimeZone("Europe/Brussels").getID()).isEqualTo("Europe/Brussels");
}


@Test
public void customTimeZones() {
assertThat(TimeZoneUtils.parseTimeZone("GMT+00").getID()).isEqualTo("GMT+00:00");
assertThat(TimeZoneUtils.parseTimeZone("GMT+00:00").getID()).isEqualTo("GMT+00:00");

assertThat(TimeZoneUtils.parseTimeZone("GMT-00").getID()).isEqualTo("GMT-00:00");
assertThat(TimeZoneUtils.parseTimeZone("GMT-00:00").getID()).isEqualTo("GMT-00:00");

assertThat(TimeZoneUtils.parseTimeZone("GMT+01:12").getID()).isEqualTo("GMT+01:12");
}


@Test
public void invalidTimeZone() {
assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone("foo")).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone("")).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone(null)).isInstanceOf(NullPointerException.class);
}
}