Skip to content

Commit

Permalink
Enrich log capture to level + message
Browse files Browse the repository at this point in the history
  • Loading branch information
nhojpatrick committed Sep 21, 2022
1 parent 03fca59 commit cd83cfc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class InMemoryAppender

public static final String IN_MEMORY_APPENDER = "InMemoryAppender";

private List<String> messages = new ArrayList<>();
private List<LogStatement> messages = new ArrayList<>();

protected InMemoryAppender(String name,
Filter filter,
Expand All @@ -39,10 +39,13 @@ protected InMemoryAppender(String name,

@Override
public void append(final LogEvent event) {
final String level = event.getLevel()
.toString();
final String message = event.getMessage()
.getFormattedMessage();
final LogStatement logStatement = new LogStatement(level, message);
getMessages()
.add(message);
.add(logStatement);
}

@SuppressFBWarnings(value = {"UP_UNUSED_PARAMETER"}, justification = "As designed")
Expand All @@ -61,13 +64,13 @@ public static InMemoryAppender createAppender(@PluginAttribute("name") String na
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Accepted will look at changing 'return messages;'")
public List<String> getMessages() {
return messages;
public List<LogStatement> getMessages() {
return this.messages;
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP",
justification = "Accepted will look at changing 'this.messages = messages;'")
public void setMessages(List<String> messages) {
public void setMessages(List<LogStatement> messages) {
this.messages = messages;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.nhojpatrick.log4j2.appenders.inmemory;

public class LogStatement {

private final String level;
private final String message;

public LogStatement(final String level,
final String message) {
this.level = level;
this.message = message;
}

public String getLevel() {
return this.level;
}

public String getMessage() {
return this.message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.nhojpatrick.log4j2.appenders.inmemory.InMemoryAppender;
import com.github.nhojpatrick.log4j2.appenders.inmemory.InMemoryAppenderHelper;
import com.github.nhojpatrick.log4j2.appenders.inmemory.LogStatement;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,10 +25,11 @@ public void inMemoryAppender() {
logger.info("Qwerty 1234567890");

final InMemoryAppender inMemoryAppender = InMemoryAppenderHelper.getAppender();
final List<String> messages = inMemoryAppender.getMessages();
final List<LogStatement> messages = inMemoryAppender.getMessages();
System.out.println(messages);
final Optional<String> message = messages
.stream()
.map(p -> p.getMessage())
.filter(p -> p.contains("Qwerty"))
.findFirst();

Expand Down

0 comments on commit cd83cfc

Please sign in to comment.