Skip to content

Commit

Permalink
switch to io.dropwizard.util.Size to make use of its validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kingsbury authored and Chris Kingsbury committed Apr 22, 2015
1 parent 343680b commit 2bbe4ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.util.Size;
import io.dropwizard.validation.ValidationMethod; import io.dropwizard.validation.ValidationMethod;


import javax.validation.constraints.Min; import javax.validation.constraints.Min;
Expand Down Expand Up @@ -71,9 +72,10 @@
* <td>{@code maxFileSize}</td> * <td>{@code maxFileSize}</td>
* <td>(unlimited)</td> * <td>(unlimited)</td>
* <td> * <td>
* The maximum size of the currently active file before a rollover is triggered. See. * The maximum size of the currently active file before a rollover is triggered. The value can be expressed
* <a href="http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy>the Logback documentation</a> * in bytes, kilobytes, megabytes, gigabytes, and terabytes using the by appending B, K, MB, GB, or TB to the
* for details. * numeric value. Examples include 100MB, 1GB, 1TB. Sizes can also be spelled out, such as 100 megabytes,
* 1 gigabyte, 1 terabyte.
* </td> * </td>
* </tr> * </tr>
* <tr> * <tr>
Expand Down Expand Up @@ -106,7 +108,7 @@ public class FileAppenderFactory extends AbstractAppenderFactory {
@Min(1) @Min(1)
private int archivedFileCount = 5; private int archivedFileCount = 5;


private String maxFileSize; private Size maxFileSize;


@NotNull @NotNull
private TimeZone timeZone = TimeZone.getTimeZone("UTC"); private TimeZone timeZone = TimeZone.getTimeZone("UTC");
Expand Down Expand Up @@ -152,12 +154,12 @@ public void setArchivedFileCount(int archivedFileCount) {
} }


@JsonProperty @JsonProperty
public String getMaxFileSize() { public Size getMaxFileSize() {
return maxFileSize; return maxFileSize;
} }


@JsonProperty @JsonProperty
public void setMaxFileSize(String maxFileSize) { public void setMaxFileSize(Size maxFileSize) {
this.maxFileSize = maxFileSize; this.maxFileSize = maxFileSize;
} }


Expand Down Expand Up @@ -213,7 +215,7 @@ protected FileAppender<ILoggingEvent> buildAppender(LoggerContext context) {
triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>(); triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
} else { } else {
SizeAndTimeBasedFNATP<ILoggingEvent> maxFileSizeTriggeringPolicy = new SizeAndTimeBasedFNATP<>(); SizeAndTimeBasedFNATP<ILoggingEvent> maxFileSizeTriggeringPolicy = new SizeAndTimeBasedFNATP<>();
maxFileSizeTriggeringPolicy.setMaxFileSize(maxFileSize); maxFileSizeTriggeringPolicy.setMaxFileSize(String.valueOf(maxFileSize.toBytes()));
triggeringPolicy = maxFileSizeTriggeringPolicy; triggeringPolicy = maxFileSizeTriggeringPolicy;
} }
triggeringPolicy.setContext(context); triggeringPolicy.setContext(context);
Expand Down
Expand Up @@ -9,6 +9,8 @@
import ch.qos.logback.core.rolling.RollingFileAppender; import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP; import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP;
import io.dropwizard.jackson.DiscoverableSubtypeResolver; import io.dropwizard.jackson.DiscoverableSubtypeResolver;
import io.dropwizard.util.Size;
import io.dropwizard.util.SizeUnit;
import org.junit.Test; import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand Down Expand Up @@ -54,12 +56,12 @@ public void hasMaxFileSize() throws Exception {
FileAppenderFactory fileAppenderFactory = new FileAppenderFactory(); FileAppenderFactory fileAppenderFactory = new FileAppenderFactory();
fileAppenderFactory.setCurrentLogFilename("logfile.log"); fileAppenderFactory.setCurrentLogFilename("logfile.log");
fileAppenderFactory.setArchive(true); fileAppenderFactory.setArchive(true);
fileAppenderFactory.setMaxFileSize("100MB"); fileAppenderFactory.setMaxFileSize(Size.kilobytes(1));
fileAppenderFactory.setArchivedLogFilenamePattern("example-%d-%i.log.gz"); fileAppenderFactory.setArchivedLogFilenamePattern("example-%d-%i.log.gz");
RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext()); RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext());


assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeAndTimeBasedFNATP.class); assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeAndTimeBasedFNATP.class);
assertThat(((SizeAndTimeBasedFNATP) appender.getTriggeringPolicy()).getMaxFileSize()).isEqualTo("100MB"); assertThat(((SizeAndTimeBasedFNATP) appender.getTriggeringPolicy()).getMaxFileSize()).isEqualTo("1024");
} }


@Test @Test
Expand Down
2 changes: 1 addition & 1 deletion dropwizard-logging/src/test/resources/yaml/logging.yml
Expand Up @@ -12,7 +12,7 @@ appenders:
- type: file - type: file
threshold: ALL threshold: ALL
maxFileSize: 100MB maxFileSize: 100MB
currentLogFilename: ./logs/max-file-size-example-%i.log currentLogFilename: ./logs/max-file-size-example.log
archivedLogFilenamePattern: ./logs/max-file-size-example-%d-%i.log.gz archivedLogFilenamePattern: ./logs/max-file-size-example-%d-%i.log.gz
archivedFileCount: 5 archivedFileCount: 5
- type: syslog - type: syslog
Expand Down

0 comments on commit 2bbe4ec

Please sign in to comment.