Repository: logging-log4j2 Updated Branches: refs/heads/master 566b76b90 -> a3e0b3f7e
LOG4J2-1706 Make TimeFilter usable as global filter and as logger filter. In that case, filter on system time. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a3e0b3f7 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a3e0b3f7 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a3e0b3f7 Branch: refs/heads/master Commit: a3e0b3f7e03ea00059cd611ef6fe0e4f98d87e1a Parents: 566b76b Author: rpopma <rpo...@apache.org> Authored: Sun Nov 13 22:34:14 2016 +0900 Committer: rpopma <rpo...@apache.org> Committed: Sun Nov 13 22:34:14 2016 +0900 ---------------------------------------------------------------------- .../logging/log4j/core/filter/TimeFilter.java | 5 ++- .../log4j/core/filter/TimeFilterTest.java | 40 +++++++++++++++++--- src/changes/changes.xml | 3 ++ 3 files changed, 41 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a3e0b3f7/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java index 564059a..e05ef19 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java @@ -30,6 +30,8 @@ import org.apache.logging.log4j.core.config.Node; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.PluginAttribute; import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.ClockFactory; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.util.PerformanceSensitive; @@ -39,6 +41,7 @@ import org.apache.logging.log4j.util.PerformanceSensitive; @Plugin(name = "TimeFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true) @PerformanceSensitive("allocation") public final class TimeFilter extends AbstractFilter { + private static final Clock CLOCK = ClockFactory.getClock(); /** * Length of hour in milliseconds. @@ -120,7 +123,7 @@ public final class TimeFilter extends AbstractFilter { } private Result filter() { - return Result.NEUTRAL; + return filter(CLOCK.currentTimeMillis()); } @Override http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a3e0b3f7/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java index e39d7db..4ac0b40 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java @@ -23,6 +23,11 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.util.ClockFactoryTest; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; @@ -31,6 +36,25 @@ import static org.junit.Assert.*; * */ public class TimeFilterTest { + private static long CLOCKTIME = System.currentTimeMillis(); + + /** Helper class */ + public static class FixedTimeClock implements Clock { + @Override + public long currentTimeMillis() { + return CLOCKTIME; + } + } + + @BeforeClass + public static void beforeClass() { + System.setProperty(ClockFactory.PROPERTY_NAME, FixedTimeClock.class.getName()); + } + + @AfterClass + public static void afterClass() throws IllegalAccessException { + ClockFactoryTest.resetClocks(); + } @Test public void testTime() { @@ -39,17 +63,21 @@ public class TimeFilterTest { assertTrue(filter.isStarted()); final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/LosAngeles")); cal.set(Calendar.HOUR_OF_DAY, 2); - long tod = cal.getTimeInMillis(); - LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(tod).build(); + CLOCKTIME = cal.getTimeInMillis(); + LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); assertSame(Filter.Result.NEUTRAL, filter.filter(event)); + cal.roll(Calendar.DAY_OF_MONTH, true); - tod = cal.getTimeInMillis(); - event = Log4jLogEvent.newBuilder().setTimeMillis(tod).build(); + CLOCKTIME = cal.getTimeInMillis(); + event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame(Filter.Result.NEUTRAL, filter.filter(event)); + assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); + cal.set(Calendar.HOUR_OF_DAY, 4); - tod = cal.getTimeInMillis(); - event = Log4jLogEvent.newBuilder().setTimeMillis(tod).build(); + CLOCKTIME = cal.getTimeInMillis(); + event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); + assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); assertSame(Filter.Result.DENY, filter.filter(event)); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a3e0b3f7/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 155ff8f..b9aa337 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -30,6 +30,9 @@ <action issue="LOG4J2-1685" dev="mikes" type="fix" due-to="Raman Gupta"> Option 'disableAnsi' in PatternLayout to unconditionally disable ANSI escape codes. </action> + <action issue="LOG4J2-1706" dev="rpopma" type="fix"> + Make TimeFilter usable as global filter and as logger filter. + </action> <action issue="LOG4J2-1680" dev="rpopma" type="fix"> (GC) Avoid allocating temporary objects in TimeFilter. </action>