Index: log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java (revision 1613338) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java (revision ) @@ -102,61 +102,6 @@ assertEquals(123, evt.getTimeMillis()); } - static class TimeMsg implements Message, TimestampMessage { - private static final long serialVersionUID = -2038413535672337079L; - private final String msg; - private final long timestamp; - - public TimeMsg(final String msg, final long timestamp) { - this.msg = msg; - this.timestamp = timestamp; - } - - @Override - public long getTimestamp() { - return timestamp; - } - - @Override - public String getFormattedMessage() { - return msg; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public Object[] getParameters() { - return null; - } - - @Override - public Throwable getThrowable() { - return null; - } - } - - @Test - public void testGetMillisReturnsMsgTimestampForTimestampMessage() { - final RingBufferLogEvent evt = new RingBufferLogEvent(); - final String loggerName = null; - final Marker marker = null; - final String fqcn = null; - final Level level = null; - final Message data = new TimeMsg("", 567); - final Throwable t = null; - final Map map = null; - final ContextStack contextStack = null; - final String threadName = null; - final StackTraceElement location = null; - final long currentTimeMillis = 123; - evt.setValues(null, loggerName, marker, fqcn, level, data, t, map, - contextStack, threadName, location, currentTimeMillis); - assertEquals(567, evt.getTimeMillis()); - } - @Test public void testSerializationDeserialization() throws IOException, ClassNotFoundException { final RingBufferLogEvent evt = new RingBufferLogEvent(); Index: log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java (revision ) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java (revision ) @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.async; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LifeCycle; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.message.TimestampMessage; +import org.apache.logging.log4j.util.Strings; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * Confirms that if you log a {@link TimestampMessage} then there are no unnecessary calls to {@link Clock}. + *

+ * See LOG4J2-744. + */ +public class AsyncLoggerTimestampMessageTest { + + @BeforeClass + public static void beforeClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, + AsyncLoggerContextSelector.class.getName()); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "AsyncLoggerTimestampMessageTest.xml"); + System.setProperty(ClockFactory.PROPERTY_NAME, + PoisonClock.class.getName()); + } + + @AfterClass + public static void afterClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY); + System.setProperty(ClockFactory.PROPERTY_NAME, Strings.EMPTY); + } + + @Test + public void testAsyncLogWritesToLog() throws Exception { + + final File f = new File("target", "AsyncLoggerTimestampMessageTest.log"); + // System.out.println(f.getAbsolutePath()); + f.delete(); + final Logger log = LogManager.getLogger("com.foo.Bar"); + log.info(new TimeMsg("Async logger msg with embedded timestamp", 123456789000L)); + ((LifeCycle) LogManager.getContext()).stop(); // stop async thread + + final BufferedReader reader = new BufferedReader(new FileReader(f)); + final String line1 = reader.readLine(); + reader.close(); + f.delete(); + assertNotNull(line1); + assertTrue("line1 correct", line1.equals("123456789000 Async logger msg with embedded timestamp")); + } + + public static class PoisonClock implements Clock { + @Override + public long currentTimeMillis() { + throw new RuntimeException("This should not have been called"); + } + } + + static class TimeMsg extends SimpleMessage implements TimestampMessage { + private final long timestamp; + + public TimeMsg(final String msg, final long timestamp) { + super(msg); + this.timestamp = timestamp; + } + + @Override + public long getTimestamp() { + return timestamp; + } + } +} Index: log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java (revision ) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java (revision ) @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.core; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.junit.InitialLoggerContext; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.message.TimestampMessage; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.apache.logging.log4j.util.Strings; +import org.junit.*; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Confirms that if you log a {@link TimestampMessage} then there are no unnecessary calls to {@link Clock}. + *

+ * See LOG4J2-744. + */ +public class TimestampMessageTest { + private ListAppender app; + + @ClassRule + public static InitialLoggerContext context = new InitialLoggerContext("log4j2-744.xml"); + + @BeforeClass + public static void beforeClass() { + System.setProperty(ClockFactory.PROPERTY_NAME, PoisonClock.class.getName()); + } + + @Before + public void setUp() throws Exception { + this.app = context.getListAppender("List").clear(); + } + + @AfterClass + public static void afterClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY); + System.setProperty(ClockFactory.PROPERTY_NAME, Strings.EMPTY); + } + + @Test + public void testTimestampMessage() { + final Logger log = context.getLogger("TimestampMessageTest"); + log.info(new TimeMsg("Message with embedded timestamp", 123456789000L)); + final List msgs = app.getMessages(); + assertNotNull(msgs); + assertEquals(1, msgs.size()); + assertEquals("123456789000 Message with embedded timestamp\n", msgs.get(0)); + } + + public static class PoisonClock implements Clock { + @Override + public long currentTimeMillis() { + throw new RuntimeException("This should not have been called"); + } + } + + static class TimeMsg extends SimpleMessage implements TimestampMessage { + private final long timestamp; + + public TimeMsg(final String msg, final long timestamp) { + super(msg); + this.timestamp = timestamp; + } + + @Override + public long getTimestamp() { + return timestamp; + } + } +} Index: log4j-core/src/test/resources/log4j2-744.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/resources/log4j2-744.xml (revision ) +++ log4j-core/src/test/resources/log4j2-744.xml (revision ) @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + \ No newline at end of file Index: log4j-core/src/test/resources/AsyncLoggerTimestampMessageTest.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/resources/AsyncLoggerTimestampMessageTest.xml (revision ) +++ log4j-core/src/test/resources/AsyncLoggerTimestampMessageTest.xml (revision ) @@ -0,0 +1,17 @@ + + + + + + %d{UNIX_MILLIS} %m%n + + + + + + + + + + \ No newline at end of file