Skip to content

Commit

Permalink
Merge pull request #4 from dm-drogeriemarkt/update-checkstyle
Browse files Browse the repository at this point in the history
fix issue with multiline log messages
  • Loading branch information
waschmittel authored Feb 14, 2020
2 parents 8465b8e + b43197a commit 2e6db26
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.dm.infrastructure</groupId>
<artifactId>log-capture</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.0.1-SNAPSHOT</version>

<name>Log Capture</name>
<description>Makes it possible to capture logs and assert if things have been logged</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 2e6db26

Please sign in to comment.