This is an automated email from the ASF dual-hosted git repository.
dakirily pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git
The following commit(s) were added to refs/heads/main by this push:
new 678312f6c1 QPID-8793: [Broker-J] Fix rolled log discovery with Logback
1.6.6 (#473)
678312f6c1 is described below
commit 678312f6c1b0fc1356d1cffce5202bba1ef2beea
Author: usugumo <[email protected]>
AuthorDate: Tue Nov 17 12:16:21 2026 +0100
QPID-8793: [Broker-J] Fix rolled log discovery with Logback 1.6.6 (#473)
---
.../logging/logback/RollingPolicyDecorator.java | 49 ++++-----
.../logback/RollingPolicyDecoratorTest.java | 113 ++++++++++++++++++++-
2 files changed, 136 insertions(+), 26 deletions(-)
diff --git
a/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java
b/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java
index ee74849179..efe592a6ec 100644
---
a/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java
+++
b/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java
@@ -37,6 +37,7 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
+import ch.qos.logback.core.Context;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.RollingPolicy;
import ch.qos.logback.core.rolling.RollingPolicyBase;
@@ -65,15 +66,18 @@ public class RollingPolicyDecorator implements RollingPolicy
private ScanTask _currentScanTask;
private String[] _previousScanResults;
- public RollingPolicyDecorator(RollingPolicyBase decorated,
RolloverListener listener, ScheduledExecutorService executorService)
+ public RollingPolicyDecorator(final RollingPolicyBase decorated,
+ final RolloverListener listener,
+ final ScheduledExecutorService
executorService)
{
_decorated = decorated;
_listener = listener;
_executorService = executorService;
- String filePathPattern = _decorated.getFileNamePattern();
- String filePathRegExp = new FileNamePattern(filePathPattern,
_decorated.getContext()).toRegex();
- FilePathBaseFolderAndPatternPair pair = new
FilePathBaseFolderAndPatternPair(filePathRegExp);
+ final String pattern = _decorated.getFileNamePattern();
+ final Context context = _decorated.getContext();
+ final FileNamePattern fileNamePattern = new FileNamePattern(pattern,
context);
+ final FilePathBaseFolderAndPatternPair pair = new
FilePathBaseFolderAndPatternPair(fileNamePattern);
_rolledFilesBaseFolder = pair.getBaseFolder();
_rolledFileRegExp = pair.getPattern();
_currentScanTask = null;
@@ -226,13 +230,13 @@ public class RollingPolicyDecorator implements
RollingPolicy
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
{
- String absolutePath = file.toAbsolutePath().toString();
+ String relativePath =
_rolledFilesBaseFolder.relativize(file.toAbsolutePath()).toString();
if (File.separatorChar == '\\')
{
- absolutePath = absolutePath.replace('\\', '/');
+ relativePath = relativePath.replace('\\', '/');
}
- if (_rolledFileRegExp.matcher(absolutePath).matches())
+ if (_rolledFileRegExp.matcher(relativePath).matches())
{
rolledFiles.add(file);
}
@@ -290,26 +294,25 @@ public class RollingPolicyDecorator implements
RollingPolicy
private final Path _baseFolder;
private final Pattern _pattern;
- public FilePathBaseFolderAndPatternPair(String fileNamePattern)
+ public FilePathBaseFolderAndPatternPair(final FileNamePattern
fileNamePattern)
{
- String path;
- int firstDigitPatternPosition= fileNamePattern.indexOf("\\d");
- if (firstDigitPatternPosition == -1)
+ final String filePathPattern = fileNamePattern.getPattern();
+ final String filePathRegExp = fileNamePattern.toRegex();
+ if (filePathRegExp.indexOf("\\d") == -1)
{
- throw new RuntimeException("Rolling policy file pattern does
not seem to contain date or integer token");
- }
- int slashBeforeDigitPatternPosition =
fileNamePattern.lastIndexOf("/", firstDigitPatternPosition);
- if (slashBeforeDigitPatternPosition != -1)
- {
- path = fileNamePattern.substring(0,
slashBeforeDigitPatternPosition);
- fileNamePattern = fileNamePattern.substring(
slashBeforeDigitPatternPosition + 1);
- }
- else
- {
- path = System.getProperty("user.dir");
+ final String message = "Rolling policy file pattern does not
seem to contain date or integer token";
+ throw new RuntimeException(message);
}
+ final int firstTokenPosition = filePathPattern.indexOf('%');
+ final int slashBeforeTokenPosition =
filePathPattern.lastIndexOf('/', firstTokenPosition);
+ final String directoryPattern = filePathPattern.substring(0,
slashBeforeTokenPosition + 1);
+ final String path = directoryPattern.isEmpty() ?
System.getProperty("user.dir") : directoryPattern;
_baseFolder = new File(path).toPath().toAbsolutePath();
- _pattern = Pattern.compile(Pattern.quote(path) + "/" +
fileNamePattern);
+
+ final String directoryRegExp = directoryPattern.isEmpty()
+ ? ""
+ : new FileNamePattern(directoryPattern,
fileNamePattern.getContext()).toRegex();
+ _pattern =
Pattern.compile(filePathRegExp.substring(directoryRegExp.length()));
}
public Path getBaseFolder()
diff --git
a/broker-plugins/logging-logback/src/test/java/org/apache/qpid/server/logging/logback/RollingPolicyDecoratorTest.java
b/broker-plugins/logging-logback/src/test/java/org/apache/qpid/server/logging/logback/RollingPolicyDecoratorTest.java
index 0c23900253..e58cd7f9c2 100644
---
a/broker-plugins/logging-logback/src/test/java/org/apache/qpid/server/logging/logback/RollingPolicyDecoratorTest.java
+++
b/broker-plugins/logging-logback/src/test/java/org/apache/qpid/server/logging/logback/RollingPolicyDecoratorTest.java
@@ -20,6 +20,7 @@
*/
package org.apache.qpid.server.logging.logback;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
@@ -30,22 +31,31 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.time.Instant;
import java.util.Arrays;
+import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.RollingPolicyBase;
import ch.qos.logback.core.rolling.helper.FileNamePattern;
+import ch.qos.logback.core.status.Status;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentMatcher;
import org.slf4j.Logger;
@@ -68,16 +78,15 @@ public class RollingPolicyDecoratorTest extends UnitTestBase
@BeforeEach
public void setUp() throws Exception
{
- _baseFolder = TestFileUtils.createTestDirectory("rollover", true);
+ _baseFolder = TestFileUtils.createTestDirectory("rollover.with.dots",
true);
_testFile = createTestFile("test.2015-06-25.0.gz");
final Context mockContext = mock(Context.class);
_delegate = mock(RollingPolicyBase.class);
final String fileNamePattern = _baseFolder.getAbsolutePath() + "/" +
"test.%d{yyyy-MM-dd}.%i.gz";
- when(_delegate.getFileNamePattern()).thenReturn(fileNamePattern);
when(_delegate.getContext()).thenReturn(mockContext);
_listener = mock(RollingPolicyDecorator.RolloverListener.class);
- _policy = new RollingPolicyDecorator(_delegate, _listener,
createMockExecutorService());
+ _policy = createPolicy(fileNamePattern);
final Pattern rolledFileRegExp = Pattern.compile(new
FileNamePattern(fileNamePattern, mockContext).toRegex());
@@ -100,6 +109,12 @@ public class RollingPolicyDecoratorTest extends
UnitTestBase
return testFile;
}
+ private RollingPolicyDecorator createPolicy(final String fileNamePattern)
+ {
+ when(_delegate.getFileNamePattern()).thenReturn(fileNamePattern);
+ return new RollingPolicyDecorator(_delegate, _listener,
createMockExecutorService());
+ }
+
private ScheduledExecutorService createMockExecutorService()
{
final ScheduledExecutorService executorService =
mock(ScheduledExecutorService.class);
@@ -142,6 +157,98 @@ public class RollingPolicyDecoratorTest extends
UnitTestBase
verify(_listener).onRollover(eq(_baseFolder.toPath()),
argThat(matcher));
}
+ @ParameterizedTest
+ @ValueSource(strings = {"java-mms.1-0", "logs[archive]", "logs(archive)",
"logs(archive", "logs+archive",
+ "logs$archive", "logs^archive", "logs{archive}"})
+ public void testRolloverWithSpecialCharactersInDirectory(final String
directoryName) throws IOException
+ {
+ final Path baseFolder =
Files.createDirectory(_baseFolder.toPath().resolve(directoryName));
+ Files.createFile(baseFolder.resolve(_testFile.getName()));
+ final RollingPolicyDecorator policy =
createPolicy(baseFolder.resolve("test.%d{yyyy-MM-dd}.%i.gz").toString());
+
+ policy.rollover();
+
+ verify(_listener).onRollover(eq(baseFolder), argThat(getMatcher(new
String[]{_testFile.getName()})));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {".%i", ".%i.gz", ".%d{yyyy-MM-dd}.%i",
".%d{yyyy-MM-dd}.%i.gz"})
+ public void testBrokerPatternsDiscoverAndDownloadLogbackArchives(final
String suffix) throws IOException
+ {
+ final Path baseFolder =
Files.createDirectory(_baseFolder.toPath().resolve("logs[old](copy)+$"));
+ final String pattern = baseFolder.resolve("qpid[old](copy)+$.log" +
suffix).toString();
+ final FileNamePattern fileNamePattern = new FileNamePattern(pattern,
_delegate.getContext());
+ final Instant instant = Instant.parse("2026-11-05T12:00:00Z");
+ final Path archive =
Path.of(fileNamePattern.convertMultipleArguments(instant, 12));
+ Files.writeString(archive, "archive content");
+ Files.createFile(archive.resolveSibling("other." +
archive.getFileName()));
+ final RolloverWatcher watcher = new
RolloverWatcher(baseFolder.resolve("qpid[old](copy)+$.log").toString());
+ when(_delegate.getFileNamePattern()).thenReturn(pattern);
+ final ScheduledExecutorService executor = createMockExecutorService();
+ final RollingPolicyDecorator policy = new
RollingPolicyDecorator(_delegate, watcher, executor);
+
+ policy.start();
+
+ final String name = archive.getFileName().toString();
+ assertEquals(List.of(name), List.copyOf(watcher.getRolledFiles()));
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ watcher.getFileContent(name).write(output);
+ assertEquals("archive content",
output.toString(StandardCharsets.UTF_8));
+ }
+
+ @Test
+ public void testRolloverWithRelativeDirectory() throws IOException
+ {
+ final Path baseFolder = Files.createTempDirectory(Path.of("target"),
"rollover.relative.");
+ try
+ {
+ Files.createFile(baseFolder.resolve("archive.0.gz"));
+ final RollingPolicyDecorator policy =
createPolicy(baseFolder.resolve("archive.%i.gz").toString());
+
+ policy.rollover();
+
+ verify(_listener).onRollover(eq(baseFolder.toAbsolutePath()),
+ argThat(getMatcher(new String[]{"archive.0.gz"})));
+ }
+ finally
+ {
+ FileUtils.delete(baseFolder.toFile(), true);
+ }
+ }
+
+ @Test
+ public void testRolloverWithoutDirectory() throws IOException
+ {
+ final Path baseFolder =
Files.createDirectory(_baseFolder.toPath().resolve("logs%literal"));
+ Files.createFile(baseFolder.resolve(_testFile.getName()));
+ setTestSystemProperty("user.dir", baseFolder.toString());
+ final Context context = new ContextBase();
+ when(_delegate.getContext()).thenReturn(context);
+
+ createPolicy("test.%d{yyyy-MM-dd}.%i.gz").rollover();
+
+ verify(_listener).onRollover(eq(baseFolder), argThat(getMatcher(new
String[]{_testFile.getName()})));
+ assertTrue(context.getStatusManager().getCopyOfStatusList().stream()
+ .noneMatch(status -> status.getLevel() == Status.ERROR),
+ "Logback should not report errors for a filename without a
directory");
+ }
+
+ @Test
+ public void testRolloverWithDateInDirectory() throws IOException
+ {
+ final String pattern =
_baseFolder.toPath().resolve("%d{yyyy-MM-dd,aux}/test.%d{yyyy-MM-dd}.%i.gz").toString();
+ final FileNamePattern fileNamePattern = new FileNamePattern(pattern,
_delegate.getContext());
+ final Instant instant = Instant.parse("2026-11-05T12:00:00Z");
+ final Path archive =
Path.of(fileNamePattern.convertMultipleArguments(instant, 12));
+ Files.createDirectory(archive.getParent());
+ Files.createFile(archive);
+
+ createPolicy(pattern).rollover();
+
+ final String name =
_baseFolder.toPath().relativize(archive).toString();
+ verify(_listener).onRollover(eq(_baseFolder.toPath()),
argThat(getMatcher(new String[]{name})));
+ }
+
@Test
public void testRolloverRescanLimit()
{
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]