Skip to content

Commit

Permalink
Add setting system/default timezone in logging configuration
Browse files Browse the repository at this point in the history
Users should have the ability to use the system timezone in the logging
configuration. It allows administrators to take advantage of the operation
system/JVM time zone management tools and use the same configuration
for Dropwizard applications in different time zones.
  • Loading branch information
arteam committed Mar 24, 2017
1 parent a8b6bdd commit 7bcd102
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/manual/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ discardingThreshold 51 When the blocking queue has only the capacit
If no discarding threshold is specified, then a default of queueSize / 5 is used.
To keep all events, set discardingThreshold to 0.
timeZone UTC The time zone to which event timestamps will be converted.
To use the system/default time zone, set it to ``system``.
target stdout The name of the standard stream to which events will be written.
Can be ``stdout`` or ``stderr``.
logFormat default The Logback pattern with which events will be formatted. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.filter.FilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;

Expand Down Expand Up @@ -100,7 +102,7 @@ public abstract class AbstractAppenderFactory<E extends DeferredProcessingAware>
private boolean includeCallerData = false;

private ImmutableList<FilterFactory<E>> filterFactories = ImmutableList.of();

private boolean neverBlock = false;

@JsonProperty
Expand Down Expand Up @@ -148,6 +150,19 @@ public TimeZone getTimeZone() {
return timeZone;
}

@JsonProperty
public void setTimeZone(String zoneId) {
if (zoneId.equalsIgnoreCase("system")) {
this.timeZone = TimeZone.getDefault();
} else {
if (Arrays.asList(TimeZone.getAvailableIDs()).contains(zoneId)) {
this.timeZone = TimeZone.getTimeZone(zoneId);
} else {
LoggerFactory.getLogger(getClass()).warn("Unknown timezone '{}' provided, using UTC", zoneId);
}
}
}

@JsonProperty
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
Expand All @@ -172,7 +187,7 @@ public ImmutableList<FilterFactory<E>> getFilterFactories() {
public void setFilterFactories(List<FilterFactory<E>> appenders) {
this.filterFactories = ImmutableList.copyOf(appenders);
}

@JsonProperty
public void setNeverBlock(boolean neverBlock) {
this.neverBlock = neverBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.dropwizard.logging;

import com.google.common.io.Resources;
import io.dropwizard.configuration.YamlConfigurationFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.validation.BaseValidator;
import org.junit.Test;

import java.io.File;
import java.net.URISyntaxException;
import java.util.TimeZone;

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

public class AppenderFactoryCustomTimeZone {

static {
BootstrapLogging.bootstrap();
}

private final YamlConfigurationFactory<ConsoleAppenderFactory> factory = new YamlConfigurationFactory<>(
ConsoleAppenderFactory.class, BaseValidator.newValidator(), Jackson.newObjectMapper(), "dw");

private static File loadResource(String resourceName) throws URISyntaxException {
return new File(Resources.getResource(resourceName).toURI());
}

@Test
public void testLoadAppenderWithCustomTimeZone() throws Exception {
final ConsoleAppenderFactory appender = factory.build(loadResource("yaml/appender_with_custom_time_zone.yml"));
assertThat(appender.getTimeZone().getID()).isEqualTo("CET");
}

@Test
public void testLoadAppenderWithNoTimeZone() throws Exception {
final ConsoleAppenderFactory appender = factory.build(loadResource("yaml/appender_with_no_time_zone.yml"));
assertThat(appender.getTimeZone().getID()).isEqualTo("UTC");
}

@Test
public void testLoadAppenderWithUtcTimeZone() throws Exception {
final ConsoleAppenderFactory appender = factory.build(loadResource("yaml/appender_with_utc_time_zone.yml"));
assertThat(appender.getTimeZone().getID()).isEqualTo("UTC");
}

@Test
public void testLoadAppenderWithWrongTimeZone() throws Exception {
final ConsoleAppenderFactory appender = factory.build(loadResource("yaml/appender_with_wrong_time_zone.yml"));
assertThat(appender.getTimeZone().getID()).isEqualTo("UTC");
}

@Test
public void testLoadAppenderWithSystemTimeZone() throws Exception {
final ConsoleAppenderFactory appender = factory.build(loadResource("yaml/appender_with_system_time_zone.yml"));
assertThat(appender.getTimeZone()).isEqualTo(TimeZone.getDefault());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: console
timeZone: CET
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: console
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: console
timeZone: system
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: console
timeZone: UTC
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: console
timeZone: Narnia

0 comments on commit 7bcd102

Please sign in to comment.