Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,36 @@ 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.

It can be a valid time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles".

If the time zone you want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone. The syntax of a custom time zone ID is:

```
CustomID:
GMT Sign Hours : Minutes
GMT Sign Hours Minutes
GMT Sign Hours

Sign: one of
+ -

Hours:
Digit
Digit Digit

Minutes:
Digit Digit

Digit: one of
0 1 2 3 4 5 6 7 8 9
```

_Hours_ must be between 0 to 23 and _Minutes_ must be between 00 to 59.
For example, "GMT+10" and "GMT+0010" mean ten hours and ten minutes ahead of GMT, respectively.

Use a blank string or `null` 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 @@ -52,7 +53,6 @@ public abstract class FormattedTimestampJsonProvider<Event extends DeferredProce
public static final String UNIX_TIMESTAMP_AS_STRING = "[UNIX_TIMESTAMP_AS_STRING]";

private static final String DEFAULT_PATTERN = "[ISO_OFFSET_DATE_TIME]";
private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault();

/**
* The pattern in which to format the timestamp.
Expand All @@ -72,7 +72,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 +204,60 @@ 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();
}


/**
* 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 TimeZone can be expressed into any format supported by {@link TimeZone#getTimeZone(String)}.
* It can be a valid time zone ID. For instance, the time zone ID for the
* U.S. Pacific Time zone is "America/Los_Angeles".
*
* <p>If the time zone you want is not represented by one of the
* supported IDs, then a custom time zone ID can be specified to
* produce a TimeZone. The syntax of a custom time zone ID is:
*
* <blockquote><pre>
* <i>CustomID:</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i>
* <i>Sign:</i> one of
* <code>+ -</code>
* <i>Hours:</i>
* <i>Digit</i>
* <i>Digit</i> <i>Digit</i>
* <i>Minutes:</i>
* <i>Digit</i> <i>Digit</i>
* <i>Digit:</i> one of
* <code>0 1 2 3 4 5 6 7 8 9</code>
* </pre></blockquote>
*
* <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
* between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten
* hours and ten minutes ahead of GMT, respectively.
*
* <p>Use a blank string or {@code null} to use the default TimeZone of the system.
Copy link
Collaborator Author

@brenuart brenuart Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joran does not allow to set a blank string nor a null value... May be we need another way here for the user to say it wants the default time zone of the system...?
User can of course not set the property and keep the default value... but what if he wants to explicitly set a value and not rely on the default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a special case for "DEFAULT" seems ok to me.

Copy link
Collaborator Author

@brenuart brenuart Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[DEFAULT] can now be used to refer to the system default time zone (between brackets to follow same convention as used by the pattern property of the same class).
Since Joran does not allow to set an empty string or a null value, I also removed these mentions in the documentation. These options are still supported tho and are documented in the javadoc for use by programmatic configuration if someone wants to go that road.

*
* @param timeZoneId the textual representation of the desired time zone
* @throws IllegalArgumentException if the input string is not a valid TimeZone representation
*/
public void setTimeZone(String timeZoneId) {
this.timeZone = TimeZone.getTimeZone(timeZoneId);
if (timeZoneId == null || timeZoneId.trim().isEmpty()) {
this.timeZone = TimeZone.getDefault();
} else {
this.timeZone = TimeZoneUtils.parseTimeZone(timeZoneId);
}
updateTimestampWriter();
}
}
80 changes: 80 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,80 @@
/*
* 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 string can be a valid time zone ID. For instance, the time zone ID for the
* U.S. Pacific Time zone is "America/Los_Angeles".
*
* <p>If the time zone you want is not represented by one of the
* supported IDs, then a custom time zone ID can be specified to
* produce a TimeZone. The syntax of a custom time zone ID is:
*
* <blockquote><pre>
* <i>CustomID:</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
* <code>GMT</code> <i>Sign</i> <i>Hours</i>
* <i>Sign:</i> one of
* <code>+ -</code>
* <i>Hours:</i>
* <i>Digit</i>
* <i>Digit</i> <i>Digit</i>
* <i>Minutes:</i>
* <i>Digit</i> <i>Digit</i>
* <i>Digit:</i> one of
* <code>0 1 2 3 4 5 6 7 8 9</code>
* </pre></blockquote>
*
* <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
* between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten
* hours and ten minutes ahead of GMT, respectively.
*
* @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);
}
}