Skip to content

Commit

Permalink
minor adjustements related to NewAsyncAppender
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 10, 2024
1 parent 70dba2d commit 63576af
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/test/input/xml/newAsyncAppender0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
xmlns:log4j='http://jakarta.apache.org/log4j/'>


<appender name="V1" class="org.apache.log4j.VectorAppender"/>
<appender name="V1" class="org.apache.log4j.VectorAppender">
<param name="delay" value="0"/>
</appender>

<appender name="ASYNC" class="org.apache.log4j.NewAsyncAppender">
<param name="QueueSize" value="500"/> <!-- Buffer size for events -->
<appender-ref ref="V1"/>
</appender>

<root>
<level value="trace" />
<appender-ref ref="ASYNC" />
<level value="trace"/>
<appender-ref ref="ASYNC"/>
</root>

</log4j:configuration>
4 changes: 2 additions & 2 deletions src/test/java/org/apache/log4j/AsyncAppenderTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public void testLocationInfoTrue() {
layout.setConversionPattern("%C:%L %m%n");
layout.activateOptions();
String initialStr = layout.format(initialEvent);
assertEquals(AsyncAppenderTestCase.class.getName(),
initialStr.substring(0, AsyncAppenderTestCase.class.getName().length()));
assertEquals(Greeter.class.getName(),
initialStr.substring(0, Greeter.class.getName().length()));
String discardStr = layout.format(discardEvent);
assertEquals("?:? ", discardStr.substring(0, 4));
}
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/apache/log4j/VectorAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
*/
public class VectorAppender extends AppenderSkeleton {

public Vector vector;
public Vector<LoggingEvent> vector = new Vector<LoggingEvent>();
long delay = 100;
public VectorAppender() {
vector = new Vector();
}

public long getDelay() {
Expand All @@ -46,25 +45,25 @@ public void setDelay(long delay) {
* Does nothing.
*/
public void activateOptions() {
//System.out.println("activateOptions called");
}

/**
* This method is called by the {@link AppenderSkeleton#doAppend} method.
*/
public void append(LoggingEvent event) {
// System.out.println("---Vector appender called with message
// ["+event.getRenderedMessage()+"].");
//System.out.println("---Vector appender called with message ["+event.getRenderedMessage()+"].");
// System.out.flush();
try {
if(delay > 0) {
Thread.sleep(delay);
}
} catch (Exception e) {
}
vector.addElement(event);
vector.add(event);
}

public Vector getVector() {
public Vector<LoggingEvent> getVector() {
return vector;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.apache.log4j.xml;

import org.apache.log4j.*;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.util.Compare;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.apache.log4j.TestConstants.TEST_INPUT_PREFIX;
import static org.apache.log4j.TestConstants.TEST_WITNESS_PREFIX;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class DOM_NewAsyncAppenderTestCase {
Logger root;
Expand All @@ -21,21 +21,34 @@ public void setUp() {

@After
public void tearDown() {
System.out.println("----tearing down ");
root.getLoggerRepository().resetConfiguration();
}


@Test
public void test1() throws Exception {
public void smoke() throws Exception {
String msg = "hello";
DOMConfigurator.configure(TEST_INPUT_PREFIX + "xml/newAsyncAppender0.xml");
root.info(msg);

Appender appender = root.getAppender("ASYNC");
assertTrue(appender instanceof NewAsyncAppender);

NewAsyncAppender newAsyncAppender = (NewAsyncAppender) appender;

VectorAppender vectorAppender = (VectorAppender) newAsyncAppender.getAppender("V1");
assertNotNull(vectorAppender);



Thread.currentThread().sleep(100);

assertEquals(1, vectorAppender.getVector().size());

LoggingEvent event = (LoggingEvent) vectorAppender.vector.elementAt(0);

assertEquals(msg, event.getMessage());


}
}

0 comments on commit 63576af

Please sign in to comment.