From b43197a60bd8a074180afc90bc6fa5ce12ef3c1a Mon Sep 17 00:00:00 2001 From: Daniel Flassak Date: Fri, 14 Feb 2020 14:56:21 +0100 Subject: [PATCH] fix issue with multiline log messages --- README.md | 9 ++++++++- pom.xml | 2 +- .../dm/infrastructure/logcapture/CapturingAppender.java | 2 +- .../java/de/dm/infrastructure/logcapture/LogCapture.java | 2 +- .../de/dm/infrastructure/logcapture/LogCaptureTest.java | 7 +++++++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc088ad..79b4224 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,12 @@ Because this is a library, Checkstyle is used to make sure all public classes/me * [Cucumber stepdefs](#Cucumber-stepdefs) * [Cucumber DTOs](#Cucumber-DTOs) -## Usage + +## Changes + +### 2.0.1 + +Fixed a bug where multiline log messages (for example Messages that contain a stack trace) could not be matched. ### Updating from Version 1.x.x to 2.x.x @@ -30,6 +35,8 @@ Because this is a library, Checkstyle is used to make sure all public classes/me * `logCapture.addAppender()` has been replaced with `logCapture.addAppenderAndSetLogLevelToDebug()` * `logCapture.removeAppender()` has been replaced with `logCapture.removeAppenderAndResetLogLevel()` +## Usage + ### Maven Add log-capture as a test dependency to your project. If you use Maven, add this to your pom.xml: diff --git a/pom.xml b/pom.xml index c9846e2..9aef0b3 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ de.dm.infrastructure log-capture - 2.0.0-SNAPSHOT + 2.0.1-SNAPSHOT Log Capture Makes it possible to capture logs and assert if things have been logged diff --git a/src/main/java/de/dm/infrastructure/logcapture/CapturingAppender.java b/src/main/java/de/dm/infrastructure/logcapture/CapturingAppender.java index 11b6638..b1eede7 100644 --- a/src/main/java/de/dm/infrastructure/logcapture/CapturingAppender.java +++ b/src/main/java/de/dm/infrastructure/logcapture/CapturingAppender.java @@ -55,7 +55,7 @@ private boolean eventIsRelevant(ILoggingEvent loggingEvent) { } Integer whenCapturedNext(Level level, String regex, int startIndex, ExpectedMdcEntry... expectedMdcEntries) { - Pattern pattern = Pattern.compile(".*" + regex + ".*"); + Pattern pattern = Pattern.compile(".*" + regex + ".*", Pattern.DOTALL + Pattern.MULTILINE); LoggedEvent eventWithWrongMdcContents = null; for (int i = startIndex; i < loggedEvents.size(); i++) { LoggedEvent event = loggedEvents.get(i); diff --git a/src/main/java/de/dm/infrastructure/logcapture/LogCapture.java b/src/main/java/de/dm/infrastructure/logcapture/LogCapture.java index b9d356e..3300f9d 100644 --- a/src/main/java/de/dm/infrastructure/logcapture/LogCapture.java +++ b/src/main/java/de/dm/infrastructure/logcapture/LogCapture.java @@ -122,7 +122,7 @@ public void removeAppenderAndResetLogLevel() { * assert that some message has been logged * * @param level expected log level - * @param regex regex to match formatted log message + * @param regex regex to match formatted log message (with Pattern.DOTALL and Pattern.MULTILINE) * @param expectedMdcEntries expected MDC entries, see @{@link ExpectedMdcEntry} * * @return a LastCapturedLogEvent from which .thenLogged(...) can be called to assert if things have been logged in a specific order diff --git a/src/test/java/de/dm/infrastructure/logcapture/LogCaptureTest.java b/src/test/java/de/dm/infrastructure/logcapture/LogCaptureTest.java index a6286df..3703efc 100644 --- a/src/test/java/de/dm/infrastructure/logcapture/LogCaptureTest.java +++ b/src/test/java/de/dm/infrastructure/logcapture/LogCaptureTest.java @@ -34,6 +34,13 @@ public void twoLogMessagesInOrder() { .thenLogged(Level.ERROR, "terrible"); } + @Test + public void captureMultilineMessages() { + log.info("something interesting\nwith something else in other lines, for example exception details"); + + logCapture.assertLogged(Level.INFO, "something interesting"); + } + @Test public void captureLogsForMultiplePackages() { log.info("something interesting");