Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Locale and timezone options for ConsoleReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantenney committed Jun 19, 2013
1 parent 13d7f1e commit fd40bfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class ConsoleReporterElementParser extends AbstractReporterElementParser {

private static final String DURATION_STRING_REGEX = "^(\\d+)\\s?(ns|us|ms|s|m|h|d)?$";
private static final String LOCALE_STRING_REGEX = "^[a-z]{2}(_[A-Z]{2})?$";

@Override
public String getType() {
Expand All @@ -41,6 +42,9 @@ protected void validate(ValidationContext c) {
c.optional(CLOCK_REF);
c.optional(OUTPUT_REF);

c.optional(LOCALE, LOCALE_STRING_REGEX, "Locale must be in the proper format");
c.optional(TIMEZONE); // Difficult to validate, if invalid will fall back to GMT

if (c.optional(RATE_UNIT)) {
TimeUnit.valueOf(c.get(RATE_UNIT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.ryantenney.metrics.spring.reporter;

import java.io.PrintStream;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import com.codahale.metrics.Clock;
Expand All @@ -31,6 +33,8 @@ public class ConsoleReporterFactoryBean extends AbstractScheduledReporterFactory
// Optional
public static final String CLOCK_REF = "clock-ref";
public static final String OUTPUT_REF = "output-ref";
public static final String LOCALE = "locale";
public static final String TIMEZONE = "timezone";
public static final String DURATION_UNIT = "duration-unit";
public static final String RATE_UNIT = "rate-unit";
public static final String FILTER_PATTERN = "filter";
Expand Down Expand Up @@ -68,11 +72,29 @@ else if (hasProperty(FILTER_REF)) {
reporter.outputTo(getPropertyRef(OUTPUT_REF, PrintStream.class));
}

if (hasProperty(LOCALE)) {
reporter.formattedFor(parseLocale(getProperty(LOCALE)));
}

if (hasProperty(TIMEZONE)) {
reporter.formattedFor(TimeZone.getTimeZone(getProperty(TIMEZONE)));
}

return reporter.build();
}

protected long getPeriod() {
return convertDurationString(getProperty(PERIOD));
}

protected Locale parseLocale(String localeString) {
int underscore = localeString.indexOf('_');
if (underscore == -1) {
return new Locale(localeString);
}
else {
return new Locale(localeString.substring(0, underscore), localeString.substring(underscore + 1));
}
}

}

0 comments on commit fd40bfe

Please sign in to comment.