This is an automated email from the ASF dual-hosted git repository.
enapps-enorman pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-base.git
The following commit(s) were added to refs/heads/master by this push:
new 80f8929 SLING-13218 Migrate test logging from log4j to logback (#17)
80f8929 is described below
commit 80f89298f96cb84a706ce904561542b2fb7b8261
Author: Eric Norman <[email protected]>
AuthorDate: Wed May 27 11:13:23 2026 -0700
SLING-13218 Migrate test logging from log4j to logback (#17)
---
pom.xml | 16 ++---
.../discovery/base/its/AbstractClusterTest.java | 14 ++--
.../base/its/AbstractSingleInstanceTest.java | 8 +--
.../base/its/AbstractTopologyEventTest.java | 8 +--
.../base/its/setup/WithholdingAppender.java | 84 ++++++++++++++++------
src/test/resources/log4j.properties | 26 -------
src/test/resources/logback-test.xml | 35 +++++++++
7 files changed, 116 insertions(+), 75 deletions(-)
diff --git a/pom.xml b/pom.xml
index 29d80e4..3ee38b1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -258,12 +258,12 @@
<scope>test</scope>
</dependency>
- <!-- using log4j under slf4j to allow fine-grained logging config (see
src/test/resources/log4j.properties) -->
+ <!-- using logback under slf4j to allow fine-grained logging config
(see src/test/resources/logback-test.xml) -->
<dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.5</version>
- <scope>test</scope>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>1.2.13</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
@@ -277,12 +277,6 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.13</version>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.threads</artifactId>
diff --git
a/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
b/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
index 29c4056..5903563 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
@@ -34,10 +34,6 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Semaphore;
-import org.apache.log4j.Level;
-import org.apache.log4j.LogManager;
-import org.apache.log4j.spi.RootLogger;
-
import org.apache.sling.discovery.ClusterView;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyEvent;
@@ -61,6 +57,8 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import ch.qos.logback.classic.Level;
+
public abstract class AbstractClusterTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -103,7 +101,7 @@ public abstract class AbstractClusterTest {
@Before
public void setup() throws Exception {
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
logLevel = discoveryLogger.getLevel();
discoveryLogger.setLevel(Level.TRACE);
logger.debug("here we are");
@@ -140,7 +138,7 @@ public abstract class AbstractClusterTest {
instance3 = null;
instance4 = null;
instance5 = null;
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
discoveryLogger.setLevel(logLevel);
}
@@ -332,7 +330,7 @@ public abstract class AbstractClusterTest {
final int MIN_EVENT_DELAY = 1;
tearDown(); // reset any setup that was done - we start with a
different setup than the default one
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
logLevel = discoveryLogger.getLevel();
discoveryLogger.setLevel(Level.DEBUG);
@@ -512,7 +510,7 @@ public abstract class AbstractClusterTest {
final int MIN_EVENT_DELAY = 1;
tearDown(); // reset any setup that was done - we start with a
different setup than the default one
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
logLevel = discoveryLogger.getLevel();
discoveryLogger.setLevel(Level.DEBUG);
diff --git
a/src/test/java/org/apache/sling/discovery/base/its/AbstractSingleInstanceTest.java
b/src/test/java/org/apache/sling/discovery/base/its/AbstractSingleInstanceTest.java
index e0157f7..d735a2c 100644
---
a/src/test/java/org/apache/sling/discovery/base/its/AbstractSingleInstanceTest.java
+++
b/src/test/java/org/apache/sling/discovery/base/its/AbstractSingleInstanceTest.java
@@ -29,8 +29,6 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
-import org.apache.log4j.Level;
-import org.apache.log4j.spi.RootLogger;
import org.apache.sling.discovery.ClusterView;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyEvent;
@@ -46,6 +44,8 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import ch.qos.logback.classic.Level;
+
public abstract class AbstractSingleInstanceTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -60,7 +60,7 @@ public abstract class AbstractSingleInstanceTest {
@Before
public void setup() throws Exception {
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
logLevel = discoveryLogger.getLevel();
discoveryLogger.setLevel(Level.DEBUG);
logger.info("setup: creating new standalone instance");
@@ -74,7 +74,7 @@ public abstract class AbstractSingleInstanceTest {
@After
public void tearDown() throws Exception {
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
discoveryLogger.setLevel(logLevel);
logger.info("tearDown: stopping standalone instance");
if (instance!=null) {
diff --git
a/src/test/java/org/apache/sling/discovery/base/its/AbstractTopologyEventTest.java
b/src/test/java/org/apache/sling/discovery/base/its/AbstractTopologyEventTest.java
index 04eab4d..d62dd33 100644
---
a/src/test/java/org/apache/sling/discovery/base/its/AbstractTopologyEventTest.java
+++
b/src/test/java/org/apache/sling/discovery/base/its/AbstractTopologyEventTest.java
@@ -23,8 +23,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import org.apache.log4j.Level;
-import org.apache.log4j.spi.RootLogger;
import org.apache.sling.discovery.TopologyEvent;
import org.apache.sling.discovery.TopologyEvent.Type;
import org.apache.sling.discovery.TopologyView;
@@ -37,6 +35,8 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import ch.qos.logback.classic.Level;
+
/**
* Test class covering correct sending of TopologyEvents
* in various scenarios (which are not covered in other tests already).
@@ -52,7 +52,7 @@ public abstract class AbstractTopologyEventTest {
@Before
public void setup() throws Exception {
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
logLevel = discoveryLogger.getLevel();
discoveryLogger.setLevel(Level.DEBUG);
}
@@ -69,7 +69,7 @@ public abstract class AbstractTopologyEventTest {
instance2.stop();
instance2 = null;
}
- final org.apache.log4j.Logger discoveryLogger =
RootLogger.getLogger("org.apache.sling.discovery");
+ final ch.qos.logback.classic.Logger discoveryLogger =
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.sling.discovery");
discoveryLogger.setLevel(logLevel);
}
diff --git
a/src/test/java/org/apache/sling/discovery/base/its/setup/WithholdingAppender.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/WithholdingAppender.java
index c70ff50..6463b0f 100644
---
a/src/test/java/org/apache/sling/discovery/base/its/setup/WithholdingAppender.java
+++
b/src/test/java/org/apache/sling/discovery/base/its/setup/WithholdingAppender.java
@@ -22,18 +22,19 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
-import java.net.URL;
-import org.apache.log4j.Layout;
-import org.apache.log4j.LogManager;
-import org.apache.log4j.PatternLayout;
-import org.apache.log4j.PropertyConfigurator;
-import org.apache.log4j.WriterAppender;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.AppenderBase;
+import org.slf4j.LoggerFactory;
-public class WithholdingAppender extends WriterAppender {
+public class WithholdingAppender extends AppenderBase<ILoggingEvent> {
private final ByteArrayOutputStream baos;
private final Writer writer;
+ private PatternLayoutEncoder encoder;
/**
* Install the WithholdingAppender, essentially muting all logging
@@ -42,39 +43,78 @@ public class WithholdingAppender extends WriterAppender {
* withheld log output
*/
public static WithholdingAppender install() {
- LogManager.getRootLogger().removeAllAppenders();
- final WithholdingAppender withholdingAppender = new
WithholdingAppender(
- new PatternLayout("%d{dd.MM.yyyy HH:mm:ss.SSS} *%-5p* [%t]
%c{1}: %m\n"));
- LogManager.getRootLogger().addAppender(withholdingAppender);
+ LoggerContext loggerContext = (LoggerContext)
LoggerFactory.getILoggerFactory();
+ Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
+
+ // Remove all existing appenders
+ rootLogger.detachAndStopAllAppenders();
+
+ final WithholdingAppender withholdingAppender = new
WithholdingAppender();
+
+ // Create and configure the encoder
+ PatternLayoutEncoder encoder = new PatternLayoutEncoder();
+ encoder.setPattern("%d{dd.MM.yyyy HH:mm:ss.SSS} *%-5p* [%t] %c{1}:
%m%n");
+ encoder.setContext(loggerContext);
+ encoder.start();
+
+ withholdingAppender.setEncoder(encoder);
+ withholdingAppender.setContext(loggerContext);
+ withholdingAppender.start();
+
+ rootLogger.addAppender(withholdingAppender);
+ rootLogger.setLevel(ch.qos.logback.classic.Level.TRACE);
+
return withholdingAppender;
}
-
+
/**
* Release this WithholdingAppender and optionally dump what was
* withheld (eg in case of an exception)
* @param dumpToSysout
*/
public void release(boolean dumpToSysout) {
- LogManager.resetConfiguration();
- URL log4jPropertiesFile = getClass().getResource("/log4j.properties");
- PropertyConfigurator.configure(log4jPropertiesFile);
+ LoggerContext loggerContext = (LoggerContext)
LoggerFactory.getILoggerFactory();
+
+ // Stop and remove this appender
+ this.stop();
+ Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
+ rootLogger.detachAppender(this);
+
+ // Reset to default configuration
+ loggerContext.reset();
+
if (dumpToSysout) {
String withheldLogoutput = getBuffer();
System.out.println(withheldLogoutput);
}
}
-
- public WithholdingAppender(Layout layout) {
- this.layout = layout;
+
+ public WithholdingAppender() {
this.baos = new ByteArrayOutputStream();
this.writer = new BufferedWriter(new OutputStreamWriter(baos));
- this.setWriter(writer);
}
-
+
+ public void setEncoder(PatternLayoutEncoder encoder) {
+ this.encoder = encoder;
+ }
+
+ @Override
+ protected void append(ILoggingEvent event) {
+ if (encoder == null) {
+ return;
+ }
+ try {
+ byte[] bytes = encoder.encode(event);
+ writer.write(new String(bytes));
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+
public String getBuffer() {
- try{
+ try {
writer.flush();
- } catch(IOException e) {
+ } catch (IOException e) {
// ignore
}
return baos.toString();
diff --git a/src/test/resources/log4j.properties
b/src/test/resources/log4j.properties
deleted file mode 100644
index ec467a0..0000000
--- a/src/test/resources/log4j.properties
+++ /dev/null
@@ -1,26 +0,0 @@
-# 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.
-
-log4j.rootLogger=INFO, stdout
-
-log4j.logger.org.apache.jackrabbit.core.TransientRepository=WARN
-#log4j.logger.org.apache.sling.discovery.impl=DEBUG
-
-log4j.appender.stdout=org.apache.log4j.ConsoleAppender
-log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
-#log4j.appender.stdout.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} *%-5p*
[%t] %c{1}: %m (%F, line %L)\n
-log4j.appender.stdout.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS}
*%-5p* [%t] %c{1}: %m\n
diff --git a/src/test/resources/logback-test.xml
b/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..29ccf2e
--- /dev/null
+++ b/src/test/resources/logback-test.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<configuration>
+ <property name="LOG_PATTERN" value="%d{dd.MM.yyyy HH:mm:ss.SSS} *%-5p* [%t]
%c{1}: %m"/>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>${LOG_PATTERN}</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="org.apache.jackrabbit.core.TransientRepository" level="WARN"/>
+ <!-- <logger name="org.apache.sling.discovery.impl" level="DEBUG"/> -->
+
+ <root level="INFO">
+ <appender-ref ref="STDOUT"/>
+ </root>
+</configuration>